VERSION

Version 1.23

SYNOPSIS

   package foo;
   use Object::Event;

   our @ISA = qw/Object::Event/;

   package main;
   my $o = foo->new;

   my $regguard = $o->reg_cb (foo => sub {
      print "I got an event, with these args: $_[1], $_[2], $_[3]\n";
   });

   $o->event (foo => 1, 2, 3);

   $o->unreg_cb ($regguard);
   # or just:
   $regguard = undef;

DESCRIPTION

This module was mainly written for AnyEvent::XMPP, AnyEvent::IRC, AnyEvent::HTTPD and \s-1BK\s0 to provide a consistent \s-1API\s0 for registering and emitting events. Even though I originally wrote it for those modules I released it separately in case anyone may find this module useful.

For more comprehensive event handling see also Glib and \s-1POE\s0.

This class provides a simple way to extend a class, by inheriting from this class, with an event callback interface.

You will be able to register callbacks for events, identified by their names (a string) and call them later by invoking the \*(C`event\*(C' method with the event name and some arguments.

There is even a syntactic sugar which allows one to call methods on the instances of Object::Event-derived classes, to invoke events. For this feature see the \*(L"\s-1EVENT\s0 \s-1METHODS\s0\*(R" section of this document.

PERFORMANCE

In the first version as presented here no special performance optimisations have been applied. So take care that it is fast enough for your purposes. At least for modules like AnyEvent::XMPP the overhead is probably not noticeable, as other technologies like \s-1XML\s0 already waste a lot more \s-1CPU\s0 cycles. Also I/O usually introduces _much_ larger/longer overheads than this simple event interface.

FUNCTIONS

This package function will add a global priority alias. If $priority is undef the alias will be removed. There are 4 predefined aliases: before => 1000 ext_before => 500 ext_after => -500 after => -1000 See also the \*(C`reg_cb\*(C' method for more information about aliases.

METHODS

Object::Event->new (%args)
Your::Subclass::Of::Object::Event->new (%args)

This is the constructor for Object::Event, it will create a blessed hash reference initialized with %args. This method should only be called if you are not able to call the \*(C`new\*(C' constructor of this class. Then you need to call this method to initialize the event system. This method installs a callback that will be called when some other event callback threw an exception. The first argument to $cb will be the exception and the second the event name. This method registers a callback $cb1 for the event with the name $eventname1. You can also pass multiple of these eventname => callback pairs. The return value $guard will be a guard that represents the set of callbacks you have installed. You can either just \*(L"forget\*(R" the contents of $guard to unregister the callbacks or call \*(C`unreg_cb\*(C' with that \s-1ID\s0 to remove those callbacks again. If \*(C`reg_cb\*(C' is called in a void context no guard is returned and you have no chance to unregister the registered callbacks. The first argument for callbacks registered with the \*(C`reg_cb\*(C' function will always be the master object $obj. The return value of the callbacks are ignored. If you need to pass any information from a handler to the caller of the event you have to establish your own \*(L"protocol\*(R" to do this. I recommend to pass an array reference to the handlers: $obj->reg_cb (event_foobar => sub { my ($self, $results) = @_; push @$results, time / 30; });

my @results; $obj->event (event_foobar => \@results); for (@results) { # ... } The order of the callbacks in the call chain of the event depends on their priority. If you didn't specify any priority (see below) they get the default priority of 0, and are appended to the other priority 0 callbacks. The higher the priority number, the earlier the callbacks gets called in the chain. If $eventname1 starts with 'before_' the callback gets a priority of 1000, and if it starts with 'ext_before_' it gets the priority 500. 'after_' is mapped to the priority -1000 and 'ext_after_' to -500. If you want more fine grained control you can pass an array reference instead of the event name: ($eventname1, $prio) = ('test_abc', 100); $obj->reg_cb ([$eventname1, $prio] => sub { ... }); Removes the callback $cb from the set of registered callbacks. Emits the event $eventname and passes the arguments @args to the callbacks. The return value $handled is a true value in case some handler was found and run. It returns false if no handler was found (see also the \*(C`handles\*(C' method below). Basically: It returns the same value as the \*(C`handles\*(C' method. Please note that an event can be stopped and reinvoked while it is being handled. See also the specification of the before and after events in \*(C`reg_cb\*(C' above. \s-1NOTE:\s0 Whenever an event is emitted the current set of callbacks registered to that event will be used. So, if you register another event callback for the same event that is executed at the moment, it will be called the next time when the event is emitted. Example: $obj->reg_cb (event_test => sub { my ($obj) = @_;

print "Test1\n"; $obj->unreg_me;

$obj->reg_cb (event_test => sub { my ($obj) = @_; print "Test2\n"; $obj->unreg_me; }); });

$obj->event ('event_test'); # prints "Test1" $obj->event ('event_test'); # prints "Test2" This method returns true if any event handler has been setup for the event $eventname. It returns false if that is not the case. Returns the name of the currently executed event. Unregisters the currently executed callback. This method stops the execution of callbacks of the current event, and returns (in non-void context) a callback that will let you continue the execution. \s-1DEPRECATED:\s0 Don't use it! Just for backward compatibility for AnyEvent::XMPP version 0.4. \s-1DEPRECATED:\s0 Don't use it! Just for backward compatibility for AnyEvent::XMPP version 0.4. This method removes all registered event callbacks from this object. This method returns a string dump of all registered event callbacks. This method is only for debugging purposes.

EVENT METHODS

You can define static methods in a package that act as event handler. This is done by using Perl's attributes functionality. To make a method act as event handler you need to add the \*(C`event_cb\*(C' attribute to it.

\s-1NOTE:\s0 Please note that for this to work the methods need to be defined at compile time. This means that you are not able to add event handles using \*(C`AUTOLOAD\*(C'!

\s-1NOTE:\s0 Perl's attributes have a very basic syntax, you have to take care to not insert any whitespace, the attribute must be a single string that contains no whitespace. That means: \*(C`event_cb (1)\*(C' is not the same as event_cb(1)!

Here is an example:

package foo; use base qw/Object::Event/;

sub test : event_cb { print "test event handler!\n" }

package main; my $o = foo->new; $o->test (); # prints 'test event handler!' $o->event ('test'); # also prints 'test event handler!'!

In case you want to set a priority use this syntax:

sub test : event_cb(-1000) { ... }

Or:

sub test : event_cb(after) { ... }

You may want to have a look at the tests of the Object::Event distribution for more examples.

\s-1ALIASES\s0

If you want to define multiple event handlers as package method you can use the \*(C`event_cb\*(C' attribute with an additional argument:

package foo; use base qw/Object::Event/;

sub test : event_cb { # default prio is always 0 print "middle\n"; }

sub test_last : event_cb(-1,test) { print "after\n"; }

sub test_first : event_cb(1,test) { print "before\n"; }

package main; my $o = foo->new; $o->test (); # prints "after\n" "middle\n" "before\n" $o->event ('test'); # prints the same $o->test_first (); # also prints the same

\s-1NOTE:\s0 Please note that if you don't provide any order the methods are sorted alphabetically:

package foo; use base qw/Object::Event/;

sub test : event_cb { # default prio is always 0 print "middle\n"; }

sub x : event_cb(, test) { # please note the empty element before the ','! print "after\n"; }

sub a : event_cb(, test) { print "before\n"; }

package main; my $o = foo->new; $o->test (); # prints "after\n" "middle\n" "before\n" $o->event ('test'); # prints the same $o->x (); # also prints the same

\s-1ALIAS\s0 \s-1ORDERING\s0

The ordering of how the methods event handlers are called if they are all defined for the same event is strictly defined:

1.

Ordering of the methods for the same event in the inheritance hierarchy is always dominated by the priority of the event callback.

2.

Then if there are multiple methods with the same priority the place in the inheritance hierarchy defines in which order the methods are executed. The higher up in the hierarchy the class is, the earlier it will be called.

3.

Inside a class the name of the method for the event decides which event is executed first. (All if the priorities are the same)

DEBUGGING

There exists a package global variable called $DEBUG that control debugging capabilities.

Set it to 1 to produce a slightly extended \*(C`events_as_string_dump\*(C' output.

Set it to 2 and all events will be dumped in a tree of event invocations.

You can set the variable either in your main program:

$Object::Event::DEBUG = 2;

Or use the environment variable \*(C`PERL_OBJECT_EVENT_DEBUG\*(C':

export PERL_OBJECT_EVENT_DEBUG=2

AUTHOR

Robin Redeker, \*(C`<elmex at ta-sa.org>\*(C', \s-1JID:\s0 \*(C`<elmex at jabber.org>\*(C'

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Object::Event

You can also look for information at:

  • AnnoCPAN: Annotated \s-1CPAN\s0 documentation http://annocpan.org/dist/Object-Event <http://annocpan.org/dist/Object-Event>

  • \s-1CPAN\s0 Ratings http://cpanratings.perl.org/d/Object-Event <http://cpanratings.perl.org/d/Object-Event>

  • \s-1RT:\s0 \s-1CPAN\s0's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=Object-Event <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Object-Event>

  • Search \s-1CPAN\s0 http://search.cpan.org/dist/Object-Event <http://search.cpan.org/dist/Object-Event>

ACKNOWLEDGEMENTS

Thanks go to:

- Mons Anderson for suggesting the 'handles' method and the return value of the 'event' method and reporting bugs.

COPYRIGHT & LICENSE

Copyright 2009-2011 Robin Redeker, all rights reserved.

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