GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  EVENT::NOTIFY (3)

.ds Aq ’

NAME

Event::Notify - Simple Observer/Notifier

CONTENTS

SYNOPSIS



  use Event::Notify;

  my $notify = Event::Notify->new;
  $notify->register( $observer );
  $notify->register_event( $event, $observer );
  $notify->notify( $event, @args );



DESCRIPTION

Event::Notify implements a simple Observer pattern. It’s not really intended to be subclassed, or a fancy system. It just registers observers, and broadcasts events, that’s it. The simplicity is that it can be embedded in a class that doesn’t necessarily want to be a subclass of a notifier.

Simply create a slot for it, and delegate methods to it:



  package MyClass;
  use Event::Notify;

  sub new {
    my $class = shift;
    my $self = shift;
    $self->{notify} = Event::Notify->new;
  }

  # This interface doesnt have to be this way. Here, were just making
  # a simple delegation mechanism
  sub register_event { shift->{notify}->register_event(@_) }
  sub unregister_event { shift->{notify}->unregister_event(@_) }
  sub notify { shift->{notify}->notify(@_) }



Voila, you got yourself a observable module without inheritance!

METHODS

    new

Creates a new instance

    register($observer)

Registers a new observer. The observer must implement a notify() method.

When called, the observer’s register() method is invoked, so each observer can register itself to whatever event the observer wants to subscribe to.

So your observer’s register() method could do something like this:



  package MyObserver;
  sub register {
    my ($observer, $notify) = @_;
    $notify->register_event( event_name1, $observer );
    $notify->register_event( event_name2, $observer );
    $notify->register_event( event_name3, $observer );
    $notify->register_event( event_name4, $observer );
  }



Think of it as sort of an automatic initializer.

    register_event($event,$observer[,\%opts])

Registers an observer $observer as observing a particular event $event The $observer can be either an object or a subroutine reference.

In case $observer is an object, the object must implement a method named notify(), or the method name specified the method parameter in the optional third parameter %opts

Calling



  $notify->register_event($event, $observer);



is the same as saying



  $notify->register_event($event, $observer, { method => notify });



If the object does not implement the named method (or notify(), if you don’t specify one), then it will croak

    unregister_event($event,$observer)

Unregisters an observer.

    notify($event,@args)

Notifies all of the observers about a particular event. @args is passed directly to the observers’ notify() event

clear_observers()

Clears all observers from this object.

AUTHOR

Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>

LICENSE

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

See http://www.perl.com/perl/misc/Artistic.html

Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 EVENT::NOTIFY (3) 2007-11-29

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.