Notification::Center - An observer/notification for perl
{ package Counter; use Moose; use Notification::Center; has count => ( is => 'rw', isa => 'Int', default => 0 ); sub inc { my ($self) = @_; $self->count( $self->count + 1 ); my $nc = Notification::Center->default; $nc->notify( { event => 'count', args => $self->count } ); } sub dec { my ($self) = @_; $self->count( $self->count - 1 ); my $nc = Notification::Center->default; $nc->notify( { event => 'count', args => $self->count } ); } no Moose; package TrackCount; use Moose; has count => ( is => 'rw', isa => 'Int', default => 0 ); sub print { my ($self) = @_; print $self->count; } sub get_count { my ( $self, $count ) = @_; $self->count($count); } no Moose; } my $count = Counter->new; my $mn = Notification::Center->default; my $tc = TrackCount->new; $mn->add( { observer => $tc, event => 'count', method => 'get_count', } ); for ( 1 .. 10 ) { $count->inc; } for ( 1 .. 5 ) { $count->dec; } $tc->print; # 5 or use IOC using Bread::Board use Bread::Board; my $c = container 'TestApp' => as { service 'fname' => 'Larry'; service 'lname' => 'Wall'; service 'notification_center' => ( class => 'Notification::Center', lifecycle => 'Singleton', ); service 'person' => ( class => 'Person', dependencies => { notification => depends_on('notification_center'), fname => depends_on('fname'), lname => depends_on('lname'), }, ); service 'upn' => ( class => 'UCPrintName', dependencies => { notification => depends_on('notification_center') }, ); service 'pn' => ( class => 'PrintName', dependencies => { notification => depends_on('notification_center'), }, ); }; my $pn = $c->fetch('pn')->get; my $upn = $c->fetch('upn')->get; my $person = $c->fetch('person')->get; my $nc = $c->fetch('notification_center')->get; $person->print_name; $nc->remove( { observer => $upn } ); $person->print_name;
An observer/notification center based on the objective-c NSNotificationCenter Class
The method creates a new instance of Notification::Center object
This method creates a singleton of the Notification::Center object
args keys: observer, event, method
observer: the object that will observer events
event: the name of the event that you are assigning the observer. Defaults to DEFAULT
method: the method you want called on the observer when the event is called. Defaults to update
args keys: observer, event
observer: the object that you want to remove
event: the name of the event that you are removing the observer. Defaults to DEFAULT
args keys : event, args
event: the event you want to trigger
args: data you want to pass into observers
http://github.com/rlb3/notification-center/tree/master
To install Notification::Center, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Notification::Center
CPAN shell
perl -MCPAN -e shell install Notification::Center
For more information on module installation, please visit the detailed CPAN module installation guide.