in reply to Re^5: Using lazy list with a for loop
in thread Using lazy list with a for loop

That has all the problems of the each builtin.

Better to have a generator creator and user.

my $iter = $api->get_widgets_iter(); while ( my ( $widget ) = $iter->() ) { ... }

Replies are listed 'Best First'.
Re^7: Using lazy list with a for loop
by LanX (Saint) on Jan 03, 2023 at 15:24 UTC
    I agree there are some edge cases to consider depending on the use-case... and I don't know the OPs use cases good enough.

    But because of cleaner scope and loop-control statements I'd rather prefer

    for (my $iter = get_iter(); my ( $widget ) = $iter->(); ) { ... }

    over

    my $iter = get_iter(); while ( my ( $widget ) = $iter->() ) { ... }

    This approach will put $iter in a tight scope and properly destroy.

    Needless to say, if Perl had macros like Lisp, we wouldn't need to repeat that discussion again and again...

    Keyword-simple could solve this, but is restricted to statements only.

    see also

    Can I check if a loop's scope is entered for the first time?

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      I agree there are some edge cases to consider depending on the use-case...

      Code that fails subtly when an exceptions or next/last is occurs? It's not a question of if it's going to bite you, but when.