in reply to Re: To Kill a Meme: while(defined($line = <>))
in thread To Kill a Meme: while(defined($line = <>))
It usually prevents them.
Usually, it doesn't do anything.
Even in the cases where the while doesn't check for definedness, like when you add something to the loop condition, it still usually doesn't do anything.
In order for it to do anything at all, you have to be either A) using a perl <= 5.00404 or B) doing something more complex in your while loop than a simple assignment from a filehandle read and one of the following must be true: 1) you are reading fixed length one character records and you retrieve a record containing a single '0', 2) you have set $/ to a string containing one '0', or 3) the last line of a file you are reading must contain a single '0' and not be terminated with $/.
If you need to code defensively for those conditions, go right ahead. Most people don't most of the time.
Besides, you seem to have a good understanding of why you add it. Many don't. Many people write it that way because they have the vague notion that they need to for some reason. They don't add it for clarity.
I, for one, think it does very little to add clarity. I think that seeing code with an explicit defined() like that has caused people unfamiliar with Perl to think the program will not give that last blank or 0 line when the explicit defined() is missing. This is confusing because, in %99.99 of the cases, the definedness check doesn't matter at all regardless of whether it is implicit or explicit. The program would go merrily on its way if the check didn't even exist.
As theorbtwo said in another post, some people are of the mind that being explicit is always better. I do understand that train of thought even if I've rarely taken it to the extreme of spelling out r-e-a-d-l-i-n-e. There are some benefits to being explicit but I think that being explicit all of the time just for the sake of — well, for the sake of being explicit — misses the point.
One of my favorite things about Perl is that I don't have to be explicit. That's a welcome relief from the languages I started out with.
It's great to be able to think about the parts of a problem that interest me and to let perl take care of the parts that don't. Sure, I've got to know enough about Perl to fix things when problems arise. And I do. But, in practice, relatively few problems have come up as a result of perl not handling its parts intuitively. With Perl, a much higher proportion of my debugging time is spent on higher level problems than with languages where being explicit is a requirement, not an option.
With Perl's expressive power, code can be focused tightly on the problem and elegant and succinct solutions are normal. I don't see any reason to dilute code by being explicit about such minor things as definedness checks when it just isn't necessary.
Edit: The anonymonk post below is absolutely correct, and I have no excuse other than fatigue. I've edited the text above for correctness.
-sauoq "My two cents aren't worth a dime.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: To Kill a Meme: while(defined($line = <>))
by Juerd (Abbot) on Nov 03, 2003 at 09:42 UTC | |
|
Re: Re: Re: To Kill a Meme: while(defined($line = <>))
by Anonymous Monk on Nov 03, 2003 at 09:37 UTC |