cmic has asked for the wisdom of the Perl Monks concerning the following question:
I was using a code from "Perl Hacks", hack-27, but it doesn't works as
expected, and I don't know why.
Any one can explain what it should yield ?
Or am I missing something?
-- cmic. retired sysadmin and Perl user..#!/usr/local/bin/perl use strict; use warnings; use Want 'howmany'; sub multi_iterator { my ($iterator)=@_; return sub{ my $context=wantarray(); return unless defined $context; return $iterator->() unless $context; return map { $iterator->() } 1 .. howmany(); }; } sub counter { my ($from, $to, $step)= @_; $step ||= 1; return sub { return if $from > $to; my $value=$from; $value +=$step; return $value; }; } #main my $counter=counter(1, 10, 3); my $iterator= multi_iterator($counter); my ($un, $deux)=$iterator->(); my $trois=$iterator->(); #should print 1, 4, 7 print "Hack-27 ", $un, ",", $deux, ",", $trois, "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: iterator w/ wantarray()
by haukex (Archbishop) on Apr 25, 2020 at 19:17 UTC | |
by cmic (Acolyte) on Apr 25, 2020 at 21:00 UTC | |
by tobyink (Canon) on Apr 27, 2020 at 07:09 UTC | |
|
Re: iterator w/ wantarray()
by stevieb (Canon) on Apr 25, 2020 at 19:00 UTC | |
by jo37 (Curate) on Apr 25, 2020 at 19:51 UTC | |
by stevieb (Canon) on Apr 25, 2020 at 21:38 UTC | |
by jo37 (Curate) on Apr 26, 2020 at 12:48 UTC | |
by stevieb (Canon) on Apr 26, 2020 at 17:58 UTC | |
|