The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

POE::Component::SubWrapper - event based wrapper for subs

SYNOPSIS

  use POE::Component::SubWrapper;
  POE::Component::SubWrapper->spawn('main');
  $kernel->post('main', 'my_sub', [ $arg1, $arg2, $arg3 ], 'callback_state');

DESCRIPTION

This is a module which provides an event based wrapper for subroutines.

METHODS

spawn

  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

  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'.

STATES

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:

  • An arrayref to a list of arguments to give the subroutine.

  • A state to callback with the results.

  • A string, either 'SCALAR', or 'ARRAY', allowing you to decide which context the function handled by this state will be called in.

    The states all call the function with the name matching the state, and give it the supplied arguments. They then postback the results to the named callback state. The results are contained in 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.

EXAMPLES

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.

EXPORT

The module exports the following functions by default:

poeize

A function called with a single bareword argument specifying the package to be wrapped.

AUTHOR

Matt Cashner (sungo@pobox.com)

Michael Stevens - michael@etla.org

LICENSE

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:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of Matt Cashner nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

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.