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

Now, I'd be really interested in hearing what you think is the common case, and where people get it wrong by using defined ($line = <>).

The most common case is reading a text file which ends with a newline. And doing so without changing $/.

What people get wrong is that they tend to think they need to check defined() because of blank lines. In other words, they get the mistaken impression that a scalar can contain a newline and be false.

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

Replies are listed 'Best First'.
Re: To Kill a Meme: while(defined($line = <>))
by Abigail-II (Bishop) on Nov 03, 2003 at 11:51 UTC
    What people get wrong is that they tend to think they need to check defined() because of blank lines.

    Really? I've never encountered people thinking that. Why do you think people think this way? Have you ever seen code where people do:

    while ($line = <>) { ... }
    and expect it to read in just one paragraph, and not the entire file?

    Having said that, assuming you are right, it would be better to teach them the proper meaning of defined, instead of discouraging them not to used 'defined'.

    Abigail