![]() |
|
Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^3: How can I create an iterator that works inside foreach() (updated)by hv (Prior) |
on Nov 05, 2022 at 04:04 UTC ( #11147983=note: print w/replies, xml ) | Need Help?? |
In the context of that website, it's possible that the intent is something more like the C-style for loop, in which case something like the following might be a better fit:
(But actual idiomatic perl would use a while loop here.) If the intent is to handle a list-style for loop, the "iterator" can be made much simpler by handling just that case, which I think is similar to the Ruby solution shown:
In the current example perl code at the link, the "_upto" function is not needed, it can simply be replaced with "sub", as in my examples above: the sub keyword in this context yields an anonymous subroutine reference from the block that follows it, and Higher Order Perl should be telling you all about that. The comment "To work in a foreach each loop, inner sub upto must be predeclared to take an anonymous sub argument" is wrong - if we have a subroutine reference, $it->() will happily invoke it, whether it's in a for loop or not.
In Section
Seekers of Perl Wisdom
|
|