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

I've seen cases where the defined() test was missing where it should have been.

I'm curious by what criteria you judge that it "should have been." Do you mean that it should be there as a matter of good defensive programming style? Or was there a real likelihood that an error would have resulted eventually? And, if so, what error?

-sauoq
"My two cents aren't worth a dime.";
  • Comment on Re: Re: To Kill a Meme: while(defined($line = <>))

Replies are listed 'Best First'.
Re: To Kill a Meme: while(defined($line = <>))
by Abigail-II (Bishop) on Nov 03, 2003 at 11:44 UTC
    I'm curious by what criteria you judge that it "should have been." Do you mean that it should be there as a matter of good defensive programming style?

    Nothing to do with style, but with correctness. Stuff like:

    while (($line = <>) && $line =~ /\S/) { ... } while (($line1 = <>) && ($line2 = <>)) { ... }
    Also, the standard way of translating a while to a for(;;) loop won't work:
    for (;$line = <>;) { ... }
    And even:
    next unless $line = <>;

    They are all cases where a defined test should be used, because otherwise you may get unwanted effects.

    Abigail