oakb has asked for the wisdom of the Perl Monks concerning the following question:
Before you complain, yes, I know that this will print an infinite list of odd numbers; please rest assured that my actual application returns an exhaustible iterator.sub increment { my ( $number, $incval ) = @_; return sub { $number += $incval; return $number; } } my $action = increment( 1, 2, 'plus', 'tons', 'more', 'arguments' ); while ( my $result = $action->() ) { print "$result\n"; }
How can I select the correct job/structure subroutine, send all necessary arguments, and get back a usable iterator?sub do_something { my $action = shift( @_ ); for ( $action ) { when ( /INCREMENT/ ) { \&increment( @_ ) } default { die "You idiot!" } } } sub increment { my ( $number, $incval ) = @_; return sub { $number += $incval; return $number; } } my $action = do_something( 'INCREMENT', 1, 2, 'long', 'list' ); while ( my $result = $action->() ) { # <-- The error happens here print "$result\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Choosing between multiple closures
by Eily (Monsignor) on Jun 12, 2014 at 07:42 UTC | |
by oakb (Scribe) on Jun 12, 2014 at 08:00 UTC | |
|
Re: Choosing between multiple closures
by Anonymous Monk on Jun 12, 2014 at 07:39 UTC | |
|
Re: Choosing between multiple closures
by Anonymous Monk on Jun 12, 2014 at 14:34 UTC | |
|
Re: Choosing between multiple closures
by Anonymous Monk on Jun 12, 2014 at 07:35 UTC | |
by oakb (Scribe) on Jun 12, 2014 at 07:47 UTC | |
by Anonymous Monk on Jun 12, 2014 at 07:59 UTC | |
by Anonymous Monk on Jun 12, 2014 at 07:54 UTC |