pax_global_header00006660000000000000000000000064116164567760014535gustar00rootroot0000000000000052 comment=ceac15e859ab348754c651011c4afd67c9637aa8 perl-Object-Event-1.23/000075500000000000000000000000001161645677600147055ustar00rootroot00000000000000perl-Object-Event-1.23/.gitignore000064400000000000000000000002001161645677600166650ustar00rootroot00000000000000blib* Makefile Makefile.old Build _build* pm_to_blib* *.tar.gz .lwpcookies Object-Event-* cover_db *.swp t/methds/meth_* README perl-Object-Event-1.23/Changes000064400000000000000000000054711161645677600162070ustar00rootroot00000000000000Revision history for Object-Event: 1.23 Thu Aug 4 10:52:32 CEST 2011 - added small safety-check to unreg_cb. 1.22 Thu Mar 10 17:17:58 CET 2011 - fixed a bug where event methods were not registered correctly. - found a bug with using the guard to track cb registrations. 1.21 Thu Nov 5 19:37:58 CET 2009 - fixed a bug in the legacy forward code to support the old AnyEvent::XMPP API. 1.2 Thu Nov 5 00:01:47 CET 2009 - removed hand_event_methods_down and inherit_event_methods_from in favour of the event_cb attribute, which made inheriting event method much easier. - added event_cb alias attribute parameter to be able to specify multiple package method event handlers for an object. - added some debugging functionality. 1.101 Fri Sep 4 14:24:58 CEST 2009 - fixed stupid bug in event method, returning always false (added test for that now). - using common::sense now. 1.1 Sun Aug 9 18:05:08 CEST 2009 - fixed some really wrong documentation (i.e.: about return values of the event method). - added init method for cases where you can't use the Object::Event constructor. - testing for undefined $self in the guard destructor. - added 'handles' method to query whether handlers exist. - made the 'event' method return true or false in case handlers ran. 1.0 Mon Mar 16 21:23:02 CET 2009 - added stop/continue feature. - deprecated forward feature. (will be kept until AnyEvent::XMPP 0.4 is replaced by the new AnyEvent::XMPP, in around 1 year probably.) - added support for arbitrary priorities. - added syntactic sugar for using method calling syntax as event invocation. 0.7 Wed Feb 18 11:56:40 CET 2009 NOTE: This is the last release which will contain the add_forward feature. The next release will contain some minor incompatible changes. - added unreg_my_set method. - fixed bug in _while_referenced implementation and added tests (by Pedro Melo) - some more tests. - added ::Methods syntactic sugar. Please note that the next version might contain some incompatible changes here. 0.6 Tue Sep 23 15:13:23 CEST 2008 - recursive event calling now also should work properly. 0.4 Tue Apr 15 12:48:11 CEST 2008 - events can now be registered from within the callbacks for the same object and event safely. 0.3 Fri Mar 21 11:35:03 CET 2008 - fixed a serious bug in stop_event 0.2 Fri Mar 21 01:38:12 CET 2008 - forgot a simple test for the event handling - added an example 0.1 Thu Mar 20 13:18:12 CET 2008 - initial release perl-Object-Event-1.23/MANIFEST000064400000000000000000000007371161645677600160450ustar00rootroot00000000000000Changes MANIFEST Makefile.PL README lib/Object/Event.pm t/00-load.t t/01_simple_events.t t/02_stop_event.t t/03_event_within.t t/04_recursive.t t/05_unreg_rec.t t/08_priorities.t t/06_unreg_guard.t t/07_event_name.t t/10_continue.t t/12_prio.t t/13_methods.t t/14_methods2.t t/15_methods_subc.t t/16_event.t t/17_methods_alias.t t/18_method_inherit.t t/18_method_inherit_2.t t/19_method_exept.t t/20_forward_legacy.t samples/simple_example samples/benchmark samples/mass_example perl-Object-Event-1.23/Makefile.PL000064400000000000000000000014151161645677600166600ustar00rootroot00000000000000use strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Object::Event', AUTHOR => 'Robin Redeker ', LICENSE => 'perl', VERSION_FROM => 'lib/Object/Event.pm', ABSTRACT_FROM => 'lib/Object/Event.pm', PL_FILES => {}, test => { TESTS => "t/*.t t/methds/*.t" }, PREREQ_PM => { 'Test::More' => 0, 'AnyEvent' => 3.5, 'common::sense' => 0, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', PREOP => 'pod2text lib/Object/Event.pm | tee README >$(DISTVNAME)/README; chmod -R u=rwX,go=rX . ;', }, clean => { FILES => 'Object-Event-*' }, ); perl-Object-Event-1.23/TODO000064400000000000000000000000001161645677600153630ustar00rootroot00000000000000perl-Object-Event-1.23/lib/000075500000000000000000000000001161645677600154535ustar00rootroot00000000000000perl-Object-Event-1.23/lib/Object/000075500000000000000000000000001161645677600166615ustar00rootroot00000000000000perl-Object-Event-1.23/lib/Object/Event.pm000064400000000000000000000556041161645677600203120ustar00rootroot00000000000000package Object::Event; use common::sense; use Carp qw/croak/; use AnyEvent::Util qw/guard/; use sort 'stable'; our $DEBUG = $ENV{PERL_OBJECT_EVENT_DEBUG}; =head1 NAME Object::Event - A class that provides an event callback interface =head1 VERSION Version 1.23 =cut our $VERSION = '1.23'; =head1 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; =head1 DESCRIPTION This module was mainly written for L, L, L and L to provide a consistent API 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 L and L. 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 method with the event name and some arguments. There is even a syntactic sugar which allows to call methods on the instances of L-derived classes, to invoke events. For this feature see the L section of this document. =head1 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 L the overhead is probably not noticeable, as other technologies like XML already waste a lot more CPU cycles. Also I/O usually introduces _much_ larger/longer overheads than this simple event interface. =head1 FUNCTIONS =over 4 =item Object::Event::register_priority_alias ($alias, $priority) This package function will add a global priority alias. If C<$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 method for more information about aliases. =cut our %PRIO_MAP = ( before => 1000, ext_before => 500, ext_after => -500, after => -1000 ); sub register_priority_alias { my ($alias, $prio) = @_; $PRIO_MAP{$alias} = $prio; unless (defined $PRIO_MAP{$alias}) { delete $PRIO_MAP{$alias} } } =back =head1 METHODS =over 4 =item Object::Event->new (%args) =item Your::Subclass::Of::Object::Event->new (%args) This is the constructor for L, it will create a blessed hash reference initialized with C<%args>. =cut sub new { my $this = shift; my $class = ref ($this) || $this; my $self = { @_ }; bless $self, $class; $self->init_object_events; return $self } =item $obj->init_object_events () This method should only be called if you are not able to call the C constructor of this class. Then you need to call this method to initialize the event system. =cut sub init_object_events { my ($self) = @_; my $pkg = ref $self; _init_methods ($pkg) unless *{"$pkg\::__OE_METHODS"}{HASH}; $self->{__oe_cb_gen} = "a"; # generation counter $self->{__oe_events} = { map { ($_ => [@{${"$pkg\::__OE_METHODS"}{$_}}]) } keys %{"$pkg\::__OE_METHODS"} }; } =item $obj->set_exception_cb ($cb->($exception, $eventname)) This method installs a callback that will be called when some other event callback threw an exception. The first argument to C<$cb> will be the exception and the second the event name. =cut sub set_exception_cb { my ($self, $cb) = @_; $self->{__oe_exception_cb} = $cb; } =item $guard = $obj->reg_cb ($eventname => $cb->($obj, @args), ...) =item $guard = $obj->reg_cb ($eventname => $prio, $cb->($obj, @args), ...) This method registers a callback C<$cb1> for the event with the name C<$eventname1>. You can also pass multiple of these eventname => callback pairs. The return value C<$guard> will be a guard that represents the set of callbacks you have installed. You can either just "forget" the contents of C<$guard> to unregister the callbacks or call C with that ID to remove those callbacks again. If 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 function will always be the master object C<$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 "protocol" 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 C<$eventname1> starts with C<'before_'> the callback gets a priority of 1000, and if it starts with C<'ext_before_'> it gets the priority 500. C<'after_'> is mapped to the priority -1000 and C<'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 { ... }); =cut our @DEBUG_STACK; sub _debug_cb { my ($callback) = @_; sub { my @a = @_; my $dbcb = $_[0]->{__oe_cbs}->[0]->[0]; my $nam = $_[0]->{__oe_cbs}->[2]; push @DEBUG_STACK, $dbcb; my $pad = " " x scalar @DEBUG_STACK; printf "%s-> %s\n", $pad, $dbcb->[3]; eval { $callback->(@a) }; my $e = $@; printf "%s<- %s\n", $pad, $dbcb->[3]; pop @DEBUG_STACK; die $e if $e; () }; } sub _print_event_debug { my ($ev) = @_; my $pad = " " x scalar @DEBUG_STACK; my ($pkg, $file, $line) = caller (1); for my $path (@INC) { last if $file =~ s/^\Q$path\E\/?//; } printf "%s!! %s @ %s:%d (%s::)\n", $pad, $ev, $file, $line, $pkg } sub _register_event_struct { my ($self, $event, $prio, $gen, $callback, $debug) = @_; my $reg = ($self->{__oe_events} ||= {}); my $idx = 0; $reg->{$event} ||= []; my $evlist = $reg->{$event}; for my $ev (@$evlist) { last if $ev->[0] < $prio; $idx++; } my $cb = $callback; $cb = _debug_cb ($callback) if $DEBUG > 1; splice @$evlist, $idx, 0, [$prio, "$callback|$gen", undef, $debug, $cb]; } sub reg_cb { my ($self, @args) = @_; my $debuginfo = caller; if ($DEBUG > 0) { my ($pkg,$file,$line) = caller; for my $path (@INC) { last if $file =~ s/^\Q$path\E\/?//; } $debuginfo = sprintf "%s:%d (%s::)", $file, $line, $pkg; } my $gen = $self->{__oe_cb_gen}++; # get gen counter my @cbs; while (@args) { my ($ev, $sec) = (shift @args, shift @args); my ($prio, $cb) = (0, undef); if (ref $sec) { for my $prefix (keys %PRIO_MAP) { if ($ev =~ s/^(\Q$prefix\E)_//) { $prio = $PRIO_MAP{$prefix}; last; } } $cb = $sec; } else { $prio = $sec; $cb = shift @args; } $self->_register_event_struct ($ev, $prio, $gen, $cb, $debuginfo); push @cbs, $cb; } defined wantarray ? \(my $g = guard { if ($self) { $self->unreg_cb ($_, $gen) for @cbs } }) : () } =item $obj->unreg_cb ($cb) Removes the callback C<$cb> from the set of registered callbacks. =cut sub unreg_cb { my ($self, $cb, $gen) = @_; if (ref ($cb) eq 'REF') { # we've got a guard object $$cb = undef; return; } return unless defined $cb; # some small safety against bad arguments my $evs = $self->{__oe_events}; # $gen is neccessary for the times where we use the guard to remove # something, because we only have the callback as ID we need to track the # generation of the registration for these: # # my $cb = sub { ... }; # my $g = $o->reg_cb (a => $cb); # $g = $o->reg_cb (a => $cb); my ($key, $key_len) = defined $gen ? ("$cb|$gen", length "$cb|$gen") : ("$cb", length "$cb"); for my $reg (values %$evs) { @$reg = grep { (substr $_->[1], 0, $key_len) ne $key } @$reg; } } =item my $handled = $obj->event ($eventname, @args) Emits the event C<$eventname> and passes the arguments C<@args> to the callbacks. The return value C<$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 method below). Basically: It returns the same value as the 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 above. NOTE: 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 B 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" =cut sub event { my ($self, $ev, @arg) = @_; my @cbs; if (ref ($ev) eq 'ARRAY') { @cbs = @$ev; } else { my $evs = $self->{__oe_events}->{$ev} || []; @cbs = @$evs; } ###################### # Legacy code start ###################### if ($self->{__oe_forwards}) { # we are inserting a forward callback into the callchain. # first search the start of the 0 priorities... my $idx = 0; for my $ev (@cbs) { last if $ev->[0] <= 0; $idx++; } # then splice in the stuff my $cb = sub { for my $fw (keys %{$self->{__oe_forwards}}) { my $f = $self->{__oe_forwards}->{$fw}; local $f->[0]->{__oe_forward_stop} = 0; eval { $f->[1]->($self, $f->[0], $ev, @arg); }; if ($@) { if ($self->{__oe_exception_cb}) { $self->{__oe_exception_cb}->($@, $ev); } else { warn "unhandled callback exception on forward event " . "($ev, $self, $f->[0], @arg): $@\n"; } } elsif ($f->[0]->{__oe_forward_stop}) { $self->stop_event; } } }; splice @cbs, $idx, 0, [0, "$cb", undef, undef, $cb]; } ###################### # Legacy code end ###################### _print_event_debug ($ev) if $DEBUG > 1; return unless @cbs; local $self->{__oe_cbs} = [\@cbs, \@arg, $ev]; eval { $cbs[0]->[4]->($self, @arg), shift @cbs while @cbs; () }; if ($@) { if (not ($self->{__oe_exception_rec}) && $self->{__oe_exception_cb}) { local $self->{__oe_exception_rec} = [$ev, $self, @arg]; $self->{__oe_exception_cb}->($@, $ev); } elsif ($self->{__oe_exception_rec}) { warn "recursion through exception callback " . "(@{$self->{__oe_exception_rec}}) => " . "($ev, $self, @arg): $@\n"; } else { warn "unhandled callback exception on event ($ev, $self, @arg): $@\n"; } } 1 # handlers ran } =item my $bool = $obj->handles ($eventname) This method returns true if any event handler has been setup for the event C<$eventname>. It returns false if that is not the case. =cut sub handles { my ($self, $ev) = @_; exists $self->{__oe_events}->{$ev} && @{$self->{__oe_events}->{$ev}} > 0 } =item $obj->event_name Returns the name of the currently executed event. =cut sub event_name { my ($self) = @_; return unless $self->{__oe_cbs}; $self->{__oe_cbs}->[2] } =item $obj->unreg_me Unregisters the currently executed callback. =cut sub unreg_me { my ($self) = @_; return unless $self->{__oe_cbs} && @{$self->{__oe_cbs}->[0]}; $self->unreg_cb ($self->{__oe_cbs}->[0]->[0]->[1]) } =item $continue_cb = $obj->stop_event 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. =cut sub stop_event { my ($self) = @_; return unless $self->{__oe_cbs} && @{$self->{__oe_cbs}->[0]}; my $r; if (defined wantarray) { my @ev = ([@{$self->{__oe_cbs}->[0]}], @{$self->{__oe_cbs}->[1]}); shift @{$ev[0]}; # shift away current cb $r = sub { $self->event (@ev) } } # XXX: Old legacy code for forwards! $self->{__oe_forward_stop} = 1; @{$self->{__oe_cbs}->[0]} = (); $r } =item $obj->add_forward ($obj, $cb) B Just for backward compatibility for L version 0.4. =cut sub add_forward { my ($self, $obj, $cb) = @_; $self->{__oe_forwards}->{$obj} = [$obj, $cb]; } =item $obj->remove_forward ($obj) B Just for backward compatibility for L version 0.4. =cut sub remove_forward { my ($self, $obj) = @_; delete $self->{__oe_forwards}->{$obj}; if (scalar (keys %{$self->{__oe_forwards}}) <= 0) { delete $self->{__oe_forwards}; } } sub _event { my $self = shift; $self->event (@_) } =item $obj->remove_all_callbacks () This method removes all registered event callbacks from this object. =cut sub remove_all_callbacks { my ($self) = @_; $self->{__oe_events} = {}; delete $self->{__oe_exception_cb}; } =item $obj->events_as_string_dump () This method returns a string dump of all registered event callbacks. This method is only for debugging purposes. =cut sub events_as_string_dump { my ($self) = @_; my $str = ''; for my $ev (keys %{$self->{__oe_events}}) { my $evr = $self->{__oe_events}->{$ev}; $str .= "$ev:\n" . join ('', map { sprintf " %5d %s\n", $_->[0], $_->[3] } @$evr) . "\n"; } $str } =back =head1 EVENT METHODS You can define static methods in a package that act as event handler. This is done by using Perl's L functionality. To make a method act as event handler you need to add the C attribute to it. B 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! B 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 is not the same as C! 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 L distribution for more examples. =head2 ALIASES If you want to define multiple event handlers as package method you can use the 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 B Please note that if you don't provide any order the methods are sorted I: 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 =head2 ALIAS ORDERING The ordering of how the methods event handlers are called if they are all defined for the same event is strictly defined: =over 4 =item 1. Ordering of the methods for the same event in the inheritance hierarchy is always dominated by the priority of the event callback. =item 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. =item 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) =back =cut our %ATTRIBUTES; sub FETCH_CODE_ATTRIBUTES { my ($pkg, $ref) = @_; return () unless exists $ATTRIBUTES{$pkg}; return () unless exists $ATTRIBUTES{$pkg}->{"$ref"}; my $a = $ATTRIBUTES{$pkg}->{"$ref"}; 'event_cb' . ( ($a->[0] ne '' || defined ($b->[1])) ? "($a->[0],$b->[1])" : '' ) } sub MODIFY_CODE_ATTRIBUTES { my ($pkg, $ref, @attrs) = @_; grep { my $unhandled = 1; if ($_ =~ /^event_cb (?: \( \s* ([^\),]*) \s* (?: , \s* ([^\)]+) \s* )? \) )?$/x) { $ATTRIBUTES{$pkg}->{"$ref"} = [$1, $2]; $unhandled = 0; } $unhandled } @attrs; } sub _init_methods { my ($pkg) = @_; my $pkg_meth = \%{"$pkg\::__OE_METHODS"}; for my $superpkg (@{"$pkg\::ISA"}) { # go recursively into super classes next unless $superpkg->isa ("Object::Event"); # skip non O::E # go into the class if we have not already been there _init_methods ($superpkg) unless *{"$superpkg\::__OE_METHODS"}{HASH}; # add the methods of the $superpkg to our own for (keys %{"$superpkg\::__OE_METHODS"}) { push @{$pkg_meth->{$_}}, @{${"$superpkg\::__OE_METHODS"}{$_} || []}; } } my %mymethds; # now check each package symbol for my $realmeth (keys %{"$pkg\::"}) { my $coderef = *{"$pkg\::$realmeth"}{CODE}; next unless exists $ATTRIBUTES{$pkg}->{"$coderef"}; # skip unattributed methods my $m = $ATTRIBUTES{$pkg}->{"$coderef"}; # $m = [$prio, $event_name] my $meth = $realmeth; if (defined $m->[1]) { # assign alias $meth = $m->[1]; } my $cb = $coderef; $cb = _debug_cb ($coderef) if $DEBUG > 1; push @{$mymethds{$meth}}, [ (exists $PRIO_MAP{$m->[0]} # set priority ? $PRIO_MAP{$m->[0]} : 0+$m->[0]), "$coderef", # callback id $realmeth, # original method name $pkg . '::' . $realmeth, # debug info $cb # the callback # only replace if defined, otherwise declarations without definitions will # replace the $cb/$coderef with something that calls itself recursively. ] if defined &{"$pkg\::$realmeth"}; #d# warn "REPLACED $pkg $meth (by $realmeth) => $coderef ($m->[1])\n"; _replace_method ($pkg, $realmeth, $meth); } # sort my methods by name for my $ev (keys %mymethds) { @{$mymethds{$ev}} = sort { $a->[2] cmp $b->[2] } @{$mymethds{$ev}}; } # add my methods to the super class method list push @{$pkg_meth->{$_}}, @{$mymethds{$_}} for keys %mymethds; # sort by priority over all, stable to not confuse names for my $ev (keys %$pkg_meth) { @{$pkg_meth->{$ev}} = sort { $b->[0] <=> $a->[0] } @{$pkg_meth->{$ev}}; } } sub _replace_method { my ($pkg, $meth, $ev) = @_; *{"$pkg\::$meth"} = sub { my ($self, @arg) = @_; _print_event_debug ($ev) if $DEBUG > 1; # either execute callbacks of the object or # alternatively (if non present) the inherited ones my @cbs = @{ $self->{__oe_events}->{$ev} || ${"$pkg\::__OE_METHODS"}{$ev} || []}; # inline the code of the C method. local $self->{__oe_cbs} = [\@cbs, \@arg, $ev]; eval { $cbs[0]->[4]->($self, @arg), shift @cbs while @cbs; () }; if ($@) { if (not ($self->{__oe_exception_rec}) && $self->{__oe_exception_cb}) { local $self->{__oe_exception_rec} = [$ev, $self, @arg]; $self->{__oe_exception_cb}->($@, $ev); } elsif ($self->{__oe_exception_rec}) { warn "recursion through exception callback " . "(@{$self->{__oe_exception_rec}}) => " . "($ev, $self, @arg): $@\n"; } else { warn "unhandled callback exception on event " . "($ev, $self, @arg): $@\n"; } } @cbs > 0 }; } =head1 DEBUGGING There exists a package global variable called C<$DEBUG> that control debugging capabilities. Set it to 1 to produce a slightly extended 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: export PERL_OBJECT_EVENT_DEBUG=2 =head1 AUTHOR Robin Redeker, C<< >>, JID: C<< >> =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc Object::Event You can also look for information at: =over 4 =item * AnnoCPAN: Annotated CPAN documentation L =item * CPAN Ratings L =item * RT: CPAN's request tracker L =item * Search CPAN L =back =head1 ACKNOWLEDGEMENTS Thanks go to: - Mons Anderson for suggesting the 'handles' method and the return value of the 'event' method and reporting bugs. =head1 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. =cut 1; perl-Object-Event-1.23/samples/000075500000000000000000000000001161645677600163515ustar00rootroot00000000000000perl-Object-Event-1.23/samples/benchmark000075500000000000000000000007741161645677600202410ustar00rootroot00000000000000#!perl package foo; use common::sense; use base qw/Object::Event/; sub new { my $this = shift; my $class = ref($this) || $this; $class->SUPER::new (@_); } our $cnt; sub test : event_cb { $cnt++ } package main; use common::sense; use Benchmark qw/:all/; my $f = foo->new; for (1..10) { $f->reg_cb (test => sub { my ($f) = @_; $foo::cnt++; }); } my $ev; timethese (250000, { simple => sub { $f->event ('test') }, method => sub { $f->test } }); print "$foo::cnt\n"; perl-Object-Event-1.23/samples/mass_example000075500000000000000000000013521161645677600207560ustar00rootroot00000000000000#!/opt/perl/bin/perl # just a small example script for testing whether memory might be leaked. package test; use Object::Event; our @ISA = qw/Object::Event/; sub new { my $c = shift; my $self = $c->SUPER::new (@_); # register on the 'up' event and then call the 'down' event $self->reg_cb (up => sub { $self->event ('down'); }); $self } sub up { my ($self) = @_; $self->event ('up'); # genereate an internal up event } package main; my $t = test->new; my $cnt = 0; $t->reg_cb ( # reg_cb registers on a set of specific events down => sub { my ($t) = @_; $cnt++; } ); for (1..1000000) { $t->up; # test will emit the 'down' even we registered upon above if ($cnt % 1000) { print "$cnt\n" } } perl-Object-Event-1.23/samples/simple_example000075500000000000000000000011601161645677600213010ustar00rootroot00000000000000#!/opt/perl/bin/perl package test; use Object::Event; our @ISA = qw/Object::Event/; sub new { my $c = shift; my $self = $c->SUPER::new (@_); # register on the 'up' event and then call the 'down' event $self->reg_cb (up => sub { $self->event ('down'); }); $self } sub up { my ($self) = @_; $self->event ('up'); # genereate an internal up event } package main; my $t = test->new; $t->reg_cb ( # reg_cb registers on a set of specific events down => sub { my ($t) = @_; print "Yay, we got down again...\n"; } ); $t->up; # test will emit the 'down' even we registered upon above perl-Object-Event-1.23/t/000075500000000000000000000000001161645677600151505ustar00rootroot00000000000000perl-Object-Event-1.23/t/00-load.t000064400000000000000000000002251161645677600164700ustar00rootroot00000000000000#!perl -T use Test::More tests => 1; BEGIN { use_ok( 'Object::Event' ); } diag( "Testing Object::Event $Object::Event::VERSION, Perl $], $^X" ); perl-Object-Event-1.23/t/01_simple_events.t000064400000000000000000000032451161645677600205160ustar00rootroot00000000000000#!perl use Test::More tests => 10; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; my $cnt = 0; my @cnt = (); my $called = 0; my $called_after = 0; my $called_before = 0; my $guard = $f->reg_cb ( before_test => sub { $called_before += $_[1] }, test => sub { $cnt[$cnt++] = 'first'; $called += $_[1] }, test => sub { $cnt[$cnt++] = 'second'; $called -= ($_[1] / 2) }, after_test => sub { $called_after += $_[1] }, ); $f->event (test => 10); undef $guard; $f->event (test => 20); is ($called, 5, "the two main event callbacks were called"); is ($cnt[0], 'first', "the first event callback was called first"); is ($cnt[1], 'second', "the second event callback was called first"); is ($called_after, 10, "main after event callback was called"); is ($called_before, 10, "main before event callback was called"); my $cb = sub { $called++ }; $f->reg_cb (hit_me => $cb); $f->event('hit_me'); is($called, 6, 'Hit me was called'); $f->unreg_cb ($cb); $f->event ('hit_me'); is($called, 6, 'Hit me was unregistered correctly'); my $died; $f->set_exception_cb (sub { $died++; }); $f->reg_cb (zombie => sub { die "And we are done, " }); $f->event ('zombie'); is ($died, 1, 'Exception callback was called'); $f->set_exception_cb (undef); $SIG{__WARN__} = sub { $died = $_[0] }; $f->event ('zombie'); like ($died, qr/unhandled callback exception/, 'Exception generated a warning'); $f->remove_all_callbacks; $called = 0; $f->event ('hit_me'); is ($called, 0, 'No more registered events'); perl-Object-Event-1.23/t/02_stop_event.t000064400000000000000000000044421161645677600200300ustar00rootroot00000000000000#!perl use Test::More tests => 25; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; my ($before, $event, $after, $ext_before, $ext_after); sub clear { $before = $event = $after = $ext_before = $ext_after = 0 } $f->reg_cb ( before_test => sub { $before = 1; $_[0]->stop_event if $_[1] eq 'before' }, ext_before_test => sub { $ext_before = 1; $_[0]->stop_event if $_[1] eq 'ext_before' }, test => sub { $event = 1; $_[0]->stop_event if $_[1] eq 'event' }, ext_after_test => sub { $ext_after = 1; $_[0]->stop_event if $_[1] eq 'ext_after' }, after_test => sub { $after = 1; $_[0]->stop_event if $_[1] eq 'after' }, ); clear(); $f->event ('test', 'event'); is ($before, 1, 'before has been executed'); is ($ext_before, 1, 'ext_before has been executed'); is ($event, 1, 'event has been executed'); is ($ext_after, 0, 'ext_after has not been executed'); is ($after, 0, 'after has not been executed'); clear(); $f->event ('test', 'before'); is ($before, 1, 'before has been executed'); is ($ext_before, 0, 'ext_before has been executed'); is ($event, 0, 'event has been executed'); is ($ext_after, 0, 'ext_after has not been executed'); is ($after, 0, 'after has not been executed'); clear(); $f->event ('test', 'after'); is ($before, 1, 'before has been executed'); is ($ext_before, 1, 'ext_before has been executed'); is ($event, 1, 'event has been executed'); is ($ext_after, 1, 'ext_after has not been executed'); is ($after, 1, 'after has not been executed'); clear(); $f->event ('test', 'ext_before'); is ($before, 1, 'before has been executed'); is ($ext_before, 1, 'ext_before has been executed'); is ($event, 0, 'event has been executed'); is ($ext_after, 0, 'ext_after has not been executed'); is ($after, 0, 'after has not been executed'); clear(); $f->event ('test', 'ext_after'); is ($before, 1, 'before has been executed'); is ($ext_before, 1, 'ext_before has been executed'); is ($event, 1, 'event has been executed'); is ($ext_after, 1, 'ext_after has not been executed'); is ($after, 0, 'after has not been executed'); perl-Object-Event-1.23/t/03_event_within.t000064400000000000000000000011531161645677600203420ustar00rootroot00000000000000#!perl use Test::More tests => 2; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; my $a = 0; my $b = 0; $f->reg_cb ( test => sub { my ($f) = @_; $a++; $f->unreg_me; $f->reg_cb (test => sub { my ($f) = @_; $b++; $f->unreg_me; }); () } ); $f->event ('test'); $f->event ('test'); $f->event ('test'); is ($a, 1, 'first callback was called once'); is ($b, 1, 'second callback was called once'); perl-Object-Event-1.23/t/04_recursive.t000064400000000000000000000010321161645677600176430ustar00rootroot00000000000000#!perl use Test::More tests => 1; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; my $a = 0; $f->reg_cb ( test => sub { my ($f) = @_; $a++; if ($a == 1) { $f->event ('test'); } elsif ($a == 2) { $f->unreg_me; } } ); $f->event ('test'); $f->event ('test'); $f->event ('test'); is ($a, 2, 'first callback was called twice'); perl-Object-Event-1.23/t/05_unreg_rec.t000064400000000000000000000015051161645677600176130ustar00rootroot00000000000000#!perl use Test::More tests => 6; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; my $a = 0; my $b = 0; $f->reg_cb ( test => sub { my ($f) = @_; $a++; $f->unreg_me; $f->event ('test2'); }, test2 => sub { my ($f) = @_; $b++; $f->unreg_me; } ); ok ($f->handles ('test'), "handles 'test'"); ok ($f->handles ('test2'), "handles 'test2'"); $f->event ('test'); $f->event ('test'); $f->event ('test2'); $f->event ('test2'); ok (!$f->handles ('test'), "doesn't handle 'test'"); ok (!$f->handles ('test2'), "doesn't handle 'test2'"); is ($a, 1, 'first callback was called once'); is ($b, 1, 'second callback was called once'); perl-Object-Event-1.23/t/06_unreg_guard.t000064400000000000000000000014001161645677600201370ustar00rootroot00000000000000#!perl use Test::More tests => 5; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; my $called = 0; my $id = $f->reg_cb (test => sub { $called += $_[1] }); $f->event (test => 10); is ($called, 10, "first test called once"); ok ($f->handles ('test'), "got a handler"); $f->unreg_cb ($id); $f->event (test => 20); is ($called, 10, "second test still called once"); ok (!$f->handles ('test'), "no handler anymore"); $called = 0; my $sub = sub { $called++ }; $id = $f->reg_cb (t => $sub); $f->event ('t'); $id = $f->reg_cb (t => $sub); $f->event ('t'); is ($called, 2, "guard removal on assignment correct"); perl-Object-Event-1.23/t/07_event_name.t000064400000000000000000000006531161645677600177700ustar00rootroot00000000000000#!perl use Test::More tests => 1; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; $f->reg_cb ( test123 => sub { my ($f) = @_; $f->{name} = $f->event_name; } ); $f->event ('test123'); is ($f->{name}, 'test123', 'event_name method returns event name'); perl-Object-Event-1.23/t/08_priorities.t000064400000000000000000000027751161645677600200500ustar00rootroot00000000000000#!perl use Test::More tests => 4; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; $f->reg_cb ( test => sub { push @{$f->{a}}, 3 }, before_test => sub { push @{$f->{a}}, 1 }, after_test => sub { push @{$f->{a}}, 5 }, ext_before_test => sub { push @{$f->{a}}, 2 }, ext_after_test => sub { push @{$f->{a}}, 4 }, ); $f->event ('test'); is (join (',', @{$f->{a}}), '1,2,3,4,5', 'priorities called in correct order'); my $idg = $f->reg_cb ( test => 2000 => sub { push @{$f->{a}}, -1 }, test => -100 => sub { push @{$f->{a}}, 3.5 }, test => 100 => sub { push @{$f->{a}}, 2.5 }, test => -2000 => sub { push @{$f->{a}}, 6 } ); my $idg2 = $f->reg_cb ( test => 2000 => sub { push @{$f->{a}}, -1 }, test => -100 => sub { push @{$f->{a}}, 3.5 }, test => 100 => sub { push @{$f->{a}}, 2.5 }, test => -2000 => sub { push @{$f->{a}}, 6 } ); @{$f->{a}} = (); $f->event ('test'); is (join (',', @{$f->{a}}), '-1,-1,1,2,2.5,2.5,3,3.5,3.5,4,5,6,6', 'custom priorities called in correct order'); $idg = undef; @{$f->{a}} = (); $f->event ('test'); is (join (',', @{$f->{a}}), '-1,1,2,2.5,3,3.5,4,5,6', 'priorities called in correct order after remove'); $f->unreg_cb ($idg2); @{$f->{a}} = (); $f->event ('test'); is (join (',', @{$f->{a}}), '1,2,3,4,5', 'priorities called in correct order after remove'); perl-Object-Event-1.23/t/10_continue.t000064400000000000000000000011051161645677600174560ustar00rootroot00000000000000#!perl use Test::More tests => 2; package foo; use common::sense; use base qw/Object::Event/; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; sub test { } package main; use common::sense; my $f = foo->new; my $cont; $f->reg_cb (test => sub { my ($f, $a) = @_; $f->{val} += $a * 1; $cont = $f->stop_event; }); $f->reg_cb (test => sub { my ($ev, $a) = @_; $f->{val} *= $a * 2; }); $f->{val} = 0; $f->event ('test', 3); is ($f->{val}, 3, 'event got captured okay'); $f->{val} = 1; $cont->(); is ($f->{val}, 6, 'event got continued okay'); perl-Object-Event-1.23/t/12_prio.t000064400000000000000000000013211161645677600166050ustar00rootroot00000000000000#!perl use Test::More tests => 1; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; # obfuscation around update_test my $oe = 'Object' . '::' . 'Event'; &{$oe. '::' . 'register_priority_alias'} ('first', 3000); &{$oe. '::' . 'register_priority_alias'} ('last', -3000); my $f = foo->new; $f->reg_cb ( first_test => sub { push @{$f->{r}}, 1 }, before_test => sub { push @{$f->{r}}, 2 }, test => sub { push @{$f->{r}}, 3 }, last_test => sub { push @{$f->{r}}, 4 }, ); $f->event ('test'); is (join (',', @{$f->{r}}), '1,2,3,4', 'all callbacks got correct order'); perl-Object-Event-1.23/t/13_methods.t000064400000000000000000000032141161645677600173030ustar00rootroot00000000000000#!perl use Test::More tests => 12; package foo; use common::sense; use base qw/Object::Event/; sub test : event_cb { my ($f, $a) = @_; $f->{a} += $a; } sub after : event_cb(after) { $_[0]->{x} = 10 } sub noafter : event_cb { $_[0]->{y} = 10 } sub pt : event_cb { push @{$_[0]->{a}}, 20 } sub foobar : event_cb; sub foozzz : event_cb(, foobar); package foo2; use base qw/foo/; sub pt : event_cb(-1000) { push @{$_[0]->{a}}, 30 } package foo3; use base qw/foo2/; sub pt : event_cb(1000) { push @{$_[0]->{a}}, 10 } package main; use common::sense; my $f = foo->new; my $f2 = foo->new; $f->reg_cb (test => sub { $_[0]->{a} += 3 }); $f2->reg_cb (test => sub { $_[0]->{a} += 9 }); $f->test (10); is ($f->{a}, 13, 'first object got event'); is ($f2->{a}, undef, 'second object got no event'); $f2->event (test => 20); is ($f->{a}, 13, 'first object got no event'); is ($f2->{a}, 29, 'second object got event'); $f->reg_cb (foobar => sub { $_[0]->{b} = 10 }); $f->foobar; $f2->foobar; is ($f->{b}, 10, 'first object got method with event callback'); is ($f2->{b}, undef, 'second object doesn\'t have method with event callback'); $f->{b} = 0; $f->foozzz; is ($f->{b}, 10, 'first object got method with event callback with alias method'); ok ($f->event ('test'), 'event returns true for methods'); my $g = foo3->new; $g->reg_cb (after => sub { $_[0]->{x} = 20 }); $g->reg_cb (noafter => sub { $_[0]->{y} = 20 }); $g->after; is ($g->{x}, 10, "priorities work"); $g->event ('after'); is ($g->{x}, 10, "priorities work 2"); $g->noafter; is ($g->{y}, 20, "priorities work"); $g->pt; is (join (',', @{$g->{a}}), '10,20,30', "priorities of event methods"); perl-Object-Event-1.23/t/14_methods2.t000064400000000000000000000016251161645677600173720ustar00rootroot00000000000000#!perl use Test::More tests => 5; package foo; use common::sense; use base qw/Object::Event/; sub test : event_cb { my ($self, $a, $b) = @_; $self->{res} = $a + $b; } sub reset { (shift)->{res} = 0 } package main; use common::sense; my $f = foo->new; $f->test (10, 20); is ($f->{res}, 30, 'calling method simply works'); $f->reset; my $id1 = $f->reg_cb (test => sub { my ($self, $a, $b) = @_; $self->{res} *= 2; $self->{res} += $a + $b; }); $f->test (10, 20); is ($f->{res}, 90, 'simple chaining works'); $f->reset; my $id2 = $f->reg_cb (before_test => sub { my ($self, $a, $b) = @_; $self->{res} = $a * $b; }); $f->test (10, 20); is ($f->{res}, 90, 'special events work'); $f->reset; $f->unreg_cb ($id1); $f->unreg_cb ($id2); $f->test (20, 30); is ($f->{res}, 50, 'unreg_cb works'); $f->reset; $f->event (test => 30, 40); is ($f->{res}, 70, 'calling event() works too'); perl-Object-Event-1.23/t/15_methods_subc.t000064400000000000000000000033211161645677600203200ustar00rootroot00000000000000#!perl use Test::More tests => 5; package first; use common::sense; use base qw/Object::Event/; sub test2 : event_cb { my ($self, $a) = @_; push @{$self->{chain}}, 'first::test2'; } sub test3 : event_cb { my ($self) = @_; push @{$self->{chain}}, 'first::test3'; } package pre; use common::sense; use base qw/first/; sub test2 : event_cb { my ($self, $a) = @_; push @{$self->{chain}}, 'pre::test2'; } package foo; use common::sense; use base qw/Object::Event/; sub test : event_cb { my ($self, $a, $b) = @_; push @{$self->{chain}}, 'foo::test'; } package bar; use common::sense; use base qw/foo pre/; sub test : event_cb { my ($self, $a, $b) = @_; push @{$self->{chain}}, 'bar::test'; } sub test2 : event_cb { my ($self, $a) = @_; push @{$self->{chain}}, 'bar::test2'; } package main; use common::sense; my $f = foo->new; my $b = bar->new; $b->test2 (100); is ((join ",", @{delete $b->{chain}}), 'first::test2,pre::test2,bar::test2', 'bar first class works.'); $b->test3 (200); is ((join ",", @{delete $b->{chain}}), 'first::test3', 'bar first undecl class works.'); $f->reg_cb (before_test => sub { my ($f) = @_; push @{$f->{chain}}, 'f::before_test'; }); $b->reg_cb (before_test => sub { my ($f) = @_; push @{$f->{chain}}, 'b::before_test'; }); $b->reg_cb (test2 => sub { my ($f) = @_; push @{$f->{chain}}, 'b::test2'; }); $f->test (10, 20); is ((join ",", @{delete $f->{chain}}), 'f::before_test,foo::test', 'foo class works.'); $b->test (10, 20); is ((join ",", @{delete $b->{chain}}), 'b::before_test,foo::test,bar::test', 'bar class works.'); $b->test2 (100); is ((join ",", @{delete $b->{chain}}), 'first::test2,pre::test2,bar::test2,b::test2', 'bar class works.'); perl-Object-Event-1.23/t/16_event.t000064400000000000000000000006151161645677600167660ustar00rootroot00000000000000#!perl use Test::More tests => 2; package foo; use common::sense; use Object::Event; $Object::Event::ENABLE_METHODS_DEFAULT = $ENV{OE_METHODS_ENABLE}; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; $f->reg_cb (test1 => sub { }); ok ($f->event ('test1'), "event returns true for reg_cb cb."); ok (!$f->event ('test2'), "event returns false for non cb."); perl-Object-Event-1.23/t/17_methods_alias.t000064400000000000000000000020401161645677600204540ustar00rootroot00000000000000#!perl use Test::More tests => 2; package foo; use common::sense; use base qw/Object::Event/; sub test : event_cb { push @{$_[0]->{x}}, 'middle' } sub after : event_cb(after, test) { push @{$_[0]->{x}}, 'after' } sub xtest : event_cb( , test) { push @{$_[0]->{x}}, 'aftermiddle' } package meh; use common::sense; use base qw/Object::Event/; sub test : event_cb { push @{$_[0]->{x}}, 'middle' } sub test_last : event_cb(-1, test) { push @{$_[0]->{x}}, 'after' } sub test_first : event_cb(1, test) { push @{$_[0]->{x}}, 'before' } package main; use common::sense; my $f = foo->new; $f->reg_cb (test => 100 => sub { push @{$_[0]->{x}}, 'first' }); $f->xtest; is (join (',', @{$f->{x}}), 'first,middle,aftermiddle,after', 'event aliases work'); my $f2 = meh->new; $f2->reg_cb (test => -0.5 => sub { push @{$_[0]->{x}}, 'shortaftermiddle' }); $f2->event ('test'); is (join (',', @{$f2->{x}}), 'before,middle,shortaftermiddle,after', 'event aliases with prios work'); eval "package meh; sub test_xx :event_cb :xx { }"; perl-Object-Event-1.23/t/18_method_inherit.t000064400000000000000000000022301161645677600206440ustar00rootroot00000000000000#!perl use Test::More tests => 3; package moh; use common::sense; use base qw/Object::Event/; sub test : event_cb { push @{$_[0]->{x}}, 'moh' } sub xtest : event_cb(,test) { push @{$_[0]->{x}}, 'moh2' } package baz; use common::sense; use base qw/moh/; sub test : event_cb(-100) { push @{$_[0]->{x}}, 'baz' } sub xtest : event_cb(-100,test) { push @{$_[0]->{x}}, 'baz2' } sub mtest : event_cb(-1000,test) { push @{$_[0]->{x}}, 'bazlast' } package foo; use common::sense; use base qw/moh/; sub test : event_cb { push @{$_[0]->{x}}, 'foo' } package meh; use common::sense; use base qw/baz foo/; sub test : event_cb { push @{$_[0]->{x}}, 'meh' } package main; use common::sense; my $f = foo->new; $f->reg_cb (test => 100 => sub { push @{$_[0]->{x}}, 'first' }); $f->test; is (join (',', @{$f->{x}}), 'first,moh,moh2,foo', 'foo class'); my $m = meh->new; $m->reg_cb (test => -1 => sub { push @{$_[0]->{x}}, 'middle2' }); $m->test; is (join (',', @{$m->{x}}), 'moh,moh2,moh,moh2,foo,meh,middle2,baz,baz2,bazlast', 'meh class diamond'); my $b = baz->new; $b->test; is (join (',', @{$b->{x}}), 'moh,moh2,baz,baz2,bazlast', 'baz class'); perl-Object-Event-1.23/t/18_method_inherit_2.t000064400000000000000000000020061161645677600210660ustar00rootroot00000000000000#!perl use Test::More tests => 3; package moh; use common::sense; use base qw/Object::Event/; sub xtest : event_cb(,test) { push @{$_[0]->{x}}, 'moh2' } sub ztest : event_cb(-10,test) { push @{$_[0]->{x}}, 'moh3' } package baz; use common::sense; use base qw/moh/; sub xtest : event_cb(-100,test) { push @{$_[0]->{x}}, 'baz2' } sub mtest : event_cb(-1000,test) { push @{$_[0]->{x}}, 'bazlast' } package meh; use common::sense; use base qw/baz/; sub test : event_cb { push @{$_[0]->{x}}, 'meh' } package main; use common::sense; my $f = baz->new; $f->reg_cb (test => 100 => sub { push @{$_[0]->{x}}, 'first' }); $f->event ('test'); is (join (',', @{$f->{x}}), 'first,moh2,moh3,baz2,bazlast', 'foo class'); my $m = meh->new; $m->reg_cb (test => -1 => sub { push @{$_[0]->{x}}, 'middle2' }); $m->test; is (join (',', @{$m->{x}}), 'moh2,meh,middle2,moh3,baz2,bazlast', 'meh class diamond'); my $b = baz->new; $b->event ('test'); is (join (',', @{$b->{x}}), 'moh2,moh3,baz2,bazlast', 'baz class'); perl-Object-Event-1.23/t/19_method_exept.t000064400000000000000000000013731161645677600203370ustar00rootroot00000000000000#!perl use Test::More tests => 3; package foo; use common::sense; use base qw/Object::Event/; sub test : event_cb { } package foo2; use base qw/foo/; sub test : event_cb(-1000) { die } package foo3; use base qw/foo2/; sub test : event_cb(1000) { } package main; use common::sense; my $f = foo3->new; my $died; $f->set_exception_cb (sub { $died++; }); $f->test; ok ($died, "got exception from method"); my $warn; $SIG{__WARN__} = sub { $warn = $_[0]; }; $f->set_exception_cb (sub { $f->test; }); $f->test; ok ($warn =~ /recursion/, "got exception callback recursion"); $warn = undef; $f->set_exception_cb (sub { $f->event ('test'); }); $f->event ('test'); ok ($warn =~ /recursion/, "got exception callback recursion via event method"); perl-Object-Event-1.23/t/20_forward_legacy.t000064400000000000000000000010601161645677600206230ustar00rootroot00000000000000#!perl use Test::More tests => 6; package foo; use common::sense; use base qw/Object::Event/; our @ISA = qw/Object::Event/; package main; use common::sense; my $f = foo->new; my $x = foo->new; my @ev; $f->add_forward ($x, sub { my ($f, $x, $ev, @args) = @_; (@ev) = ($f, $x, $ev, @args); }); $f->event ('test', 1, 2, 3); is (ref ($ev[0]), "foo", "first object ok"); is ("$ev[1]", "$x", "second object ok"); is ($ev[2], "test", "event name ok"); is ($ev[3], 1, "event name ok"); is ($ev[4], 2, "event name ok"); is ($ev[5], 3, "event name ok"); perl-Object-Event-1.23/t/pod-coverage.t000064400000000000000000000010401161645677600177030ustar00rootroot00000000000000use common::sense; use Test::More; # Ensure a recent version of Test::Pod::Coverage my $min_tpc = 1.08; eval "use Test::Pod::Coverage $min_tpc"; plan skip_all => "Test::Pod::Coverage $min_tpc required for testing POD coverage" if $@; # Test::Pod::Coverage doesn't require a minimum Pod::Coverage version, # but older versions don't recognize some common documentation styles my $min_pc = 0.18; eval "use Pod::Coverage $min_pc"; plan skip_all => "Pod::Coverage $min_pc required for testing POD coverage" if $@; all_pod_coverage_ok(); perl-Object-Event-1.23/t/pod.t000064400000000000000000000003411161645677600161150ustar00rootroot00000000000000#!perl -T use common::sense; use Test::More; # Ensure a recent version of Test::Pod my $min_tp = 1.22; eval "use Test::Pod $min_tp"; plan skip_all => "Test::Pod $min_tp required for testing POD" if $@; all_pod_files_ok(); perl-Object-Event-1.23/test000075500000000000000000000001241161645677600156070ustar00rootroot00000000000000#!/bin/sh export OE_METHODS_ENABLE=0 make test export OE_METHODS_ENABLE=1 make test