in reply to Re: To Kill a Meme: while(defined($line = <>))
in thread To Kill a Meme: while(defined($line = <>))

But don't forget (which is the point of sauoq's rant) that Perl automatically checks defined when you're assigning from a filehandle to a scalar in a while loop condition.
$ perl -MO=Deparse -e'while($line=<>){}' while (defined($line = <ARGV>)) { ; } -e syntax OK

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^2: To Kill a Meme: while(defined($line = <>))
by pg (Canon) on Nov 03, 2003 at 05:42 UTC

    Update:

    You missed the real point. This has nothing to do with whether Perl "automatically" checks undef. The real point here is that, under a normal situation without set $/, you don't get a real empty string or a "0" back, so those posibilities are eliminated, and this leaves undef the only condition that may evaluate $line to false, thus the use of "defined" is not neccessary here.

    A placehold for original:

    I realized the confusion I had in the original reply, and removed it. Then I saw sauoq had already replied.

    It would be okay if there was no reply, but it is immoral to make his reply funny ;-), especailly when he is right. So I leave this comment here to confirm what he said is true, and that was my confusion in my original post.

      You missed the real point. This has nothing to do with whether Perl "automatically" checks undef. The real point here is that, under a normal situation without set $/, you don't get a real empty string or a "0" back, so those posibilities are eliminated, and this leaves undef the only condition that may evaluate $line to false, thus the use of "defined" is not neccessary here.

      But it is possible to get a false value back without changing $/, if the last "line" consists of a single 0 with no terminating newline. And you can see the effect of losing the defined check if you add something else to the conditional (perl only supplies the defined test when the condition is a plain read operator or read operator with assignment (control D to end input without final newline):

      :~$ cat > OOPS 3 2 1 0^D :~$ perl -e '$foo = 42; while($foo and $_ = <>){print}' OOPS 3 2 1 :~$ :~$ perl -e '$foo = 42; while($foo and defined($_ = <>)){print}' OOPS 3 2 1 0:~$

      So the possibility of losing data without defined exists regardless of what value is assigned to $/.

      I have to admit it... you just thoroughly confused me with that post.

      Of course it is valid to get an undef! Otherwise you'd never get out of the loop. The loop checks for definedness.

      But why go to the trouble of writing a client and server to show it?

      $ perl -le '1 while ($l = <>); print "undef!" unless defined $l' < /de +v/null undef!

      -sauoq
      "My two cents aren't worth a dime.";