POE::Session object_states: handlers are sub names not CODEREFs

This works as expected:

    sub _poll_start {
        my $self = $_[OBJECT];
    [...]
    POE::Session->create(
        'object_states' => [
            $self => {
                '_start' => '_poll_start',
                'Work'   => '_poll_work',
                '_stop'  => '_poll_stop',
            }
        ],
    );

This, on the other hand, calls the handlers but without filling the @_ array:

    sub _poll_start {
        my $self = $_[OBJECT];    # $self will be undef
    [...]
    POE::Session->create(
        'object_states' => [
            $self => {
                '_start' => _poll_start,
                'Work'   => _poll_work,
                '_stop'  =>'_poll_stop,
            }
        ],
    );

Since this behavior is now documented, it is a feature not a bug :).

2 Comments

That's nothing to do with POE, though - you're just calling _poll_start / _poll_work etc. without parameters?

perl -e'sub _poll_start { print "Called with @_\n" } my $thing = { start => _poll_start };'

passing sub { $self->_poll_start } or better yet $self->curry::_poll_start (after installing the excellent 'curry' module) should work.

Tom's suggestion only works with inline_states, which take code references. If you pass a code reference to object_states, POE::Session will treat the code reference as a method name. Your program will fail when Perl can't find method "CODE(0xindeterminate)".

You can use object_states, package_states, and inline_states simultaneously in the same POE::Session instance. It's possible to use object_states with two or more objects. package_states can invoke handlers from more than one package.

Be careful! It's very easy to make a mess with this.

Leave a comment

About Mark Leighton Fisher

user-pic Perl/CPAN user since 1992.