* Tom Christiansen <tchrist@perl.com> [2010-04-04 00:40]: > Yes, it's somewhat clearer to write: > > for my $tick (reverse 1 .. 10) { ... } > > for our $Arg (@ARGV) { ... } > > And I do admittedly do this in my own code, except perhaps for > implicit $_. As do I. > However, I truly do not believe cluttering every example with > a declaration for each variable used makes it clearer or > better. I never talked about that. My point was purely about `foreach`; and I am glad to hear we essentially agree on that issue. > An example that demonstrates, say, the abs($x) function, should > be able to be written > > $x >= 0 ? $x : -$x > > without any needlessly distracting declarations. > > Eh? Sure, one can’t declare `$x` en passant there. > ######################## > > Similarly, > > dbmopen(%HIST,'/usr/lib/news/history',0666); > while (($key,$val) = each %HIST) { > print $key, ' = ', unpack('L',$val), "\n"; > } > dbmclose(%HIST); > > is not improved by being written > > dbmopen(my %HIST,'/usr/lib/news/history',0666); > while (my($key,$val) = each %HIST) { > print $key, ' = ', unpack('L',$val), "\n"; > } > dbmclose(%HIST); Yes it is. Modulo coding style, that is certainly how I would write it if I were doing this in real code. Funnily enough what I wrote about `foreach` applies here: `%HIST` is shadowed for the duration of the tie, so the behaviour with a predeclared variable is unlikely to be useful. Likewise, I am extremely unlikely to ever write that `while` loop using previously declared variables (unless it’s `($a, $b)` because I’m writing a one-liner). > Here's an example of gratuitous my() confusing matters: > > my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); > print "$abbr[$mon] $mday"; > # $mon=9, $mday=18 gives "Oct 18" > > I might have written that as > > @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); > print "$abbr[$mon] $mday\n"; # use old $mon, $mday vals > # $mon=9, $mday=18 gives "Oct 18" I think it is implied there that the variables are those introduced in the previous code example. A `my` in front of that one would help, admittedly. -- *AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1} &Just->another->Perl->hack; #Aristotle Pagaltzis // <http://plasmasturm.org/>Thread Previous | Thread Next