|
NAMEPOEx::Role::SessionInstantiation - A Moose Role for turning objects into POE SessionsVERSIONversion 1.102610DESCRIPTIONPOEx::Role::SessionInstantiation provides a nearly seamless integration for non-POE objects into a POE environment. It does this by handling the POE stuff behind the scenes including allowing per instances method changes, session registration to the Kernel, and providing some defaults like setting an alias if supplied via the attribute or constructor argument, or defining a _default that warns if your object receives an event that it does not have.This role exposes your class' methods as POE events. SYOPSISpackage My::Class; use 5.010; use MooseX::Declare; class My::Class { # using the role instantly makes it a POE::Session upon instantiation with 'POEx::Role::SessionInstantiation'; # alias the decorator use aliased 'POEx::Role::Event'; # decorated methods are all exposed as events to POE method foo (@args) is Event { # This event is not only added through POE but also added as a # method to each instance that happens to have 'foo' fired # Access to POE information is done through the 'poe' accessor $self->poe->kernel->state ( 'added_event', sub { say 'added_event has fired' } ); # Some sugar to access the kernel's yield method # This will push the 'bar' method into POE's event queue $self->yield('bar'); } method bar (@args) { # $self is also safe to pass as a session reference # Or you can pass along $self->ID() $self->post($self, 'baz') } method baz (@args) { # call also works too $self->call($self, 'added_event'; } } # Constructing the session takes all the normal options my $session = My::Class->new({ options => { trace => 1 } }); # Still need to call ->run(); POE::Kernel->run(); NOTESLike all dangerous substances, this Role needs a big fat warning. It should be noted thoroughly that this Role performs some pretty heinous magic to accomplish a polished and consistent transformation of your class into a Session.
So please heed the warnings and don't blame me if this summons the terrasque into your datacenter and you left your +5 gear at home. FURTHER NOTESCUSTOMIZING VIA TRAITSPOEx::Role::SessionInstantiation now allows for Trait declarations upon import. This is similar to how Moose itself allows for modification of its own Meta things through arguments passed to use (ie. use Moose -traits => qw /Foo/), but allows for parameterized roles. Below are some examples of using traits to modify POEx::Role::SessionInstantiation's default behavior.First let's declare a role that frobinates something at start: use MooseX::Declare; role FrobAtStart { with 'POEx::Role::SessionInstantiation::Meta::Session::Events'; has frobinator => ( is => 'ro', isa => 'Object', required => 1, handles => { 'frob' => 'frob' } ); after _start is POEx::Role::Event { $self->frob(); } } And how about a logger role that logs unknown delivered events that wants the logging method/event to be a named parameter role SomeLogger(Str :$foo) { with 'POEx::Role::SessionInstantiation::Meta::Session::Events'; has logger => ( is => 'ro', isa => 'Object', required => 1 ); method $foo(Str $event) is POEx::Role::Event { $self->logger->log("Unknown event: $event") } after _default is POEx::Role::Event { $self->$foo($self->poe->state); } } Now let's use them class My::Session { # need to make sure these are loaded use FrobinateAtStart; use SomeLogger; # and now the magic use POEx::Role::SessionInstantiation traits => [ 'FrobAtStart', SomeLogger => { foo => 'log' } ]; # compose it now that it has traits applied with 'POEx::Role::SessionInstantiation'; ... } For more information on how this mechanism works, please see MooseX::CompileTime::Traits WRITING YOUR OWN TRAITSTo make it easy to advise just little parts of POEx::Role::SessionInstantiation it is broken down into a few different roles that you can 'with' like in the examples above.
Please see their POD for more details on the inner workings of this module. AUTHORNicholas Perez <nperez@cpan.org>COPYRIGHT AND LICENSEThis software is copyright (c) 2010 by Nicholas Perez.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |