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

One minor nit: You missed a way to get a false-but-defined value. It's possible if you've set $/=\1, that is, if you're reading a byte at a time.
One minor nit: that won't work, I quote perldoc perlop
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:
You can test if you need convincing ;)
E:\>perl $|=1; $/=\1; print "[$_]" while <>; __END__ abcd [a][b][c][d][ ]0000000000000 [0][0][0][0][0][0][0][0][0][0][0][0][0][ ]^Z
update:
theorbtwo: that was my point, defined is implicit, so it wouldn't matter if it was false
sauoq: I was not responding to the entire meditation, but to theorbtwos response. I do not consider using while(defined($_=<>)) a meme.

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: Re: To Kill a Meme: while(defined($line = <>))
by sauoq (Abbot) on Nov 03, 2003 at 07:53 UTC

    I think you missed his point, if not the point of the whole meditation. The behavior you cite from the docs did not exist in 5.004_04. The fact that it didn't could result in some obscure bugs. I pointed out two ways you could run into trouble because of it. And theorbtwo was trying to point out another one. (He was wrong, but only because setting $/ to a reference to a number in order to read fixed length records was not supported in 5.004_04.)

    You can test if you need convincing and you have perl 5.004_04 ;-)

    $ perl5.00404 -e '$/=\1; print "[$_]" while <>;' abcd 0000 ^D [abcd 0000 ]$
    And if you need some evidence that the documentation you pasted is irrelevant to the case at hand:
    $ perl5.00404 -we'$/="0"; print $l while ($l = <>)' Value of <HANDLE> construct can be "0"; test with defined() at -e line + 1. foo000bar foo0$

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Re: Re: To Kill a Meme: while(defined($line = <>))
by theorbtwo (Prior) on Nov 03, 2003 at 07:52 UTC

    Your snippet isn't doing what you think it is; using -MO=Deparse,-p shows that it really is doing a defined() check.

    $|++; $/=\1; print "[$_]" while ($_=<>, $_) __END___ abcd [a][b][c][d][ ]0000000000000

    (I see no holes with sauoq's response, though.)


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).