in reply to Re^2: globbing filehandles in arrays
in thread globbing filehandles in arrays
The PBP rationale is (very loosely (and probably poorly :) paraphrasing; see p100-101) that the Perl-style iteration is more explicit in that you can see right off what's being iterated.
## Compare . . . for( my $i = 0; $i < $#array; $i++ ) { frobnitz( $array[ $i ] ); } ## VS for my $item ( @array ) { frobnitz( $item ); }
The first case the fact that it's working with each element of @array isn't as readily apparent (you've got to parse that it's looking at $#array and then match that up with the indexing); whereas the foreach-style comes right out up top and says "I'm frobnitz'ing every item of @array".
Another thing is that in order to use the C-style loops you need to have an actual array to work with, whereas a foreach-style loop will work with any arbitrary list of values (e.g. for my $key ( sort { $h{$b} <=> $h{$a} } keys %h ) { ... }).
Again it's just a style issue and it's not "wrong", per se; it just doesn't read as . . . "Perlish".
The cake is a lie.
The cake is a lie.
The cake is a lie.
|
|---|