Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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??


in reply to How can I create an iterator that works inside foreach()

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147974]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found