For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2005 > Odd bug









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Odd bug
urizen

2005-04-27, 3:58 pm

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.

jl_post@hotmail.com

2005-04-27, 3:58 pm

urizen wrote:
>
> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com