[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: [ID 20000128.003] Bug in magic locals vs block loops?



>>>>> "Ronald" == Ronald J Kimball <rjk@linguist.dartmouth.edu> writes:

Ronald> Coincidentally, I happened to stumble across this quirk earlier today while
Ronald> commenting on a script on another list.  In this case, it was roughly:

Ronald> until ($line =~ /regex/) {
Ronald>     $line = <>;
Ronald> }
Ronald> if ($line =~ /regex/) {
Ronald>     print $&;
Ronald> }

Ronald> This could have been simplified to:

Ronald> until ($line =~ /regex/) {
Ronald>     $line = <>;
Ronald> }
Ronald> print $&;

Ronald> were it not for this behavior.  And no !~ in the loop conditional, either.
Ronald> :)

See, that's where I hate logic like this, and start creating my own
loopy things, because yours fails if you hit EOF before /regex/
matches.

    {
      last unless defined($line = <>);
      redo unless $line =~ /regex/;
      print $&;
    }

There, nice and handy.  And even nearly the same number of lines.  And
now that I stare at this, I'd probably go to an even more familiar
form:

    while (<>) {
      redo unless /regex/;
      print $&;
      last;
    }

There.  Very clean, no bugs visible. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


Follow-Ups from:
Rick Delaney <rick.delaney@home.com>
References to:
Tim_Peters@Dragonsys.com
Larry Wall <larry@wall.org>
Ronald J Kimball <rjk@linguist.dartmouth.edu>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]