This is an outdated 20-minute overview of Reflex, a modern Perl library for building asynchronous, eventy classes and programs. Please see the extended dance remix I presented at YAPC::NA 2011 instead.
17. Dynamic Composition
• Objects are created,
• related,
• communicated (with),
• and destructicated
• at run time.
• Lather, rinse, repeat until the program ends.
18. Static Composition
• Code is bolted together before running.
• It stays that way “forever”.
• Subclassing.
• Moose roles.
23. Ye Olde Coderef
• Quick and dead simple to use.
• Closure tricks for convenience and speed.
• Closures can’t use OO to extend or
override event handlers.
• Uses circular references—memory leakage
if you’re not careful.
25. Methods
has ticker => (
isa => 'Reflex::Interval', is => 'rw' );
sub BUILD {
my $self = shift;
$self->ticker(
Reflex::Interval->new(
interval => 1,
on_tick => cb_method($self, "callback"),
)
);
}
sub callback { print "tick...n" }
26. Methods
has ticker => (
isa => 'Reflex::Interval', is => 'rw' );
sub BUILD {
my $self = shift;
$self->ticker(
Reflex::Interval->new(
interval => 1,
on_tick => cb_method($self, "callback"),
)
);
}
sub callback { print "tick...n" }
27. Methods
has ticker => (
isa => 'Reflex::Interval', is => 'rw' );
sub BUILD {
my $self = shift;
$self->ticker(
Reflex::Interval->new(
interval => 1,
on_tick => cb_method($self, "callback"),
)
);
}
sub callback { print "tick...n" }
28. Methods
• Cleaner object oriented design.
• Callbacks can use every Moose trick in the
book.
• Requires more forethought.
• Perl & Moose OO less performant than
anonymous subroutines & closures.
42. What’s a Promise?
“an object that acts as a proxy
for a result that is initially not
known, usually because the
computation of its value has
not yet completed.”
— Wikipedia
44. What’s a Promise?
• Asynchronous object.
• Create it.
• Do other stuff while it runs.
• Pick up the result later.
• Blocks or returns “incomplete” if not done.
• Other stuff runs while it blocks.
45. Timer Promise (1 of 2)
use Reflex::Interval;
my $one = Reflex::Interval->new(
interval => 1
);
my $two = Reflex::Interval->new(
interval => 2
);
57. Reflex::Interval (1 of 3)
package Reflex::Interval;
use Moose; extends 'Reflex::Base';
has interval => (
isa => 'Num', is => 'ro'
);
has auto_repeat => (
isa => 'Bool', is => 'ro', default => 1
);
has auto_start => (
isa => 'Bool', is => 'ro', default => 1
);
61. Reflex Roles
• Reflex eventiness implemented with roles.
• Timers, I/O, signals, etc.
• Roles are reified by simple classes.
• Reflex::Interval is Reflex::Role::Interval.
• Larger roles comprise simpler ones.
• It’s all Mooses, all the way down.
65. Smart Defaults
• A primary attribute identifies the role.
• Default parameters are named after that
attribute.
• Roles avoid conflicts by default.
• Without tediously supplying all parameters.
66. Key Attribute
with 'Reflex::Role::Interval' => {
interval => "watchdog",
}
Role Parameter Default Name
method_start start_watchdog()
method_stop stop_watchdog()
cb_tick on_watchdog_tick()
ev_tick watchdog_tick
67. Key Attribute
with 'Reflex::Role::Interval' => {
interval => "logmark",
}
Role Parameter Default Name
method_start start_logmark()
method_stop stop_logmark()
cb_tick on_logmark_tick()
ev_tick logmark_tick
78. Reflex is Newish
• Large swathes of design are stable.
• Details need to be ironed out.
• Will happen faster if you use it.
• Help me find the edge cases.
79. How To Help
• http://github.com/rcaputo/reflex
• See docs/TODO.otl
• #reflex on irc.perl.org
• poe-subscribe@perl.org
• Hackathon?
• Hire someone to use it on your project.
80. Contribute to a Project
• Nick Perez’s Reflex-based psgi server.
• Reflexive::Stream::Filtering
• Attach POE filters to Reflex streams.
• Reflexive::Role::TCPServer
• Consumable full-featured TCP server.
• (Your Project Here)