mifflin has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to understand what POE is all about. I've been reading the perldoc and the first thing it shows is an example in the SYNOPSYS. For starters I decided to type the code in and see what it does.

use strict; use warnings; use POE; sub handler_start { my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; print "Session ",$session->ID," has started\n"; $heap->{count} = 0; $kernel->yield('increment') if $heap->{count} < 10; } sub handler_increment { my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; print "Session ",$session->ID," counted to ",++$heap->{count},".\n" +; } sub handler_stop { my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; print "Session ",$session->ID," has stopped.\n"; } for (1..10) { POE::Session->create( inline_states => { _start => \&handler_start, increment => \&handler_increment, _stop => \&handler_stop, }, ); } POE::Kernel->run();

Here's the output...

Session 2 has started Session 3 has started Session 4 has started Session 5 has started Session 6 has started Session 7 has started Session 8 has started Session 9 has started Session 10 has started Session 11 has started Session 2 counted to 1. Session 2 has stopped. Session 3 counted to 1. Session 3 has stopped. Session 4 counted to 1. Session 4 has stopped. Session 5 counted to 1. Session 5 has stopped. Session 6 counted to 1. Session 6 has stopped. Session 7 counted to 1. Session 7 has stopped. Session 8 counted to 1. Session 8 has stopped. Session 9 counted to 1. Session 9 has stopped. Session 10 counted to 1. Session 10 has stopped. Session 11 counted to 1. Session 11 has stopped.

My question is basically, "what is this code trying to accomplish?"

I'm not understanding the point of the $heap->{count} variable.

It seems to always be 1.

Can anyone help me to understand?

Replies are listed 'Best First'.
•Re: Trying to understand POE
by merlyn (Sage) on Sep 28, 2004 at 21:43 UTC
Re: Trying to understand POE
by ikegami (Patriarch) on Sep 28, 2004 at 21:52 UTC
    It seems to always be 1.

    Not quite. It started off at 0. It demonstrates ten counters were each incremented once, as opposed to one counter incremented ten times. In other words, it demonstrated how to create sessions variables.

      Or to put it another way, the session's heap is akin to thread local storage (e.g. pthread_getspecific if you're familiar with pthreads).

Re: Trying to understand POE
by thospel (Hermit) on Sep 29, 2004 at 00:30 UTC
    Except that this isn't the example in the SYNOPSIS (at least it isn't in my version of POE). Mine has an uncoditional yield in handler_start and the further yielding upto value 10 in handler_increment. With these modification the example might make more sense to you (it demonstrates per session variables and POE event scheduling).
      yep, you're right! I typed it in wrong :(
      However, the example does make more sense now
      Thanks!
Re: Trying to understand POE
by kelan (Deacon) on Sep 30, 2004 at 14:18 UTC

    POE is a pure Perl event framework that allows you to create separated "threads" of work. Basically you set up event handlers and then those handlers are called when the appropriate events occur. Events can be generated by things like incoming data on a network socket or a GUI button being pushed, or by the handlers themselves, to execute the next step in some process for example.

    There's a nice turorial over at the POE site called Evolution of a POE Server that might help shed some light. Also, you should browse through the POE Cookbook to get a feel for the types of things you can do with POE.