File Coverage

blib/lib/Reflex.pm
Criterion Covered Total %
statement 12 19 63.1
branch 1 4 25.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 19 29 65.5


line stmt bran cond sub pod time code
1             package Reflex;
2             # vim: ts=2 sw=2 noexpandtab
3             $Reflex::VERSION = '0.100';
4 9     9   404401 use warnings;
  9         16  
  9         274  
5 9     9   31 use strict;
  9         11  
  9         172  
6              
7 9     9   35 use Carp qw(croak);
  9         10  
  9         1584  
8              
9             sub import {
10 10     10   27 my $class = shift;
11 10 50       210 return unless @_;
12              
13 0         0 my $caller_package = caller();
14              
15             # Use the packages in the caller's package.
16             # TODO - Is there a way to place the use in the caller's package
17             # without the eval?
18              
19             eval join(
20             "; ",
21             "package $caller_package",
22 0         0 map { "use $class\::$_" }
  0         0  
23             @_
24             );
25              
26             # Rewrite the error so that it comes from the caller.
27 0 0       0 if ($@) {
28 0         0 my $msg = $@;
29 0         0 $msg =~ s/(\(\@INC contains.*?\)) at .*/$1/s;
30 0         0 croak $msg;
31             }
32             }
33              
34             sub run_all {
35 8     8 1 741 Reflex::Base->run_all();
36             }
37              
38             1;
39              
40             __END__
41              
42             =pod
43              
44             =encoding UTF-8
45              
46             =for :stopwords Rocco Caputo cpan testmatrix url annocpan anno bugtracker rt cpants
47             kwalitee diff irc mailto metadata placeholders metacpan
48              
49             =head1 NAME
50              
51             Reflex - Class library for flexible, reactive programs.
52              
53             =head1 VERSION
54              
55             This document describes version 0.100, released on April 02, 2017.
56              
57             =head1 SYNOPSIS
58              
59             The distribution includes a few different versions of this synopsis.
60             See eg/eg-18-synopsis-no-moose.pl if you don't like Moose.
61             See eg/eg-32-promise-tiny.pl if you prefer promises (condvar-like).
62             See eg/eg-36-coderefs-tiny.pl if you prefer coderefs and/or closures.
63              
64             {
65             package App;
66             use Moose;
67             extends 'Reflex::Base';
68             use Reflex::Interval;
69             use Reflex::Trait::Watched qw(watches);
70              
71             watches ticker => (
72             isa => 'Reflex::Interval',
73             setup => { interval => 1, auto_repeat => 1 },
74             );
75              
76             sub on_ticker_tick {
77             print "tick at ", scalar(localtime), "...\n";
78             }
79             }
80              
81             exit App->new()->run_all();
82              
83             =head1 DESCRIPTION
84              
85             Reflex is a class library that assists with writing reactive (AKA
86             event-driven) programs. Reflex uses Moose internally, but it doesn't
87             enforce programs to use Moose's syntax.
88              
89             Those who enjoy Moose should find useful Reflex's comprehensive suite
90             of reactive roles.
91              
92             Reflex is considered "reactive" because it's an implementation of the
93             reactor pattern. http://en.wikipedia.org/wiki/Reactor_pattern
94              
95             =head2 About Reactive Objects
96              
97             Reactive objects provide responses to interesting (to them) stimuli.
98             For example, an object might be waiting for input from a client, a
99             signal from an administrator, a particular time of day, and so on.
100             The App object in the SYNOPSIS is waiting for timer tick events. It
101             generates console messages in response to those events.
102              
103             =head2 Example Reactive Objects
104              
105             Here an Echoer class emits "pong" events in response to ping()
106             commands. It uses Moose's extends(), but it could about as easily use
107             warnings, strict, and base instead. Reflex::Base gets its emit()
108             method from Reflex::Role::Reactive.
109              
110             package Echoer;
111             use Moose;
112             extends 'Reflex::Base';
113              
114             sub ping {
115             my ($self, $args) = @_;
116             print "Echoer was pinged!\n";
117             $self->emit( -name => "pong" );
118             }
119              
120             The next object uses Echoer. It creates an Echoer and pings it to get
121             started. It also reacts to "pong" events by pinging the Echoer again.
122             Reflex::Trait::Watched (via its exported watches() declarative syntax)
123             implicitly watches the object in echoer(), mapping its "pong" event to
124             the on_echoer_pong() method.
125              
126             package Pinger;
127             use Moose;
128             extends 'Reflex::Base';
129             use Reflex::Trait::Watched qw(watches);
130              
131             watches echoer => (
132             isa => 'Echoer',
133             default => sub { Echoer->new() },
134             );
135              
136             sub BUILD {
137             my $self = shift;
138             $self->echoer->ping();
139             }
140              
141             sub on_echoer_pong {
142             my $self = shift;
143             print "Pinger got echoer's pong!\n";
144             $self->echoer->ping();
145             }
146              
147             Then the Pinger would be created and run.
148              
149             Pinger->new()->run_all();
150              
151             A complete, runnable version of this example is in the distribution as
152             eg/eg-37-ping-pong.pl.
153              
154             =head2 Coderef Callbacks
155              
156             Reflex supports any conceivable callback type, even the simple ones:
157             plain old coderefs. You don't need to write objects to handle events.
158              
159             Here we'll start a periodic timer and handle its ticks with a simple
160             callback. The program is still reactive. Every second it prints
161             "timer ticked" in response Reflex::Interval's events.
162              
163             my $t = Reflex::Interval->new(
164             interval => 1,
165             auto_repeat => 1,
166             on_tick => sub { say "timer ticked" },
167             );
168              
169             $t->run_all();
170              
171             A complete, runnable version of the above example is available as
172             eg/eg-36-tiny-coderefs.pl in the distribution.
173              
174             =head2 Promises Instead of Callbacks
175              
176             Callback haters are not left out. Reflex objects may also be used as
177             asynchronous event generators. The following example is identical in
178             function to the previous coderef callback example, but it doesn't use
179             callbacks at all.
180              
181             It may not be obvious that the same emit() method drives all of
182             Reflex's forms of callback. The same Reflex::Interval class can be
183             used in many different ways.
184              
185             use Reflex::Interval;
186              
187             my $t = Reflex::Interval->new(
188             interval => 1,
189             auto_repeat => 1,
190             );
191              
192             while (my $event = $t->next()) {
193             say "next() returned an event (@$event)";
194             }
195              
196             =head1 PUBLIC METHODS
197              
198             Reflex itself contains some convenience methods for cleaner semantics.
199              
200             =head2 run_all
201              
202             Run all active Reflex objects until they destruct.
203              
204             # (Omitted: First you'll need to create some Reflex objects.)
205              
206             Reflex->run_all();
207             exit;
208              
209             =head1 BUNDLED MODULES AND DOCUMENTATION INDEX
210              
211             Reflex bundles a number of helpful base classes to get things started.
212              
213             =head2 Core Modules
214              
215             The basic modules upon which most everything else is built.
216              
217             =head3 Reflex - You're reading it!
218              
219             =head3 Reflex::Base - A base class for reactive (aka, event driven) objects.
220              
221             =head3 Reflex::Role - Define a new Reflex parameterized role.
222              
223             =head3 Reflex::Role::Reactive - Add non-blocking reactive behavior to a class.
224              
225             =head2 Callback Adapters
226              
227             Reflex provides adapters for nearly every kind of callback that
228             exists, including condvar-like promises that allow Reflex objects to
229             be used inline without callbacks at all.
230              
231             =head3 Reflex::Callback - A base class for callback adapters.
232              
233             =head3 Reflex::Callback::CodeRef - Implement plain coderef callbacks.
234              
235             =head3 Reflex::Callback::Method - Implement class and object method callbacks.
236              
237             =head3 Reflex::Callback::Promise - Return events procedurally rather than via callbacks.
238              
239             =head3 Reflex::Callbacks - Convenience functions to creating and use callbacks.
240              
241             =head2 POE Adapters
242              
243             POE provides over 400 modules for various useful things. Reflex can
244             work with them using these adapters.
245              
246             =head3 Reflex::POE::Event - Communicate with POE components that expect command events.
247              
248             =head3 Reflex::POE::Postback - Communicate with POE components that respond via postbacks.
249              
250             =head3 Reflex::POE::Session - Communicate with POE components that expect to talk to POE sessions.
251              
252             =head3 Reflex::POE::Wheel - A generic POE::Wheel adapter to use them in Reflex.
253              
254             =head3 Reflex::POE::Wheel::Run - Adapt POE::Wheel::Run by wrapping it in a Reflex class.
255              
256             =head2 Object Collections
257              
258             It's often useful to manage collections of like-typed modules, such as
259             connections or jobs.
260              
261             =head3 Reflex::Collection - Automatically manage a collection of collectible objects.
262              
263             =head3 Reflex::Role::Collectible - Allow objects to be managed by Reflex::Collection.
264              
265             =head3 Reflex::Sender - API to access the objects an event has passed through.
266              
267             =head2 I/O
268              
269             Event driven programs most often react to I/O of some sort. These
270             modules provide reactive I/O support.
271              
272             =head3 Reflex::Acceptor - A non-blocking server (client socket acceptor).
273              
274             =head3 Reflex::Client - A non-blocking socket client.
275              
276             =head3 Reflex::Connector - A non-blocking client socket connector.
277              
278             =head3 Reflex::Role::Accepting - Add non-blocking connection accepting to a role.
279              
280             =head3 Reflex::Role::Connecting - Add non-blocking client connecting to a class.
281              
282             =head3 Reflex::Role::InStreaming - Add non-blocking streaming input behavior to a class.
283              
284             =head3 Reflex::Role::OutStreaming - Add non-blocking streaming output behavior to a class.
285              
286             =head3 Reflex::Role::Readable - Add non-blocking readable-watching behavior to a class.
287              
288             =head3 Reflex::Role::Reading - Add standard non-blocking sysread() behavior to a class.
289              
290             =head3 Reflex::Role::Recving - Add standard non-blocking send/recv behavior to a class.
291              
292             =head3 Reflex::Role::Streaming - Add non-blocking streaming I/O behavior to a class.
293              
294             =head3 Reflex::Role::Writable - Add non-blocking writable-watching behavior to a class.
295              
296             =head3 Reflex::Role::Writing - Add standard non-blocking syswrite() behavior to a class.
297              
298             =head3 Reflex::Stream - A non-blocking, buffered and translated I/O stream.
299              
300             =head3 Reflex::UdpPeer - A base class for non-blocking UDP networking peers.
301              
302             =head2 Signals and Child Processes
303              
304             Modules that provide signal support, including SIGCHLD for child
305             process management.
306              
307             =head3 Reflex::PID - A non-blocking SIGCHLD watcher for a specific process.
308              
309             =head3 Reflex::Role::PidCatcher - Add non-blocking SIGCHLD watching to a class.
310              
311             =head3 Reflex::Role::SigCatcher - Add non-blocking signal handling behavior to a class.
312              
313             =head3 Reflex::Signal - A non-blocking signal watcher.
314              
315             =head2 Timers
316              
317             Timer management has been relatively overlooked so far. We'll get to
318             it eventually, and you're welcome to help.
319              
320             =head3 Reflex::Interval - A non-blocking periodic interval timer.
321              
322             =head3 Reflex::Role::Interval - Add non-blocking periodic callbacks to a class.
323              
324             =head3 Reflex::Role::Timeout - Add non-blocking timeout timer behavior to a class.
325              
326             =head3 Reflex::Role::Wakeup - Add non-blocking wakeup alarm behavior to a class.
327              
328             =head3 Reflex::Timeout - A non-blocking single-shot delayed timer.
329              
330             =head3 Reflex::Wakeup - A non-blocking single-shot alarm for a specific time.
331              
332             =head2 Breadboarding Traits
333              
334             Reflex also implements signal/slot style object interaction, through
335             emit() and watch() methods. These traits were inspired by Smalltalk's
336             watchable object attributes.
337              
338             =head3 Reflex::Trait::EmitsOnChange - Cause a Moose attribute to emit() an event when it changes.
339              
340             =head3 Reflex::Trait::Observed - (Deprecated. See Reflex::Trait::Watched.)
341              
342             =head3 Reflex::Trait::Watched - Automatically watch a Reactive object stored in a Moose attribute.
343              
344             =head1 ASSISTANCE
345              
346             Thank you for volunteering to assist with this project. You can find
347             like-minded people in a few places, in descending order of preference.
348             Or, oh, wait, maybe you wanted assistance using it? We'll help you,
349             too. :)
350              
351             See irc.perl.org #reflex for help with Reflex.
352              
353             See irc.perl.org #poe for help with POE and Reflex.
354              
355             See irc.perl.org #moose for help with Moose.
356              
357             Support is officially available from POE's mailing list as well. Send
358             a blank message to
359             L<poe-subscribe@perl.org|mailto:poe-subscribe@perl.org>
360             to join.
361              
362             The Reflex package also has helpful examples which may serve as a
363             tutorial until Reflex is documented more.
364              
365             =head1 BUGS
366              
367             We appreciate your feedback, bug reports, feature requests, patches
368             and kudos. You may enter them into our request tracker by following
369             the instructions at
370             L<https://rt.cpan.org/Dist/Display.html?&Queue=Reflex>.
371              
372             We also accept e-mail at
373             L<bug-Reflex@rt.cpan.org|mailto:bug-Reflex@rt.cpan.org>.
374              
375             =head1 AUTHORS
376              
377             Rocco Caputo, RCAPUTO on CPAN.
378              
379             =head2 CONTRIBUTORS
380              
381             Reflex is open source, and we welcome involvement.
382              
383             Chris Fedde, CFEDDE on CPAN
384              
385             =over 2
386              
387             =item * L<https://github.com/rcaputo/reflex>
388              
389             =item * L<http://gitorious.org/reflex>
390              
391             =back
392              
393             =head1 TODO
394              
395             Please browse the source for the TODO marker. Some are visible in the
396             documentation, and others are sprinlked around in the code's comments.
397              
398             Also see L<docs/TODO.otl> in the distribution.
399             This is a Vim Outliner file with the current roadmap and progress.
400              
401             Set up Dist::Zilla to reduce technical debt and make releasing code
402             fun again.
403              
404             =head1 COPYRIGHT AND LICENSE
405              
406             Copyright 2009-2011 by Rocco Caputo.
407              
408             Reflex is free software. You may redistribute and/or modify it under
409             the same terms as Perl itself.
410              
411             =head1 SEE ALSO
412              
413             Please see those modules/websites for more information related to this module.
414              
415             =over 4
416              
417             =item *
418              
419             L<L<Moose>, L<POE>, the Reflex and Reflexive namespaces on CPAN.|L<Moose>, L<POE>, the Reflex and Reflexive namespaces on CPAN.>
420              
421             =item *
422              
423             L<Ohlo - L<https://www.ohloh.net/p/reflex-perl>|Ohlo - L<https://www.ohloh.net/p/reflex-perl>>
424              
425             =item *
426              
427             L<CIA - L<http://cia.vc/stats/project/reflex>|CIA - L<http://cia.vc/stats/project/reflex>>
428              
429             =back
430              
431             =head1 SUPPORT
432              
433             =head2 Perldoc
434              
435             You can find documentation for this module with the perldoc command.
436              
437             perldoc Reflex
438              
439             =head2 Websites
440              
441             The following websites have more information about this module, and may be of help to you. As always,
442             in addition to those websites please use your favorite search engine to discover more resources.
443              
444             =over 4
445              
446             =item *
447              
448             MetaCPAN
449              
450             A modern, open-source CPAN search engine, useful to view POD in HTML format.
451              
452             L<http://metacpan.org/release/Reflex>
453              
454             =item *
455              
456             Search CPAN
457              
458             The default CPAN search engine, useful to view POD in HTML format.
459              
460             L<http://search.cpan.org/dist/Reflex>
461              
462             =item *
463              
464             RT: CPAN's Bug Tracker
465              
466             The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
467              
468             L<https://rt.cpan.org/Public/Dist/Display.html?Name=Reflex>
469              
470             =item *
471              
472             AnnoCPAN
473              
474             The AnnoCPAN is a website that allows community annotations of Perl module documentation.
475              
476             L<http://annocpan.org/dist/Reflex>
477              
478             =item *
479              
480             CPAN Ratings
481              
482             The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
483              
484             L<http://cpanratings.perl.org/d/Reflex>
485              
486             =item *
487              
488             CPAN Forum
489              
490             The CPAN Forum is a web forum for discussing Perl modules.
491              
492             L<http://cpanforum.com/dist/Reflex>
493              
494             =item *
495              
496             CPANTS
497              
498             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
499              
500             L<http://cpants.cpanauthors.org/dist/Reflex>
501              
502             =item *
503              
504             CPAN Testers
505              
506             The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.
507              
508             L<http://www.cpantesters.org/distro/R/Reflex>
509              
510             =item *
511              
512             CPAN Testers Matrix
513              
514             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
515              
516             L<http://matrix.cpantesters.org/?dist=Reflex>
517              
518             =item *
519              
520             CPAN Testers Dependencies
521              
522             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
523              
524             L<http://deps.cpantesters.org/?module=Reflex>
525              
526             =back
527              
528             =head2 Email
529              
530             You can email the author of this module at C<poe-subscribe@perl.org> asking for help with any problems you have.
531              
532             =head2 Internet Relay Chat
533              
534             You can get live help by using IRC ( Internet Relay Chat ). If you don't know what IRC is,
535             please read this excellent guide: L<http://en.wikipedia.org/wiki/Internet_Relay_Chat>. Please
536             be courteous and patient when talking to us, as we might be busy or sleeping! You can join
537             those networks/channels and get help:
538              
539             =over 4
540              
541             =item *
542              
543             irc.perl.org
544              
545             You can connect to the server at 'irc.perl.org' and join this channel: #reflex to get help.
546              
547             =back
548              
549             =head2 Bugs / Feature Requests
550              
551             Please report any bugs or feature requests by email to C<bug-reflex at rt.cpan.org>, or through
552             the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Reflex>. You will be automatically notified of any
553             progress on the request by the system.
554              
555             =head2 Source Code
556              
557             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
558             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
559             from your repository :)
560              
561             git clone git://github.com/rcaputo/reflex.git
562              
563             =head1 BUGS AND LIMITATIONS
564              
565             You can make new bug reports, and view existing ones, through the
566             web interface at L<http://rt.cpan.org/Public/Dist/Display.html?Name=Reflex>.
567              
568             =head1 AUTHOR
569              
570             Rocco Caputo <rcaputo@cpan.org>
571              
572             =head1 ACKNOWLEDGEMENTS
573              
574             irc.perl.org channel
575             L<#moose|irc://irc.perl.org/moose>
576             and
577             L<#poe|irc://irc.perl.org/poe>.
578             The former for assisting in learning their fine libraries, sometimes
579             against everyone's better judgement. The latter for putting up with
580             lengthy and sometimes irrelevant design discussion for oh so long.
581              
582             =head1 COPYRIGHT AND LICENSE
583              
584             This software is copyright (c) 2017 by Rocco Caputo.
585              
586             This is free software; you can redistribute it and/or modify it under
587             the same terms as the Perl 5 programming language system itself.
588              
589             =head1 AVAILABILITY
590              
591             The latest version of this module is available from the Comprehensive Perl
592             Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
593             site near you, or see L<https://metacpan.org/module/Reflex/>.
594              
595             =head1 DISCLAIMER OF WARRANTY
596              
597             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
598             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
599             WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
600             PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
601             EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
602             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
603             PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
604             SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
605             THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
606              
607             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
608             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
609             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
610             TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
611             CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
612             SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
613             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
614             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
615             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
616             DAMAGES.
617              
618             =cut