# example_code.pl - a master object, a flunkie object and the # POE code to make it go. # # first, we create the POE session, with the master object's constructor # as it's argument # # note that it also takes an arrayref of event handler subroutines. # # the subroutines are just regular old subroutines. if an event # with the same name as your subroutine gets posted to your session, # (and you've told the POE session about it) the subroutine runs. # # the _start and _stop events are the standard POE start and stop # events, for init and cleanup. # # you'll notice that i start all these subroutines by pulling the # object, kernel, session, and heap out of the default subroutine # paramater array @_. these are the object that is handling the # event (or $self as some people write it) ,POE kernel, the # current session, and the heap respectively. the heap lets you # store a scalar to store a reference to some arbitrary data # structure. # start the program use POE; POE::Session->new( master->new('Atilla') => [qw(_start _stop make_flunkies)] ); $poe_kernel->run(); # $poe_kernel is exported by POE #### end the program package master; use POE; # constructor sub new { my ($self,$class) = ({},shift); $self->{name} = shift; return bless $self,$class; } # this gets called when the standard '_start' event happens sub _start { my ($object, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP]; print "Master $object->{name} started...\n"; # now we 'post' an event to the kernel, to trigger the make_flunkies subroutine $kernel->post($session,'make_flunkies'); } # this gets called when the standard '_stop' event happens sub _stop { my ($object, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP]; print "Master $object->{name} is all done\n"; } sub make_flunkies { my ($object, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP]; # we'll create some flunkies... for(qw(moe larry curly joe shemp)) { # create a new session for each flunkie POE::Session->new(flunkie->new($_) => [qw(_start _stop do_your_schtick)]) } # now tell each flunkie to do their thing - we couldn't do this without # the $kernel->alias_set($object->{name}) code in the flunkie::_start # method. for(qw(moe larry curly joe shemp)) { $kernel->post($_,'do_your_schtick'); } } 1; package flunkie; use POE; sub new { my ($self,$class) = ({},shift); $self->{name} = shift; return bless $self,$class; } sub _start { my ($object, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP]; print "$object->{name} started\n"; $kernel->alias_set($object->{name}); # we do this so we can refer to the flunkie by name later... } sub _stop { my ($object, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP]; print "$object->{name} stopped\n"; } sub do_your_schtick { my ($object, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP]; my @schticks = ( 'dancing', 'juggling', 'slipping on a banana peel', 'pretending to be on fire', 'hitting himself with a hammer', 'flapping his arms', 'walking on his hands', 'licking a hamster','driving a tiny car','performing a funny walk' ); print "$object->{name} is " . $schticks[rand @schticks] ." for your amusement\n"; }