Reflex::Role::Readable - add readable-watching behavior to a class
version 0.050
use Moose; has socket => ( is => 'rw', isa => 'FileHandle', required => 1 ); with 'Reflex::Role::Readable' => { handle => 'socket', cb_ready => 'on_socket_readable', active => 1, }; sub on_socket_readable { my ($self, $arg) = @_; print "Data is ready on socket $arg->{handle}.\n"; $self->pause_socket_readabe(); }
Reflex::Role::Readable is a Moose parameterized role that adds readable-watching behavior for Reflex-based classes. In the SYNOPSIS, a filehandle named "socket" is watched for readability. The method on_socket_readable() is called when data becomes available.
TODO - Explain the difference between role-based and object-based composition.
The handle parameter must contain the name of the attribute that holds the handle to watch. The name indirection allows the role to generate unique methods by default. For example, a handle named "XYZ" would generates these methods by default:
handle
cb_ready => "on_XYZ_readable", method_pause => "pause_XYZ_readable", method_resume => "resume_XYZ_readable", method_stop => "stop_XYZ_readable",
This naming convention allows the role to be used for more than one handle in the same class. Each handle will have its own name, and the mixed in methods associated with them will also be unique.
active specifies whether the Reflex::Role::Readable watcher should be enabled when it's initialized. All Reflex watchers are enabled by default. Set it to a false value, preferably 0, to initialize the watcher in an inactive or paused mode.
active
Readability watchers may be paused and resumed. See method_pause and method_resume for ways to override the default method names.
method_pause
method_resume
cb_ready names the $self method that will be called whenever handle has data to be read. By default, it's the catenation of "on_", the handle name, and "_readable". A handle named "XYZ" will by default trigger on_XYZ_readable() callbacks.
cb_ready
handle => "socket", # on_socket_readable() handle => "XYZ", # on_XYZ_readable()
All Reflex parameterized role callbacks are invoked with two parameters: $self and an anonymous hashref of named values specific to the callback. cb_ready callbacks include a single named value, handle, that contains the filehandle from which has become ready for reading.
handle is the handle itself, not the handle attribute's name.
method_pause sets the name of the method that may be used to pause the watcher. It is "pause_${handle}_readable" by default.
method_resume may be used to resume paused readability watchers, or to activate them if they are started in an inactive state.
method_stop may be used to stop readability watchers. These watchers may not be restarted once they've been stopped. If you want to pause and resume watching, see method_pause and method_resume.
method_stop
TODO - I'm sure there are some.
Reflex Reflex::Role::Writable Reflex::Role::Streaming
"ACKNOWLEDGEMENTS" in Reflex "ASSISTANCE" in Reflex "AUTHORS" in Reflex "BUGS" in Reflex "BUGS" in Reflex "CONTRIBUTORS" in Reflex "COPYRIGHT" in Reflex "LICENSE" in Reflex "TODO" in Reflex
To install Reflex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Reflex
CPAN shell
perl -MCPAN -e shell install Reflex
For more information on module installation, please visit the detailed CPAN module installation guide.