If statement not working


I’m working with the below if statement in my code and it doesn’t seem to matching the condition correctly:

if ($maxFile ne "") { 
                     my $fileName = basename($maxFile,".TXT");
                     my $currentSeqNum = substr($fileName,-1,1);
                     my $nextSeqNum = $currentSeqNum + 1;
   else { my $nextSeqNum = 0; }

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


this returns:

MaxFile: FOT05172.TXT

File Name: 

Current Sequence Num: 

Next Sequence Num: 

$maxfile is returned from a DBI call.

Any ideas where I’m going wrong?



I’m not a perl guy, but this is a typical problem - you’re variables are out of scope. I’m surprised you’re not getting an error on the print statements, but the print processor must be able to handle them OK.

The reason you get no values is because you’re defining them inside the if statement, which means they only exist within the if statement itself. You need to define them outside the if statement, then remove the my from within the if statement (otherwise, it’ll create ANOTHER version of the variables with a different scope).

See this page where I verified the answer before I posted: http://www.expertwebinstalls.com/cgi_tutorial/basic_perl_syntax_guide_variables.html

This recently came up in the London.pm mailing list.

Start with the first mail by Andrew Beverley, who did the same thing as you. In the languages I write I generally also declare (undefined) variables outside and set new values inside the condition. You only need to do this if these variables will be referenced after the condition, as you are doing, but if you were doing something like

if (something) {
my $foo = bar;
run a sub with $foo and return, never to call $foo again;
else (other stuff) {
my @bar = baz, quux…;
do something else and never call @bar again;

Perl will let you try to print undefined variables because it assumes you know what you’re doing, but while developing you might want to let the interpreter check undefined/undeclared variables using the -w or “use warnings” options. This is also nice for when you declare a variable properly and later reference it, but with a misspelling. When it works and when it ignores, so watch out for that “my $nextSeqNum = $currentSeqNum + 1;” thing.

Also, you are comparing a file(handle) to empty string? You might rather want to use -x to test either that your file exists, or that it’s not empty, whichever is more important to you (or you can test for both).

1 Like

The lack of an error message is because the OP’s code doesn’t use the “strict” or “warnings” pragmas:

$ perl -e 'print "$currentSeqNum\

$ perl -e 'use warnings; print "$currentSeqNum\
Name "main::currentSeqNum" used only once: possible typo at -e line 1.
Use of uninitialized value $currentSeqNum in concatenation (.) or string at -e line 1.

$ perl -e 'use strict; print "$currentSeqNum\
Global symbol "$currentSeqNum" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.

Catching problems like this is one of the major reasons why it’s standard advice to always

use strict;
use warnings;

in your Perl programs.

1 Like

Thanks all. Very informative and extremely useful.