PERL: Strange outcome of variable value manipulation


I only dip my toe into PERL programming on the odd ocassion so I was wondering if anyone had any ideas as to why the below is happening:

When I run my PERL script the variable values seem to get mixed up.

my $fileName = basename($maxFile,".TXT");
my $currentSeqNum = substr($fileName,-1,1);
my $nextSeqNum = ($currentSeqNum)++;

print "File Name: $fileName\
print "Current Sequence Num: $currentSeqNum\
print "Next Sequence Num: $nextSeqNum\

Which produces:

MaxFile: FOT05172.TXT

File Name: FOT05172

Current Sequence Num: 3

Next Sequence Num: 2

$maxFile gets it value from an sql query using DBI which is correct. The problem lies in the fact that the “Current Sequence Num:” and “Next Sequence Num:” values seem to be the wrong way round.

Can anyone spot a mistake in my code?



The auto-increment operator will return the value of the variable, then increment the variable. So first $nextSeqNum is assigned the value of $currentSeqNum, then $currentSeqNum is incremented. What you really wanted to do is:

my $nextSeqNum = $currentSeqNum + 1;

Thanks, good spot.