The call to defined() in

while( defined( my $line = <FH> ))
is almost never necessary.

After including a couple paragraphs about it in a reply to a node on a completely different topic, I decided to SuperSearch a bit to see just how common it is. After searching for a couple variations in the spacing, I found dozens of recent nodes that contained something similar to this construct. Some were replies and some were probably necessary uses of defined() without the diamond operator, but I suspect many people are still using this under the misconception that it is needed.

It isn't.

I can hear some monks asking, "Okay... then why is it used so often?" Some others are searching their hazy memories and murmuring something vague about the horror of lines that evaluate to false. Still others are pointing to five year-old apache error logs filled with warnings.

So, why is it used?

Because a warning present in 5.004_04 filled our heads with silly notions of lines evaluating to "0" and punished our well-developed sense of laziness with huge error logs until we joined in the chant. "While defined. While defined. While defined!" See, perl told us what we needed to do...

$ perl5.00404 -we 'print $line while ($line = <>)' Value of <HANDLE> construct can be "0"; test with defined() at -e line + 1.

Sure, the warning was added for a reason. Perl had a little inconsistency. Though the construct while( <FH> ) was equivalent to while( defined( $_ = <FH> ) ), there was no automatic check for definedness if you assigned the result of the diamond operator within the while loop. So, while( $line = <FH> ) was not equivalent to while( defined( $line = <FH> ) ) as might have been expected.

Of course, this was quickly corrected and the warning was removed. (I believe the warning was gone by 5.004_05.) But 5.004_04 was pretty stable, the perl community had really started to grow, and that unfortunate warning got a lot of "play".

Here's the kicker: the bug we were being warned about was actually pretty obscure. Most perl code would have never been affected by it anyway. It was not, for instance, at all like the warning indicating a possible typo when a variable was only used once. That warning is usually helpful. This one usually wasn't. No one was flooding IRC channels and mailing lists with questions about while loops prematurely ending, not even the usual crowd of newbies who were constantly forgetting to include that -w on their shebang line.

Usually, checking the truth of the value returned by the diamond is sufficient. This is because the input record separator is included in the value and strings like "\n" and "0\n" are true values. An error could only occur if the value returned was actually false. That could happen if you changed $/ to a string which was itself false, like '0'. But how often do you do that? The only other time, that I can think of at least, that this could cause a problem is with a file where the last line is a '0' (or a string of them) and, for whatever reason, isn't followed by a newline. I'd guess that was more common, though still not very.

Undoubtedly, a lot of people who were introduced to perl with 5.004_04 took that warning as law without really understanding why. Others who did understand the issues but liked to run with warnings enabled just coded around the problem; and over time even some of them (myself included) forgot exactly why.

And here we are. Years later and defined has probably been typed at least a million times unnecessarily accounting for over 7 million keystrokes and almost a man year of productivity. :-)

Unless you are still supporting old versions of perl, please, try to lay this old habit to rest.

Note that there are still plenty of places where it makes sense to use defined inside a while loop. This is specifically a plea to avoid it in cases where you are reading from a filehandle like this: while( defined( $line = <FH> ) ) and nothing more.

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

In reply to To Kill a Meme: while(defined($line = <>)) by sauoq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.