Don't ask to ask, just ask | |
PerlMonks |
Re: Iterating over verbatim hash referenceby LanX (Saint) |
on Jan 23, 2010 at 01:45 UTC ( [id://819102]=note: print w/replies, xml ) | Need Help?? |
Easily extending foreach to multiple iterator-vars is a problem in perl5. That's why the for syntax was extended in perl6
Me myself I would love to have a real macro mechanism to solve this properly in perl5, anyway I can suggest you some code to simulate it at least in a functional way
I'm not sure if it works prior to 5.10, IMHO there was an issue about "late" my declarations. Some things to note:
Other approaches¹ solving the repeated LIST creation may be to split functionality onto two functions deal and to such that either for (deal(LIST);to(VARS)) { ... } or while ( deal(LIST) .. to(VARS) ) { ... } work! (But error-handling becomes trickier) In the latter correct return values would make the flip-flop operator (scalar ..) build the list only once!
Cheers Rolf UPDATES: ¹) thats really complicated to realize avoiding a nesting mess, because it's not possible to identify from which codeposition to() is called, the linenumber in caller is not sufficent. ²) maybe the best solution! The call syntax for iterating over a literal hash would be Please note, no (fat) comma needed, because now we are passing code returning a list NOT a hashref! That's analogous to map and grep's syntax.
In Section
Seekers of Perl Wisdom
|
|