Odd bug

U

urizen

Have encountered an odd bug in Perl 5.8.4 that doesn't seem to be in my
code. The following is a bit of the debugger output:

501: $ocr = 1 if $ocr == 0 and (-e glob "${file}_*.txt"
or -e glob "'${file}'_*.txt");
DB<33> x $ocr == 0 and (-e glob "${file}_*.txt"
or -e glob "'${file}'_*.txt");
0 1
DB<34> p $ocr
0
DB<35> n
main::(/home/ajordon/scripts/GenJob:502):
502: ($str, $tmp) = $file =~ /^(.*?)([^\/]+)$/;
DB<35> p $ocr
0

Despite the if clause being true, the assignment in line 501 isn't
happening. To make things even odder, it does work on other values of
$file. And, if I cut and paste line 501 directly into the debugger, it
functions properly. Really at a loss here, so any suggestions would be
appreciated.
 
J

jl_post

urizen said:
Have encountered an odd bug in Perl 5.8.4 that doesn't
seem to be in my code. The following is a bit of the
debugger output:

501: $ocr = 1 if $ocr == 0 and (-e glob "${file}_*.txt"
or -e glob "'${file}'_*.txt");
DB<33> x $ocr == 0 and (-e glob "${file}_*.txt"
or -e glob "'${file}'_*.txt");
0 1
DB<34> p $ocr
0
DB<35> n
main::(/home/ajordon/scripts/GenJob:502):
502: ($str, $tmp) = $file =~ /^(.*?)([^\/]+)$/;
DB<35> p $ocr
0

Despite the if clause being true, the assignment in line
501 isn't happening. To make things even odder, it does
work on other values of $file. And, if I cut and paste
line 501 directly into the debugger, it functions properly.
Really at a loss here, so any suggestions would be
appreciated.


Dear Urizen,

If I remember correctly, the glob() function in scalar context
returns the matches one at a time until all entries have been returned,
after which it returns undef.

I think what's happening with you is that, in the debugger, you use
the glob() function in scalar context to return the only entry
available. This works correctly, but then when you type "n" in the
debugger, the glob() function gets called a second time and, not
finding another entry that matches your glob, returns undef (making the
condition false).

Instead of saying:

if ... and (-e glob "${file}_*.txt" or ...

try assigning the return value of the glob() function to a scalar
first, then checking to see if it exists, like this:

my $filename = glob "${file}_*.txt";
die "No file found!" if not defined $filename;
[do something] if ... and (-e $filename or ...

The reason you're having trouble is that, in scalar context, glob()
is really meant to be used in a loop, like this:

while (defined ($filename = glob("*.txt")))
{
# do something with $filename
}

Instead, you're using it to assign to a scalar variable, as if it's
assumed that only one file matches your glob.

Anyway, try my above advice and see if it helps, Urizen.

-- Jean-Luc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top