Whenever you use while (my $line = readline $fh), perl always adds defined if you haven't done so already. I think that this inconsistency between code and the ops that are actually executed is worse and causes people unfamiliar with Perl to think the program will not give that last blank or 0 line.

I am used to writing while (defined(my $line = readline $fh)) and will continue to write it like this. It does not cause any bug, what it does is crystal clear and the 9 extra characters won't hurt anyone.

Whenever I see the defined thing missing, I add it for clarity.

I read your post a couple of times, but the only argument I see for getting rid of "defined" is that it is not necessary. I'd understand your plea if defined would cause strange bugs, but it doesn't. It usually prevents them. Do you think we should also forget all about whitespace as well, just because it is not necessary?

And here we are. Years later and defined has been typed much more often in conditionals to avoid warnings than together with while. Those warnings ("Use of uninitialized value") are there for a reason. The very same reason that testing for definedness is usually a GOOD idea. It usually takes less than a second to type, and saves a lot of time when debugging. Perl's adding defined automatically to while is for me not a reason to not use it.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


In reply to Re: To Kill a Meme: while(defined($line = <>)) by Juerd
in thread 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.