Class::Observable - Base class for observable classes/objects

# Define an observable class

package My::Object;

use base qw( Class::Observable );

# Tell all classes/objects observing this object that a state-change # has occurred

sub create {

     my ( $self ) = @;
     eval { $self->perform_save() };
     if ( $@ ) {
         My::Exception->throw( "Error saving: $@" );
     }
     $self->notify_observers( 'create' );

}

# Define an observer

package My::Observer;

sub update {

     my ( $class, $object, $action ) = @;
     unless ( $action ) {
         warn "Cannot operation on [", $object->id, "] without action";
         return;
     }
     $class->on_create( $object ) if ( $action eq 'create' );
     $class->onupdate( $object ) if ( $action eq 'update' );

}

# Register the observer class with all instances of the observable # class

My::Object->add_observer( 'My::Observer' );

# Register the observer class with a single instance of the # observable class

my $object = My::Object->new( 'foo' ); $object->add_observer( 'My::Observer' );

# Register an observer object the same way

my $observer = My::Observer->new( 'bar' ); My::Object->add_observer( $observer ); my $object = My::Object->new( 'foo' ); $object->add_observer( $observer );

# Register an observer using a subroutine

sub catch_observation { ... }

My::Object->add_observer( \&catch_observation ); my $object = My::Object->new( 'foo' ); $object->add_observer( \&catch_observation );

# Delete a single observer; delete all observers

My::Object->delete_observer( $observer ); My::Object->delete_observers();

# More... see the docs

INSTALLATION

To install this module perform the typical four-part Perl salute:

perl Makefile.PL
make
make test
make install

DEPENDENCIES

This module requires these other modules and libraries:

Class::ISA
Scalar::Util

COPYRIGHT AND LICENCE

Copyright (c) 2002-2004 Chris Winters. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Chris Winters <chris@cwinters.com>