in reply to Re: Can you read a false value from a file?
in thread Can you read a false value from a file?

Ah, so a string with a trailing newline will never be false, but it's possible that the last line of the file won't have the trailing newline. So, without the defined( ... ) I might fail to process the last line But even then, my $line = <FH> will still return true.

Replies are listed 'Best First'.
Re^3: Can you read a false value from a file?
by converter (Priest) on Feb 11, 2006 at 10:06 UTC
    When you write while ($scalar = <FILEHANDLE>) { ... }, perl takes care of the test for definedness for you:
    $ perl -MO=Deparse -e 'open $fh,"foo.txt"; while (my $line = <$fh>) { +print $line }' open $fh, 'foo.txt'; while (defined(my $line = <$fh>)) { print $line; } -e syntax OK
Re^3: Can you read a false value from a file?
by ikegami (Patriarch) on Feb 11, 2006 at 09:58 UTC
    Did you only read the last line of my post? I said the opposite, and included a code sample proving the opposite. Even without the newline, it won't fail.

      No, I misread the whole thing - sorry.

      Is it documented anywhere that

      while (my $line = <FH>)

      is equivalent to

      while (defined(my $line = <FH>))

        But it used not to be that way, the implicit defined got added. In fact, your description is precisely why it got changed. Even more: if one of the files in the middle of a files list ended wit a bare "0", the rest of the files were just dropped.