NAME
POE::Session - a state machine, driven by POE::Kernel
SYNOPSIS
new POE::Session(
$kernel,
'_start' => sub {
my ($k, $me, $from) = @_;
# initialize the session
},
'_stop' => sub {
my ($k, $me, $from) = @_;
# shut down the session
},
'_default' => sub {
my ($k, $me, $from, $state, @etc) = @_;
# catches states for which no handlers are registered
# returns 0 if the state is not handled; 1 if it is (for signal squelch)
return 0;
},
);
DESCRIPTION
POE::Session builds an initial state table and registers it as a full session with POE::Kernel. The Kernel will invoke _start after the session is registered, and _stop just before destroying it. _default is called when a signal is dispatched to a nonexistent handler.
States are invoked as: &$state_code_ref($kernel, $namespace, $source_session, @$etc).
PUBLIC METHODS
- new POE::Session($kernel, 'state' => sub { ... }, ....);
-
Build an initial state table, and register it with a
$kernel. Returns a reference to the new Session, which should be discarded promptly since the$kernelwill maintain it (and extra references prevent garbage collection).
SPECIAL NAMESPACE VARIABLES
- _debug
-
This will set the runtime debugging level for the
POE::Session.Currently it only toggles (true/false) displaying states as they are dispatched, and maybe some minor harmless warnings.
SPECIAL STATES
All states except _start are optional. Events will be discarded quietly for any states that do not exist.
- _start ($kernel, $namespace, $from)
-
Informs a
POE::Sessionthat it has been added to aPOE::Kernel.$kernelis a reference to the kernel that owns this session;$namespaceis a reference to a hash that has been set aside for this session to store persistent information;$fromis the session that sent the _start event (usually aPOE::Kernel).This is the only required state.
- _stop ($kernel, $namespace, $from)
-
Informs a
POE::Sessionthat is about to be removed from aPOE::Kernel. Anything in$namespacethat Perl cannot garbage-collect should be destroyed here to avoid leaking memory.$kernel,$namespaceand$fromare the same as for _start. - _default ($kernel, $namespace, $from, $state, @etc)
-
Informs a
POE::Sessionthat it has received an event for which no state has been registered. Without a _default state,POE::Kernelwill silently drop undeliverable events.$kernel,$namespaceand$fromare the same as for _start.$stateis the state name that would have received the event.@etcare any additional parameters (other than$kernel,$namespaceand$from) that would have been sent to$state.If the
_defaultstate handles the event, return 1. If the_defaultstate does not handle the event, return 0. This allows default states to squelch signals by handling them. - _child ($kernel, $namespace, $departing_session)
-
Informs a
POE::Sessionthat a session it created (or inherited) is about to be stopped. One use for this is maintaining a limited pool of parallel sub-sessions, starting new sessions when old ones go away.$kerneland$namespaceare the same as for _start.$departing_sessionis a reference to the session going away. - _parent ($kernel, $namespace, $new_parent)
-
Informs a
POE::Sessionthat its parent session is stopping, and that its new parent will be$new_parent.$kerneland$namespaceare the same as for _start.$new_parentis the new parent of this session.
SPECIAL STATE CLASSES
- Special States
-
These states are generated by
POE::Kerneland mainly deal with session management. Construction, destruction, and parent/child relationships. - Signal States
-
These are states that have been registered as
%SIGhandlers byPOE::Kernel::sig(...).Signal states are invoked with these paramters:
$kernel-
This is the kernel that is managing this session.
$namespace-
This is a hash into which the session can store "persistent" data. The
$namespacehash is preserved in the Kernel until the Session stops. - $from
-
This is the Session that generated this state. Under normal circumstances, this will be the Kernel.
- $signal_name
-
The name of the signal that caused this state event to be sent. It does not include the "SIG" prefix (e.g., 'ZOMBIE'; not 'SIGZOMBIE').
Signal states should return 0 if they do not handle the signal, or 1 if the signal is handled. Sessions will be stopped if they do not handle terminal signals that they receive. Terminal signals currently are defined as SIGQUIT, SIGTERM, SIGINT, SIGKILL and SIGHUP in Kernel.pm.
Note:
_defaultis also the default signal handle. It can prevent most signals from terminating a Session.There is one "super-terminal" signal, SIGZOMBIE. It is sent to all tasks when the Kernel detects that nothing can be done. The session will be terminated after this signal is delivered, whether or not the signal is handled.
- Select States
-
These states are registerd to
signal(2)logic byPOE::Kernel::select(...)and related functions.Select states are invoked with these parameters:
- Alarm States
-
These are states that accept delayed events sent by
POE::Kernel::alarm(...), but any state can do this, so why is it listed separately? - Wheel States
-
These states are added to and removed from sessions whenever
POE::Wheelderivatives are created or destroyed. They can last the entire life of a session, or they can come and go depending on the current needs of a session.
PROTECTED METHODS
- $session->_invoke_state($kernel, $source_session, $state, \@etc)
-
Called by
POE::Kernelto invoke state$stategenerated from$source_sessionwith a list of optional parameters in\@etc. Invokes the _default state if it exists and$statedoes not.Returns 1 if the event was dispatched, or 0 if the event had nowhere to go.
- $session->register_state($state, $handler)
-
Called back by
POE::Kernelto add, change or remove states from this session.
PRIVATE METHODS
- DESTROY
-
Destroys the session. Deletes internal storage.
EXAMPLES
All the programs in tests/ use POE::Session, but especially see tests/sessions.perl and tests/forkbomb.perl.
BUGS
None known.
CONTACT AND COPYRIGHT
Copyright 1998 Rocco Caputo <troc@netrus.net>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.