Search code examples
perlwhile-loopfilehandle

Usage of defined with Filehandle and while Loop


While reading a book on advanced Perl programming(1), I came across this code:

while (defined($s = <>)) {
    ...

Is there any special reason for using defined here? The documentation for perlop says:

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: [...]

So, would there be a corner case or that's simply because the book is too old and the automatic defined test was added in a recent Perl version?


(1) Advanced Perl Programming, First Edition, Sriram Srinivasan. O'Reilly (1997)


Solution

  • Perl has a lot of implicit behaviors, many more than most other languages. Perl's motto is There's More Than One To Do It, and because there is so much implicit behavior, there is often More Than One Way To express the exact same thing.

    /foo/ instead of $_ =~ m/foo/

    $x = shift instead of $x = shift @_

    while (defined($_=<ARGV>)) instead of while(<>)

    etc.

    Which expressions to use are largely a matter of your local coding standards and personal preference. The more explicit expressions remind the reader what is really going on under the hood. This may or may not improve the readability of the code -- that depends on how knowledgeable the audience is and whether you are using well-known idioms.

    In this case, the implicit behavior is a little more complicated than it seems. Sometimes perl will implicitly perform a defined(...) test on the result of the readline operator:

    $ perl -MO=Deparse -e 'while($s=<>) { print $s }'
    while (defined($s = <ARGV>)) {
        print $s;
    }
    -e syntax OK
    

    but sometimes it won't:

    $ perl -MO=Deparse -e 'if($s=<>) { print $s }'
    if ($s = <ARGV>) {
        print $s;
    }
    -e syntax OK
    
    $ perl -MO=Deparse -e 'while(some_condition() && ($s=<>)) { print $s }'
    while (some_condition() and $s = <ARGV>) {
        print $s;
    }
    -e syntax OK
    

    Suppose that you are concerned about the corner cases that this implicit behavior is supposed to handle. Have you committed perlop to memory so that you understand when Perl uses this implicit behavior and when it doesn't? Do you understand the differences in this behavior between Perl v5.14 and Perl v5.6? Will the people reading your code understand?

    Again, there's no right or wrong answer about when to use the more explicit expressions, but the case for using an explicit expression is stronger when the implicit behavior is more esoteric.