in reply to Caveats when using anonymous subs as subroutine arguments
Granted it may be a relic of your sample simplification, but in this case I might had factored out the common bits into a helper that looks up the exact arguments from a hash instead (and then sets up methods for each task based on the contents of the hash).
my %helper_args = ( Task1 => [ qw(ARG1 ARG2 ARG3) ], Task2 => [ qw(ARG6 ARG2 ARG1 ARG5) ], ## ... ); for my $task ( keys %helper_args ) { *{"Do$task"} = sub { $_[0]->_common_runner( $task, @_[1..$#_] ) }; } sub _common_runner { my $self = shift; my $task = shift; ## "Task1", "Task2", ... my @rest = @_; if( !$self->TaskIsDone() and (my( $setup ) = $self->can( "Setup$task +" )) ) { $self->$setup(); } my @args = @{ $helper_args{ $task } || [] }; $self->CommonTask( @args, @rest ); }
Alternately create a common base class and then have that base class' CommonTask calls methods on itself (with the base class' implementations being noops / stubs); in other words the Gang Of Four "Template Method Pattern".
Update: Oh, and to actually answer your question no that's not a totally bletcherous solution and that is one of the "accepted" usages of prototypes (to get new keyword-like syntax).
The cake is a lie.
The cake is a lie.
The cake is a lie.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Caveats when using anonymous subs as subroutine arguments
by Glav (Novice) on Dec 02, 2008 at 01:56 UTC |