Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: How can I create an iterator that works inside foreach()by hv (Prior) |
on Nov 05, 2022 at 00:14 UTC ( [id://11147974]=note: print w/replies, xml ) | Need Help?? |
The significant difference between foreach (EXPR) BLOCK and while (EXPR) BLOCK is that foreach evaluates the expression once in list mode getting a single list of results, then invokes BLOCK over the items in that list in turn; on the other hand, while evaluates the expression once (in scalar mode) and immediately invokes BLOCK on that value, then evaluates EXPR again to get another value, repeating until EXPR gives a false value. The nature of an iterator is that it is designed to be called repeatedly, returning a single result each time. So if you're making an iterator, it won't be designed to work with foreach - you would need instead to have a function that returns a single list of all the values. So the next question is: why do you want to do this, what problem are you trying to solve?
In Section
Seekers of Perl Wisdom
|
|