The POE site does seem a little complex. Here is an example of a script that calls one function once, then that function starts a loop of calls to a second function.
use strict;
use POE;
use POE::Session;
POE::Session->create(
inline_states => {
_start => \&start, #_start
+ is automagicaly the first event
continue => \&looping #user defin
+ed event we will call later
}
);
$poe_kernel->run();
sub start {
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
print "Starting in 5!\n";
$kernel->delay( continue => 5 ); # call the continue event in fi
+ve seconds
}
sub looping {
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
$heap->{counter}++; #the heap is just a hash we can put goodi
+es in.
print "$heap->{counter} iteration!\n";
if ($heap->{counter} < 10) {
$kernel->delay( continue => 1 ); #start state continue in 1
+seconds
}
}
Now that might look confusing but try it out. Its actualy very nice, and fun. Beats the crap out of making a loop and having counter variables to see if its time to run it or not.
Now here is the same code, but i cut and pasted parts and changed the intervals. This is why poe is pretty!
use strict;
use POE;
use POE::Session;
POE::Session->create(
inline_states => {
_start => \&start, #_start
+ is automagicaly the first event
continue => \&looping #user defin
+ed event we will call later
}
);
POE::Session->create(
inline_states => {
_start => \&start2, #_star
+t is automagicaly the first event
continue => \&looping2 #user defi
+ned event we will call later
}
);
$poe_kernel->run();
sub start {
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
print "Starting A in 5! (Loops every .5 seconds) \n";
$kernel->delay( continue => 5 ); # call the continue event in fi
+ve seconds
}
sub looping {
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
$heap->{counter}++; #the heap is just a hash we can put goodi
+es in.
print "A: $heap->{counter} iteration! \n";
if ($heap->{counter} < 100) {
$kernel->delay( continue => .5 ); #start state continue in 1
+ seconds
}
}
sub start2 {
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
print "Starting B in 7 (Loops every 2 seconds)!\n";
$kernel->delay( continue => 7 ); # call the continue event in fi
+ve seconds
}
sub looping2 {
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
$heap->{counter}++; #the heap is just a hash we can put goodi
+es in.
print "B: Iteration number $heap->{counter}!\n";
if ($heap->{counter} < 100) {
$kernel->delay( continue => 2 ); #start state continue in 1
+seconds
}
}
Tested and pre-approved. :-)
___________
Eric Hodges |