
MooseX::Observer::Role::Observable - Adds methods an logic to a class, enabling instances changes to be observed

version 0.010

############################################################################
package Counter;
use Moose;
has count => (
traits => ['Counter'],
is => 'rw',
isa => 'Int',
default => 0,
handles => {
inc_counter => 'inc',
dec_counter => 'dec',
},
);
# apply the observable-role and
# provide methodnames, after which the observers are notified of changes
with 'MooseX::Observer::Role::Observable' => { notify_after => [qw~
count
inc_counter
dec_counter
reset_counter
~] };
sub reset_counter { shift->count(0) }
sub _utility_method { ... }
############################################################################
package Display;
use Moose;
# apply the oberserver-role, tagging the class as observer and ...
with 'MooseX::Observer::Role::Observer';
# ... require an update-method to be implemented
# this is called after the observed subject calls an observed method
sub update {
my ( $self, $subject, $args, $eventname ) = @_;
print $subject->count;
}
############################################################################
package main;
my $counter = Counter->new();
# add an observer of type "Display" to our observable counter
$counter->add_observer( Display->new() );
# increments the counter to 1, afterwards its observers are notified of changes
# Display is notified of a change, its update-method is called
$counter->inc_counter; # Display prints 1
$counter->dec_counter; # Display prints 0

This is a parameterized role, that is applied to your observed class. Usually when applying this role, you provide a list of methodnames. After method modifiers are installed for these methods. They call the _notify-method, which in turn calls the update-method of all observers.

Adds an observer to the object. This Observer must do the MooseX::Observer::Role::Observer role.
Returns how many observers are attached to the object.
Returns a list of all observers attached to the object.
Remove the given observer from the object.
Removes all observers from the object.
This private method notifies all observers, passing $self, $args and an $eventname to the observers' update method.

See perlmodinstall for information and options on installing Perl modules.

Please see those modules/websites for more information related to this module.

Thomas Müller <tmueller@cpan.org>

This software is copyright (c) 2011 by Thomas Müller.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.