in reply to Re: iterator w/ wantarray()
in thread iterator w/ wantarray()

Problem solved

I realized that it is a closure!

You are right. The sub counter is modified like this and it works OK.
Thank you for your help.

# # My fixed counter() # sub counter { my ($from, $to, $step)= @_; $step ||= 1; return sub { return if $from > $to; my $value=$from; $from +=$step; return $value; }; }
-- cmic. Life helps. Perl Too.

Replies are listed 'Best First'.
Re^3: iterator w/ wantarray()
by tobyink (Canon) on Apr 27, 2020 at 07:09 UTC

    This would be a more normal way of doing it. Don't change $from but close over $value.

    # # My fixed counter() # sub counter { my ($from, $to, $step) = @_; $step //= 1; my $value = $from; return sub { return if $value > $to; $value += $step; }; }