File Coverage

blib/lib/Reflex.pm
Criterion Covered Total %
statement 11 19 57.8
branch 1 4 25.0
condition n/a
subroutine 4 5 80.0
pod 1 1 100.0
total 17 29 58.6


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