Odd bug

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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 "$_*.txt"
    or -e glob "'$'_*.txt");
DB<33> x $ocr == 0 and (-e glob "$_*.txt"
    or -e glob "'$'_*.txt");
0 1
DB<34> p $ocr
DB<35> n
502: ($str, $tmp) = $file =~ /^(.*?)([^\/]+)$/;
DB<35> p $ocr

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

Re: Odd bug

urizen wrote:
Quoted text here. Click to load it

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 "$_*.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 "$_*.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

Site Timeline