http://www.perlmonks.org?node_id=1016739

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

Hi Monks,

I'm currently stuck with a piece of POE code. Here is a slightly stripped versions:
#!perl use warnings; use strict; use diagnostics; use IO::Socket; use POE qw(Wheel::SocketFactory Wheel::ReadWrite); my $SERVER_ADDR = '192.168.178.6'; my $SERVER_PORT = '50099'; POE::Session->create( inline_states => { _start => sub { # Start the server. $_[HEAP]{server} = POE::Wheel::SocketFactory->new( RemoteAddress => $SERVER_ADDR, RemotePort => $SERVER_PORT, SocketProtocol => 'udp', SuccessEvent => "on_connect", FailureEvent => "on_server_error", ); }, on_connect => sub { # Begin interacting with the server. my $kernel = $_[KERNEL]; my $client_socket = $_[ARG0]; my $io_wheel = POE::Wheel::ReadWrite->new( Handle => $client_socket, InputEvent => "on_receive_data", ErrorEvent => "on_connect_error", ); $_[HEAP]{client}{ $io_wheel->ID() } = $io_wheel; $io_wheel->put( "login monitor monitor", "log on", ); $kernel->yield('keepalive', $io_wheel, $kernel); }, on_server_error => sub { # Shut down server. my ( $operation, $errnum, $errstr ) = @_[ ARG0, ARG1, ARG2 + ]; warn "Server $operation error $errnum: $errstr\n"; delete $_[HEAP]{server}; }, on_receive_data => sub { # Handle client input. my ( $kernel, $input, $wheel_id ) = @_[ KERNEL, ARG0, ARG1 + ]; print "Received: $input\n"; }, on_connect_error => sub { # Handle client error, including disconnect. my $wheel_id = $_[ARG3]; delete $_[HEAP]{client}{$wheel_id}; }, keepalive => sub { my ( $io_wheel, $kernel ) = @_[ ARG0, ARG1 ]; $io_wheel->put( "keepalive" ); $kernel->delay_add( 'keepalive' => 10 ); }, } ); POE::Kernel->run(); exit;
My intention is to create a timer that calls
keepalive => sub { my ( $io_wheel, $kernel ) = @_[ ARG0, ARG1 ]; $io_wheel->put( "keepalive" ); $kernel->delay_add( 'keepalive' => 10 );
every 10 seconds. The first call to this sub works and the server, to which this client connects, sends the expected answer. After 10 seconds the sub is called again and it always ends up with:
Uncaught exception from user code: Can't call method "put" on an undefined value at fac.pl line 5 +9. at /usr/local/share/perl/5.14.2/POE/Kernel.pm line 1256 POE::Kernel::_rethrow_kr_exception('POE::Kernel=ARRAY(0x88f2e8 +)') called at /usr/local/share/perl/5.14.2/POE/Kernel.pm line 1244 POE::Kernel::run('POE::Kernel') called at fac.pl line 65
Any help would be really appreciated