File Coverage

blib/lib/Reflex/Callback/CodeRef.pm
Criterion Covered Total %
statement 5 5 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod 1 1 100.0
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Reflex::Callback::CodeRef;
2             # vim: ts=2 sw=2 noexpandtab
3             $Reflex::Callback::CodeRef::VERSION = '0.100';
4 13     13   105 use Moose;
  13         17  
  13         92  
5             extends 'Reflex::Callback';
6              
7             has code_ref => (
8             is => 'ro',
9             isa => 'CodeRef',
10             required => 1,
11             );
12              
13             sub deliver {
14 9     9 1 17 my ($self, $event) = @_;
15 9         248 $self->code_ref()->($self->object(), $event);
16             }
17              
18             __PACKAGE__->meta->make_immutable;
19              
20             1;
21              
22             __END__
23              
24             =pod
25              
26             =encoding UTF-8
27              
28             =for :stopwords Rocco Caputo
29              
30             =head1 NAME
31              
32             Reflex::Callback::CodeRef - Callback adapter for plain code references
33              
34             =head1 VERSION
35              
36             This document describes version 0.100, released on April 02, 2017.
37              
38             =head1 SYNOPSIS
39              
40             Used within Reflex:
41              
42             use Reflex::Callbacks qw(cb_coderef);
43              
44             my $ct = Reflex::Interval->new(
45             interval => 1 + rand(),
46             auto_repeat => 1,
47             on_tick => cb_coderef {
48             print "coderef callback triggered\n";
49             },
50             );
51              
52             $ct->run_all();
53              
54             Low-level usage:
55              
56             sub callback {
57             my $arg = shift;
58             print "hello, $arg->{name}\n";
59             }
60              
61             use Reflex::Callback;
62             my $cb = Reflex::Callback::CodeRef->new( code_ref => \&code );
63             $cb->deliver(greet => { name => "world" });
64              
65             =head1 DESCRIPTION
66              
67             Reflex::Callback::CodeRef maps the generic Reflex::Callback interface
68             to plain coderef callbacks. Reflex::Callbacks' cb_coderef() function
69             and other syntactic sweeteners hide the specifics.
70              
71             =head2 new
72              
73             Reflex::Callback::CodeRef's constructor takes a single named
74             parameter, "code_ref", which should contain the coderef to be called
75             by deliver().
76              
77             =head2 deliver
78              
79             Reflex::Callback::CodeRef's deliver() method invokes the coderef
80             supplied during the callback's construction. deliver() takes two
81             positional parameters: an event name (which is not currently used for
82             coderef callbacks), and a hashref of named parameters to be passed to
83             the callback.
84              
85             deliver() returns whatever the coderef does.
86              
87             =head1 SEE ALSO
88              
89             Please see those modules/websites for more information related to this module.
90              
91             =over 4
92              
93             =item *
94              
95             L<Reflex|Reflex>
96              
97             =item *
98              
99             L<Reflex>
100              
101             =item *
102              
103             L<L<Reflex::Callback> documents the base class' generic interface.|L<Reflex::Callback> documents the base class' generic interface.>
104              
105             =item *
106              
107             L<L<Reflex::Callbacks> documents callback convenience functions.|L<Reflex::Callbacks> documents callback convenience functions.>
108              
109             =item *
110              
111             L<Reflex/ACKNOWLEDGEMENTS>
112              
113             =item *
114              
115             L<Reflex/ASSISTANCE>
116              
117             =item *
118              
119             L<Reflex/AUTHORS>
120              
121             =item *
122              
123             L<Reflex/BUGS>
124              
125             =item *
126              
127             L<Reflex/BUGS>
128              
129             =item *
130              
131             L<Reflex/CONTRIBUTORS>
132              
133             =item *
134              
135             L<Reflex/COPYRIGHT>
136              
137             =item *
138              
139             L<Reflex/LICENSE>
140              
141             =item *
142              
143             L<Reflex/TODO>
144              
145             =back
146              
147             =head1 BUGS AND LIMITATIONS
148              
149             You can make new bug reports, and view existing ones, through the
150             web interface at L<http://rt.cpan.org/Public/Dist/Display.html?Name=Reflex>.
151              
152             =head1 AUTHOR
153              
154             Rocco Caputo <rcaputo@cpan.org>
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2017 by Rocco Caputo.
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =head1 AVAILABILITY
164              
165             The latest version of this module is available from the Comprehensive Perl
166             Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
167             site near you, or see L<https://metacpan.org/module/Reflex/>.
168              
169             =head1 DISCLAIMER OF WARRANTY
170              
171             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
172             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
173             WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
174             PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
175             EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
176             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
177             PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
178             SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
179             THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
180              
181             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
182             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
183             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
184             TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
185             CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
186             SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
187             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
188             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
189             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
190             DAMAGES.
191              
192             =cut