in reply to Basic question about Iterator code
my $value = $it->();
instead of
in the first sample.my $value->it();
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; }
|
---|