问题
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
回答1:
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"
}
来源:https://stackoverflow.com/questions/1064273/how-does-variable-scoping-work-in-a-poe-session