in reply to To Kill a Meme: while(defined($line = <>))
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' }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: To Kill a Meme: while(defined($line = <>))
by sauoq (Abbot) on Nov 03, 2003 at 09:26 UTC | |
by Juerd (Abbot) on Nov 03, 2003 at 09:42 UTC | |
by Anonymous Monk on Nov 03, 2003 at 09:37 UTC |