NAME

PApp::Callback - a workaround for the problem of nonserializable code.


SYNOPSIS

 use PApp::Callback;
 my $function = register_callback BLOCK [key => value...];
 my $cb = $function->refer([args...]);
 &$cb;
 my $cb = create_callback BLOCK [key => value...];


DESCRIPTION

HIGHLY EXPERIMENTAL MODULE, INDEED!

The problem: Code is unserializable (at the moment, but it will probably never be efficient to serialize code).

The workaround (not the solution): This class can be used to create serializable callbacks (or ``references''). You first have to register all possible callback functions (in every process, and before you try to call callbacks). Future versions might allow loading files or strings with the function definition.

register_callback functiondef, key => value...
Registers a function (preferably at program start) and returns a callback object that can be used to create callable but serializable objects.

If functiondef is a string it will be interpreted as a function name in the callers package (unless it conatins '::'). Otherwise you should use a ``name => <funname>'' argument to uniquely identify the function. If it is omitted the filename and linenumber will be used, but that is fragile.

Examples:

 my $func = register_callback {
               print "arg1=$_[0] (should be 5), arg2=$_[1] (should be 7)\n";
            } name => "toytest_myfunc1";
 my $cb = $func->refer(5);
 # experimental alternative: $func->(5)
 # ... serialize and deserialize $cb using Data::Dumper, Storable etc..
 # should call the callback with 5 and 7
 $cb->(7);

create_callback <same arguments as register_callback>
Just like register_callback, but additionally calls refer on the result, returning the function reference directly.

$cb = $func->refer([args...])
Create a callable object (a code reference). The callback $cb can either be executed by calling the call method or by treating it as a code reference, e.g.:
 $cb->call(4,5,6);
 $cb->(4,5,6);
 &$cb; # this does not work with Storable-0.611 and below

It will behave as if the original registered callback function would be called with the arguments given to register_callback first and then the arguments given to the call-method.

refer is implemented in a fast way and the returned objects are optimised to be as small as possible.

$cb->call([args...])
Call the callback function with the given arguments.


BUGS

 - should be able to serialize code at all costs
 - should load modules or other thingies on demand
 - the 'type' (ref $cb) of a callback is not CODE


SEE ALSO

the PApp manpage.


AUTHOR

 Marc Lehmann <pcg@goof.com>
 http://www.goof.com/pcg/marc/