in reply to Re^4: Tie::File, is untie file handle must?
in thread Tie::File, is untie file handle must?
Well, I can't remember what is supposed to happen with Perl, but it definitely isn't something a C/C++ coder would do. And it will look better when it's revisited years later. I haven't tried the idiom as the OP has it. Doesn't Perl squawk about the variable being undefined if using strict?
Update:
Thanks, 1nickt. Here is the explanation I was looking for (from p. 72 of Perl 6 Now: The Core Ideas Illustrated with Perl 5 by Scott Walters):Perl 5 throws you a Use of uninitialized value when you try to use a variable that contains undef, at least for most operations. use strict; use warnings; my $i; $i++; is acceptable; it was decided that trying to increment undef obviously means that undef should be interpreted as 0 and is common enough not to require explicitly declaring the variable. String operations, bitwise operations, and most math operations will throw this warning.
So the idiom is okay and will not cause a squawk, but I still maintain that, in code to be shared and used by others, one should initialize the variable, and I think Damian Conway would agree it is a best practice (I'll see if I can find a reference to this situation in his book).
Update 2:
I couldn't find anything specific, so I can't fairly claim a Conway best practice, but I still claim explicitly initializing the counting variable adheres to Prof. Conway's goal of Maintainability where he says, among other things,
...Better yet, try to optimize for comprehensibility: easy-to-read and easy to understand aren't necesarily the same thing.
I shall try not to complain about the iterated-undefined-index-variable idiom again, although I will continue to initialize it in my code as a personal best practice.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Tie::File, is untie file handle must?
by 1nickt (Canon) on Aug 05, 2015 at 04:24 UTC |