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

Can you read a false value from a file?

by grantm (Parson)
on Feb 11, 2006 at 08:57 UTC ( [id://529538]=perlquestion: print w/replies, xml ) Need Help??

grantm has asked for the wisdom of the Perl Monks concerning the following question:

According to the perlsyn (Perl Syntax) man page ...

while (<FH>) {

... is equivalent to ...

while (defined($_ = <FH>)) {

Conventional Perl wisdom has it that if you're not using $_, but assigning to a named variable ...

while (my $line = <FH>) {

... then you should write it like this ...

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

The reason being, you only want the loop to terminate at the end of file (<FH> will return undef) and not when you read a line that happens to evaluate to false.

My question is: Assuming $/ still contains the default value, is it possible to read a line which evaluates to false?

A quick experiment shows that the string "0" is false, but "0\n" is true. Can anyone give a string with a trailing newline that evaluates to false in a boolean context?

And, if changing $/ does make it possible, what would I have to change it to?

Replies are listed 'Best First'.
Re: Can you read a false value from a file?
by ikegami (Patriarch) on Feb 11, 2006 at 09:23 UTC

    Just like
    while (<FH>)
    is equivalent to
    while (defined($_ = <FH>))

    while (my $line = <FH>)
    is equivalent to
    while (defined(my $line = <FH>))

    This can be demonstrated using the following program.

    open(my $in, '<', 'file.dat') or die("Unable to open input file: $!\n"); while (my $line = <$in>) { if ($line) { print("true\n"); } else { print("true for 'while', but false for 'if'\n"); } }

    outputs

    true for 'while', but false for 'if'

    when a file.dat contains the lone character zero (0).

    By the way, overloading aside, only the following evaluate to false: numerical expressions evaluating to zero, the string '0', the empty string '' and the undefined value. 0.0 and 0E0 are the same as 0, but neither '0.0' nor '0E0' are the same as '0'.

      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.

        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
        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found