Params::Callbacks - Enable functions to accept blocking callbacks
# Using the object oriented calling style... use Params::Callbacks; sub counted { my ($callbacks, @args) = Params::Callbacks->extract(@_); $callbacks->yield(@args); } my $size = counted 1, 2, 3, sub { print "> $_\n" for @_; return @_; }; print "$size items\n"; # > 1 # > 2 # > 3 # 3 items # Or, just mix-in the "callbacks" function... use Params::Callbacks qw/callbacks/; sub counted { my ($callbacks, @args) = &callbacks; $callbacks->yield(@args); } my $size = counted 'A', 'B', 'C', sub { print "> $_\n" for @_; return @_; }; print "$size items\n"; # > A # > B # > C # 3 items
This package provides the developer with an easy and consistent method for converting a function that returns a result, into a function that will allow its result to pass through one or more blocking callbacks, before it is delivered to the caller.
It is up to the function's author to decide how and where in the function's flow those callbacks should be invoked. This is could be important during the creation of sets of results: one could apply the callbacks to a finished set, or apply them to each element of the result set as it is added.
Accepts a list of values and strips off any trailing code-references, which are used to create a callback queue. A new list is returned consisting of a reference to the callback queue, followed by the originally listed items that were not callbacks. Note that only trailing code-references are considered callbacks; once an inelligible items is encountered the collection stops.
A callback queue may be empty and that's fine.
A special form of call to callbacks, using the current @_.
callbacks
@_
Yields a result up to the callback queue, returning whatever comes out at the other end.
A result will pass through an empty callback queue unmodified.
On their own, callbacks receive their input result as a list; @_, to be precise, since they're really only functions.
When invoking a function that accepts callbacks, you might string a sequence of code-references, or anonymous sub blocks together, being careful to separate each witha comma (,), e.g:
sub
function ARGUMENTS, sub { ... }, sub { ... }, sub { ... };
Alternatively, use the list function to do exactly the same but dispense line-noise altogether:
list
function ARGUMENTS, list { ... } list { ... } list { ... }
Yes, much easier on the eye!
Use in place of list when you want the input result one item at a time, i.e. even though the result is a list, the callback is called once for each item in the list and all items are gathered before being passed on.
Both the item and list callbacks may be mixed freely.
item
None.
:all
Everything in @EXPORT_OK.
Please report any bugs to http://rt.cpan.org/
Iain Campbell <cpanic@cpan.org>
Copyright (C) 2012 by Iain Campbell
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.
To install Params::Callbacks, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Params::Callbacks
CPAN shell
perl -MCPAN -e shell install Params::Callbacks
For more information on module installation, please visit the detailed CPAN module installation guide.