Usage of defined with Filehandle and while Loop

后端 未结 3 358
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 20:30

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

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

Is there

3条回答
  •  借酒劲吻你
    2021-01-20 21:17

    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($_=)) 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 = )) {
        print $s;
    }
    -e syntax OK
    

    but sometimes it won't:

    $ perl -MO=Deparse -e 'if($s=<>) { print $s }'
    if ($s = ) {
        print $s;
    }
    -e syntax OK
    
    $ perl -MO=Deparse -e 'while(some_condition() && ($s=<>)) { print $s }'
    while (some_condition() and $s = ) {
        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.

提交回复
热议问题