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

Hello Monks, I'm back again requesting assistance. I've been trying to tweak this POE Cookbook example but when I try to increment a variable within a subroutine is does not increment.

#!/usr/bin/perl use strict; use warnings; use POE qw(Component::Server::TCP Component::Client::TCP); # Spawn the forwarder server on port 8020. When new connections # arrive, spawn clients to connect them to their destination. my $sequence_number = '00001'; my $format_sequence_number = sprintf("%s", $sequence_number); POE::Component::Server::TCP->new( Port => 8000, ClientConnected => sub { my ($heap, $session) = @_[HEAP, SESSION]; logevent('server got connection', $session); spawn_client_side(); }, ClientInput => sub { my ($kernel, $session, $heap, $input) = @_[KERNEL, SESSION, HEAP, ARG0]; logevent('server got input', $session, $input); $kernel->post($heap->{client_id} => send_stuff => $input); }, ClientDisconnected => sub { my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; logevent('server got disconnect', $session); $kernel->post($heap->{client_id} => "shutdown"); }, InlineStates => { send_stuff => sub { my ($heap, $stuff) = @_[HEAP, ARG0]; logevent("sending to server", $_[SESSION]); $heap->{client}->put($stuff); }, _child => sub { my ($heap, $child_op, $child) = @_[HEAP, ARG0, ARG1]; if ($child_op eq "create") { $heap->{client_id} = $child->ID; } }, }, ); sub spawn_client_side { POE::Component::Client::TCP->new( RemoteAddress => 'localhost', RemotePort => 8001, Started => sub { $_[HEAP]->{server_id} = $_[SENDER]->ID; }, Connected => sub { my ($heap, $session) = @_[HEAP, SESSION]; logevent('client connected', $session); }, ServerInput => sub { my ($kernel, $heap, $session, $input) = @_[KERNEL, HEAP, SESSION, ARG0]; logevent('client got input', $session, $input); $kernel->post($heap->{server_id} = send_stuff => $input); }, Disconnected => sub { my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; logevent('client disconnected', $session); $kernel->post($heap->{server_id} => 'shutdown'); }, InlineStates => { send_stuff => sub { my ($heap, $stuff) = @_[HEAP, ARG0]; logevent('sending to client', $_[SESSION]); if ($stuff =~ /READ/) { $stuff = $format_sequence_number . 'READ' . '00000'; $heap->{server}->put($stuff); $sequence_number += 1; } }, }, ); } sub logevent { my ($state, $session, $arg) = @_; my $id = $session->ID(); print "session $id $state "; print ": $arg" if (defined $arg); print "\n"; } warn 'running'; $poe_kernel->run(); exit 0;

I have 2 main goals with this code:

  1. have it auto-increment for each typed message sent by the client from "00001" until it reaches "99999" then have it reset to "00001" with each new client connection (hence the attempted sprintf function)
  2. have 4 unique counters for client-side InlineStates (will add more if/else matching patterns under /READ/)

Replies are listed 'Best First'.
Re: POE - can't increment within sub
by Anonymous Monk on Dec 12, 2015 at 04:17 UTC
    Yeah, ++ and += 1 are not exactly the same.
    $ perl -E 'my $x = "001"; $x += 1; say $x' 2 $ perl -E 'my $x = "001"; $x++; say $x' 002

        Unfortunately, my main problem is that when I run this example it only prints out the '00001' and ever increments it in the context I provided.

        I know, and that's why I always use += 1 rather than ++ in Perl :)

        I feel like an idiot...I didn't read over your answer carefully enough until now. I tried it with the () around the variable name and it worked correctly!!!

        Is there a way for me to use an if/else statement in this context? I want the numbers to start back at "00001" after it reaches "99999" but doing something like this:

        my $sequence_number = "00001"; if (($sequence_number)++ gt "99999") { $sequence_number = "00001"; } else { ($sequence_number)++; }

        Doesn't appear to work.

Re: POE - can't increment within sub
by Anonymous Monk on Dec 12, 2015 at 03:36 UTC
    you didn't write ++ anywhere

      Yes I did..under InlineStates I used

      $sequence_number += 1;

      which is the same thing as

      $sequence_number++

      but just written differently.