NAME

POE::Session::Attributes - Use attributes to define your POE Sessions

SYNOPSIS

# in Some/Module.pm

package Some::Module ;
use base qw(POE::Session::Attributes) ;
use POE ;

sub _start : Package {   # package state
    my ($pkg, @args) = @_[OBJECT, ARG0 .. $#_] ;
    ...
}

sub _stop : Object {     # object state
    my ($self, ...) = @_[OBJECT, ...] ;
    ...
}

sub some_other_event : Inline {  # inline state
    print "boo hoo\n" ;
}

...
1 ;

# meanwhile, in some other file

use Some::Module ;
use POE ;

my $new_session_id =
    Some::Module->spawn("your", {arguments => "here"}) ;

...

POE::Kernel->run() ;

# Inheritance works, too
package Some::Module::Subclass ;
use base qw(Some::Module) ;

sub _stop : Object {
    my ($self, @rest) = @_ ;
    do_some_local_cleanup() ;
    $self->SUPER::_stop(@rest) ;  # you can call parent method, too
}

DESCRIPTION

This module's purpose is to save you some boilerplate code around POE::Session->create() method. Just inherit your class from POE::Session::Attributes and define some states using attributes. Method spawn() in your package will be provided by POE::Session::Attributes (of course, you can override it, if any).

ATTRIBUTES

sub your_sub : Package

Makes a package state. Name of your subroutine ("your_sub") will be used as state name.

sub your_sub : Inline

Makes an inline state. Name of your subroutine ("your_sub") will be used as state name.

sub your_sub : Object

Makes an object state. Name of your subroutine ("your_sub") will be used as state name. An instance of your class will be created by spawn() method, if at least one Object state is defined in your package. Method new() from your package will be called to create the instance. Arguments for the call to new() will be the same as specified for spawn() call.

METHODS

new()

POE::Session::Attributes provides a default constructor (bless {}, $class). You can (and probably should) override it in your inheriting class. new() will be called by spawn() if at least one Object state is defined.

spawn()

Creates a new POE::Session based on your class/package. An argument list for spawn() method will be used for "args" parameter to POE::Session->create(). The same argument list will be used to call new(), if you have Object states in your class/package.

Yes, it's probably somewhat messy. Suggest a fix.

When called in scalar context, returns a reference to a newly created POE::Session (but make sure to read POE::Session documentation to see why you shouldn't use it). In list context, returns a reference to POE::Session and a reference to a newly created instance of your class (in case it was really created).

SEE ALSO

POE, POE::Session, attributes.

There is a somewhat similar module on CPAN, POE::Session::AttributeBased.

AUTHOR

dmitry kim, <dmitry.kim@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2006 by dmitry kim

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.