Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: When does while() test for defined vs truth

by wog (Curate)
on Dec 17, 2001 at 07:24 UTC ( [id://132455]=note: print w/replies, xml ) Need Help??


in reply to When does while() test for defined vs truth

Quoth perlop (update on seeing reply: or maybe perlop2):

....

I/O Operators

....

If and only if the input symbol is the only thing inside the conditional of a "while" statement (even if disguised as a "for(;;)" loop), the value is automatically assigned to the global variable $_, destroying whatever was there previously....

...

In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined....

....

As with filehandle reads, an automatic "defined" is gener ated when the glob occurs in the test part of a "while", because legal glob returns (e.g. a file called 0) would otherwise terminate the loop....

....

Replies are listed 'Best First'.
Re: Re: When does while() test for defined vs truth
by blakem (Monsignor) on Dec 17, 2001 at 07:42 UTC
    Ah, no wonder I couldn't find it... its actually in perlop2.

    That still doesn't explain the difference between the last two examples I gave:

    # This tests for definedness while($x = <*.pl>) {} # This tests for truthfulness 1 while($x = <*.pl>)
    Is that a bug, or should the two forms of while() behave differently?

    -Blake

      When Perl sees you using <FH> or $var = <FH> in a while (or until), it will insert the defined() for you. From perlop in bleadperl:
      The following lines are equivalent:
      while (defined($_ = <STDIN>)) { print; } while ($_ = <STDIN>) { print; } while (<STDIN>) { print; }
      (...)

      In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a "" or a "0" with no trailing newline.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        I noticed that the behavior was consistant for filehandle reads, which is why I found the behavior of globbing perplexing...

        The following lines are equivalent to each other:

        while (<STDIN>) { print; } # tests for definedness print while <STDIN>; # tests for definedness
        But these two are not equivalent to each other:
        while ($_ = <*>) { print } # tests for definedness print while ($_ = <*>); # tests for truthfulness
        Update -- Here is an example in action:
        % mkdir newdir % cd newdir % touch 0 1 2 % ls 0 1 2 % perl -le 'while ($_ = <*>) { print }' 0 1 2 % perl -le 'print while ($_ = <*>)' [note nothing is printed here]

        -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://132455]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found