Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > eb17b85b26594a75306845fce2963a49 > files > 4

perl-Class-Observable-1.40.0-3.mga7.noarch.rpm

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->_on_update( $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>