| POE-Component-SubWrapper documentation | Contained in the POE-Component-SubWrapper distribution. |
POE::Component::SubWrapper - event based wrapper for subs
use POE::Component::SubWrapper;
POE::Component::SubWrapper->spawn('main');
$kernel->post('main', 'my_sub', [ $arg1, $arg2, $arg3 ], 'callback_state');
This is a module which provides an event based wrapper for subroutines.
POE::Component::SubWrapper->spawn('main');
SubWrapper components are not normal objects, but are instead 'spawned' as separate sessions. This is done with with PoCo::SubWrapper's 'spawn' method, which takes one required and one optional argument. The first argument is the package name to wrap. This is required. The second argument is optional and contains an alias to give to the session created. If no alias is supplied, the package name is used as an alias.
poeize My::Package;
Another way to create SubWrapper components is to use the poeize method,
which is included in the default export list of the package. You can simply
do:
poeize Data::Dumper;
and Data::Dumper will be wrapped into a session with the alias 'Data::Dumper'.
When a SubWrapper component is created, it scans the package named for subroutines, and creates one state in the session created with the same name of the subroutine.
The states each accept 3 arguments:
ARG0 and are either a scalar
if the function was called in scalar context, or an arrayref of results
if the function was called in list context.The test scripts are the best place to look for examples of POE::Component::Subwrapper usage. A short example is given here:
use Data::Dumper;
poeize Data::Dumper;
$kernel->post('Data::Dumper', 'Dumper', [ { a => 1, b => 2 } ], 'callback_state', 'SCALAR');
sub callback_handler {
my $result = @_[ARG0];
# do something with the string returned by Dumper({ a => 1, b => 2})
}
Data::Dumper is the wrapped module, Dumper is the function called, {a =>
1, b => 2} is the data structure that is dumped, and $result is the
resulting string form.
The module exports the following functions by default:
poeizeA function called with a single bareword argument specifying the package to be wrapped.
Matt Cashner (sungo@pobox.com)
Michael Stevens - michael@etla.org
Copyright (c) 2000-2002 Michael Stevens
Copyright (c) 2002-2004 Matt Cashner
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
| POE-Component-SubWrapper documentation | Contained in the POE-Component-SubWrapper distribution. |
package POE::Component::SubWrapper;
use warnings; use strict; use Carp qw(croak); use POE; use Devel::Symdump; use Exporter::Lite; our @EXPORT = qw(poeize); our $VERSION = '2.01'; use constant DEBUG => 0;
sub spawn { #{{{ DEBUG && print "PoCo::SubWrapper->spawn: Entering\n"; my $type = shift; my $package = shift; my $alias = shift; croak "Too many args" if scalar @_; $alias = $package unless defined($alias) and length($alias); DEBUG && print "PoCo::SubWrapper->spawn: type = [$type], package = [$package], alias = [$alias]\n"; # get subroutines defined by package. my @subs; my $sym = Devel::Symdump->new($package); { no strict 'refs'; foreach my $function ($sym->functions) { *p = *$function; my $coderef = *p{CODE}; my ($key) = ($function =~ /([^:]*)$/); use Data::Dumper; DEBUG && print "Symbol is $function\n"; DEBUG && print "key is $key\n"; DEBUG && print "Coderef is [", Dumper($coderef), "]\n"; push @subs, { name => $key, code => $coderef }; } } my %states; foreach my $sub (@subs) { DEBUG && print "Building state for ", $sub->{name}, "\n"; $states{$sub->{name}} = build_handler($package, $sub->{code}); } $states{'_start'} = \&wrapper_start; $states{'_stop'} = sub {}; my $s = POE::Session->create( inline_states => \%states, heap => { alias => $alias, } ); return $s; } #}}}
sub poeize (*) { #{{{ my $package = shift; spawn($package, $package); } #}}}
sub wrapper_start { #{{{ $_[KERNEL]->alias_set($_[HEAP]->{alias}); } #}}}
sub shutdown { #{{{ $_[KERNEL]->alias_remove($_[HEAP]->{alias}); } #}}}
sub build_handler { #{{{ my ($package, $sub) = @_; my $ref = sub { my ($kernel, $heap, $args_ref, $callback, $context, $sender) = @_[KERNEL, HEAP, ARG0, ARG1, ARG2, SENDER]; warn Dumper($kernel->alias_list($_[SESSION])); DEBUG && print "Handler called for package=[$package], sub = [$sub]\n"; my @sub_args = @$args_ref; my $result; #my $name = "${package}::${sub}"; DEBUG && print "handler: calling [$sub]\n"; if (defined($context) and $context eq 'SCALAR') { # scalar context. default if not supplied. DEBUG && print "handler: calling in scalar context\n"; $result = scalar &{$sub}(@sub_args); } else { # array context. my @result; DEBUG && print "handler: calling in array context\n"; @result = &{$sub}(@sub_args); $result = \@result; } $kernel->post($sender, $callback, $result); return; }; return $ref; } #}}} 1; __END__
# sungo // vim: ts=4 sw=4 et