views:

131

answers:

1

Can anyone explain how variable scoping works within a POE session? What is the proper way to pass state within the session, without impacting other sessions?

Thanks Josh

+3  A: 

Scoping is unaffected by POE.

You can use POE's heap (accessible through $_[HEAP]) to pass data around between your various handlers.

According to the docs, the heap is distinct between sessions by default, but it is possible to override this so that different sessions share a heap.

sub my_state_handler {
    $_[HEAP]{some_data} = 'foo';
    $_[KERNEL]->yield('another_handler');
}

sub another_handler {
    print $_[HEAP]{some_data}, "\n"; # prints "foo\n"
}
daotoad
When creating a session, a new hash is used for the heap, as you said. One can specify the heap manually when creating a session: http://search.cpan.org/perldoc?POE::Session#heap_=>_ANYTHINGYou could use an existing hash for session's heap, or an array, etc.
Hinrik