Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: Automatic Loop Counter not in perlby Joost (Canon) |
on Aug 21, 2007 at 10:50 UTC ( [id://634050]=note: print w/replies, xml ) | Need Help?? |
One thing I don't like about your proposed variable is that as far as I can see, you'd have to do additional assignment of the $LOOPCOUNTER variable if you want to nest loops.
I prefer the way more "functional/OO" languages like javascript and ruby implement foreach loops using callbacks with explicit parameters. Something that's easy to implement in perl for fullblown objects: If perl's prototypes would work for methods, you could build something very close to ruby's
For completeness, here's the javascript version: Note that ruby & javascript's versions also have the advantage of being easily overridable for any object (including "standard arrays", since both allow methods to be attached to objects instead of only to "object types" / classes - and, of course, in both languages arrays are always "real" objects). updated: fixed typo, slight cleanup of wording.
In Section
Meditations
|
|