Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Basic question about Iterator code

by choroba (Cardinal)
on May 12, 2025 at 14:18 UTC ( [id://11165017]=note: print w/replies, xml ) Need Help??


in reply to Basic question about Iterator code

You probably meant
my $value = $it->();

instead of

my $value->it();
in the first sample.

As for the error: in order to use the block syntax with a (&)-prototyped subroutine, you need to declare the subroutine before you use it. Move the last line to the top of the second sample and everything should work.

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; sub Iterator(&) { $_[0] } sub upto { my ($m, $n) = @_; return Iterator { $m <= $n ? $m++ : undef } } my $it = upto(3,5); while (my $value = $it->()) { say $value; }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-06-16 03:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.