package main; use strict; { # these are the events to be triggered my @events= ( [moose => q/Moose saves thousands from terror attack!/] , [antelope => q/Antelope wins baseball game /], [moose => q/Moose alerts medial over email scams!/] , ); sub events_happening { # this sub will just iterate over @events, using closure funkyness # it will return an empty list when there are none left ( hence the [] ) @{ shift @events || [] }; } } # here are some callbacks sub moose { print "[HANDLED moosed ->", @_ , " for you ]\n"; } sub antelope { print "[ANTELOPE HANDLER WON'T BE CALLED->", @_ , "]\n"; } # set up our event handling object my $handler = f00li5h::Events->new(); # we pass in a proper coderef here $handler->register_callback( 'moose' => \&moose ); # here, we just pass in a name, the class will reject it... $handler->register_callback( 'antelope' => 'antelope' ); # this one uses an anonymous sub (which is a coderef) # it would run happily, if i had a 'llama' event in the list # you can add that as an exercise if you want ;) $handler->register_callback( 'llama' => sub { print "I'm there is no event to trigger me, because f00li5h thought of adding me after he'd written and run the code"; } ); use Data::Dumper; # neat, an event loop, normally, you would $f00li5h->run() and have the object get it's events from somewhere while( my($event_name, @event_args) = events_happening ){ my $result = $handler->trigger_event($event_name, @event_args); warn "Triggering $event_name with (" . join( ",", @event_args ) . ") resulted in ". Dumper($result) if $result; }