in reply to using multiple conditionals in a while(test)

In a limited set of cases, Perl applies special treatment to while loops. One such case is when the condition is a read from a file.

In these special cases while( COND ) { gets translated into while( defined( local $_ = COND ) ) {.

But for this to happen, COND has to be a 'simple' condition.

Your example:

while( (<FP>) && (!/^_stop\n/) ) {

doesn't satisfy this requirement.

To compensate, you would have to explicitly code that to use $_. Ie.

while( defined( local $_ = <FP> ) and ! /^_stop\n/ ) {

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: using multiple conditionals in a while(test)
by jwkrahn (Abbot) on Dec 04, 2010 at 07:12 UTC
    gets translated into while( defined( local $_ = COND ) ) {.

    Not quite.    The $_ variable is NOT localized in a while loop:

    $ echo "one two three four five six seven eight nine ten" | perl -e' while ( <> ) { last if /seven/; } print "$.: $_\n"; ' 7: seven

      You're right.

      C:\test>perl -MO=Deparse -e"while( <> ) { print }" while (defined($_ = <ARGV>)) { print $_; } -e syntax OK

      But if you're doing it yourself, it's as well to.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I see. I had a feeling it was a special case.
        Excellent explanations and examples.
        Thank you.