in reply to Re: undefined question
in thread undefined question

I forgot to mention that your loop will not execute if a line evaluating to false in boolean context is read

Actually, no - if you read the perlop 'I/O Operators' entry carefully, you'll find that using <FILEHANDLE> inside a while (or for(;;)) construct is treated specially - a defined test is done for you, to avoid exactly the problem you mention...

I case you're too lazy to look it up, I quote:

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. If you really mean for such values to terminate the loop, they should be tested for explicitly:

while (($_ = <STDIN>) ne '0') { ... } while (<STDIN>) { last unless $_; ... }

In other boolean contexts, "<filehandle>" without an explicit "defined" test or comparison elicit a warning if the "use warnings" pragma or the -w command-line switch (the "$^W" variable) is in effect.

HTH

--
3dan

Replies are listed 'Best First'.
Re: Re: Re: undefined question
by chromatic (Archbishop) on Jun 09, 2003 at 17:08 UTC

    You're right; I just confirmed this with B::Deparse. Thanks!

    $ perl -MO=Deparse while (my $in = <STDIN>) { print $in; } ^D while (defined(my $in = <STDIN>)) { print $in; } - syntax OK