Callback - object interface for function callbacks
use Callback; my $callback = new Callback (\&myfunc, @myargs); my $callback = new Callback ($myobj, $mymethod, @myargs); $callback->call(@some_more_args);
Callback provides a simple interface for function callbacks. It would be possible to provide nearly all of the functionality using inline code references. Callback was written before inline code references were added to perl.
What callback does provide is a simple interface for common situation: passing around a function.
When a callback is constructed, a base set of arguments can be provided. These function arguments will preceed any arguments added at the time the call is made.
There are two forms for the callback constructor, depending on whether the call is a pure functional call or a method call. The rule is that if the first argument is an object, then the second argument is a method name to be called on that object.
use Callback qw(@callbackTrace);
If you're writing a debugging routine that provides a stack-dump (for example, Carp::confess) it is useful to know where a callback was registered.
my $ct = 0; while (($package, $file, $line, $subname, $hasargs, $wantarray) = caller($i++)) { ... if ($subname eq 'Callback::call') { print "callback registered $Callback::callbackTrace[$ct]\n"; $ct++; } }
Without such code, it becomes very hard to know what's going on.
Copyright (C) 1994, 2000, David Muir Sharnoff. All rights reserved. License hearby granted for anyone to use this module at their own risk. Please feed useful changes back to muir@idiom.com.
To install Callback, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Callback
CPAN shell
perl -MCPAN -e shell install Callback
For more information on module installation, please visit the detailed CPAN module installation guide.