![]() |
|
more useful options | |
PerlMonks |
How can I create an iterator that works inside foreach()by PUCKERING (Sexton) |
on Nov 04, 2022 at 21:55 UTC ( #11147970=perlquestion: print w/replies, xml ) | Need Help?? |
PUCKERING has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to create an iterator that will work inside a foreach (). No problem creating one for a while (), but foreach () eludes me.
I've tried a closure, and a class (with a next method), and even the Iterator module. No joy. Here's an example, cribbed from the Iterator POD: That prints 3, when it should print lines containing 3, 4 and 5. Even the venerable High Order Perl book, which has an entire chapter devoted to iterators and some fine examples, fails to mention anything about using them in foreach -- or not being able to. And while foreach is used within the implementation of some examples, they use cases all use while(). This leads me to think there may be some fundamental reason why it cannot work. Your wisdom and enlightenment, as always, would be much appreciated.
Back to
Seekers of Perl Wisdom
|
|