I've come across a problem when using the perl redo statement. I created a small script (see below) which reproduces the problem.

On a Linux machine with perl 5.12.0, the script works; it has problems on an AIX machine with perl 5.8.2.

#!/usr/local/bin/perl use strict; my ($file) = @ARGV; open( FH, "<", $file ) or die "$!"; while( my $line = <FH> ) { chomp( $line ); if( $line =~ /^#/ ) { redo; } else { print "Doesn't begin with #\n"; } }

Note: the file that is opened and read contains a single '#' character in the first position.

The "correct" case: perl 5.12.0

When running in the perl debugger, and watching $line, after the redo statement executes, the code correctly jumps to the chomp statement, without reevaluating the while-condition, and without anything happening to the $line variable. (Note that, with this code sample, this will induce an endless loop. In the 'real' code, there are other things happening which would prevent this.)

The "weird" case: perl 5.8.2

When stepping through with the debugger here (same code, same input file), and watching $line, after the redo statement executes, and before executing the chomp statement, this displays:

main::(jt:12): redo; DB<2> Watchpoint 0: $line changed: old value: '#' new value: '' main::(jt:9): chomp( $line );

Notice how the $line just changed for no reason (from pound to undef).

I understand that the ideal thing here is to upgrade to 5.12 across the board, but there are many machines involved and that's not possible at the moment.

Am I hitting something known? I've Googled around for "perl redo bug", etc, but cannot locate anything.

UPDATE

By taking the my out of the while-condition, and declaring my $line just above the while condition, this seems to fix the problem. However, I still wonder why.


In reply to Does perl 5.8.2 have a bug with 'redo' statement by Mubby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.