Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

No Change in Pre- or Post-Increment Varable

by ljamison (Sexton)
on Dec 21, 2015 at 21:54 UTC ( [id://1150898]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks! I've had a number of posts recently about a script I'm working on and I'm back again still stuck on a problem incrementing variables.

The following is part of a script which handles incoming socket connections, processes them (currently to Log file) then sends a formatted response message back to the server (an "ACK/NAK" type handshake method). My goal is to have our $stat_rsnr_seq increment for each connection after it is accepted as shown in the STAT elsif statement. For reference, the initConnection() handles the client socket connection, the LogIt sub handles logging to a text file, sendRequest handles the socket send() functionality, and recv() handles the socket recv() functionality. My problem is that a client is accepted, data is received by this script matching the STAT pattern and the response is sent to the client as it should. HOWEVER the increment never appears to take place and is always '00001' even though I need it to go up to '99999' then back to '00001' if it exceeds '99999'. What am I missing? I'm assuming it's something trivial that I'm just overlooking. Thank you for any assistance!

#!/usr/bin/perl -w use warnings; use IO::Socket::INET; use POSIX qw( WNOHANG ); # ... other variable initializations our $stat_rsnr_seq = '00001'; # ... Irrelevant listening port information and data handling subs while () { # forever while ($handle = $listener->accept()) { print "A client has connected!\n"; my $pid = fork(); die "Cannot fork: $!" unless defined($pid); do { $pid = waitpid(-1, WNOHANG); } while $pid > 0; # end do { $pid = waitpid(-1, WNOHANG); } if ($pid == 0) { # child process my $accepted_message = &recv(); &LogIt("Received message from EMS: $accepted_message"); # PING response if ($accepted_message =~ /00000RSNR00000/) { # RSNR methods/RESP response/wait on STAT } elsif ($accepted_message =~ /ADD00200/) { # ADD methods/RESP response/wait on STAT } elsif ($accepted_message =~ /STAT00025/) { # log message &LogIt("Message received: $accepted_message"); ############################################## # Transmit to server $handle = initConnection($host, $port); &LogIt("init_Connection - $handle"); if (!$handle) { &LogIt("Cannot connect to port $port: $!"); } # end of (!$handle) if statement else { my $msglength = 20 + 5; # Item ID + Status Code my $request_msg = $stat_rsnr_seq."RSNR"."00005"."00000"; $stat_rsnr_seq++; &sendRequest($request_msg); my $resp_msg = &recv(); if ($resp_msg =~ /RESP0000500000/) { &LogIt("Success: $accepted_message, $resp_msg"); } # end of if ($resp_msg =~ /RESP0000500000/) elsif ($resp_msg =~ /RESP0000501000/) { &LogIt("Invalid transaction code: $accepted_message, $resp_msg"); } # end of elsif ($resp_msg =~ /RESP0000501000/) elsif ($resp_msg =~ /RESP0000501001/) { &LogIt("Retry: $accepted_message, $resp_msg"); } # end of elsif ($resp_msg =~ /RESP0000501001/) elsif ($resp_msg =~ /RESP0000501002/) { &LogIt("Invalid sequence number: $accepted_message, $resp_msg"); } # end of elsif ($resp_msg =~ /RESP0000501002/) elsif ($resp_msg =~ /RESP0000500020/) { &LogIt("Invalid length: $accepted_message, $resp_msg"); } # end of elsif $resp_msg =~ (/RESP0000500020/) else { # Try again! $handle = initConnection($host, $port); &sendRequest($request_msg); my $resp_msg = &recv(); if($resp_msg !~ /RESP0000500000/){ &LogIt("Error with message: $accepted_message, $resp_msg"); } } $handle->close(); } # end of (!$handle) else statement exit(0); } # end of elsif ($accepted_message =~ /STAT00025/) } # end if ($pid == 0) } # end while ($handle = $listener->accept()) } # end while ()

Replies are listed 'Best First'.
Re: No Change in Pre- or Post-Increment Varable
by Old_Gray_Bear (Bishop) on Dec 21, 2015 at 22:23 UTC
    I rather doubt that there is a bug in the Perl code that handles $variable++, so lets look at what happens to your variable in your code that you posted.
    1. You initialize $stat_rsnr_seq to the string '00001'
    2. You use it in a printed message
    3. You increment it.
    So there are a couple of possibilities:
    1. You are resetting the variable some where else in your (unposted) code.
    2. Your if (!$handle){...} always fires (you did check to see that $handle has what you think it has, right?) and you never get to the $stat_rsnr_seq++ statement. I rather think that this is the more probable case.

    By the way, you do know that when '99999' is reached, the next value you will get for the increment is '100000' not '00000'.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: No Change in Pre- or Post-Increment Varable
by Anonymous Monk on Dec 21, 2015 at 22:19 UTC

    What am I missing? I'm assuming it's something trivial that I'm just overlooking.

    Variables aren't shared when you fork, each fork gets its own copy, so in the children you increment the variable, the parent doesn't know about that, children have their own copy

Re: No Change in Pre- or Post-Increment Varable
by stevieb (Canon) on Dec 21, 2015 at 22:18 UTC

    Welcome to the Monastery, ljamison...

    I've only reviewed your code by eye quickly, but I'd suggest putting a print statement within the else() after your if (! $handle) and see if it is ever triggered.

    In the code you supplied, that's the only place $stat_rsnr_seq ever gets incremented, so you want to ensure you hit that code.

    I'm not familiar with our, so another Monk will have to say whether that's part of the problem (I've only ever needed my()).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1150898]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found