Robert Zielfelder wrote: > > I have a script that uses an array to determine weather or not a subroutine > needs to be run or not. I want to be able do a foreach on the array and > invoke the subroutine using the control variable. The names of the > subroutines are the same as the items inside the array. This is what I have > so far: > > .... > foreach $sub (@list_of_subs) { > &{$sub}; ##-- this is the part I am stuck on. This doesn't work > } > .... > > I have a bunch of subroutines defined in the script, but they don't need to > be invoked unless the "@list_of_subs" contains the name of the sub. I know > that I could stick a bunch of if statements in there and make this work, but > I am trying to be a little more efficient if I can. Any insight would be > appreciated.
One way to do it: $ perl -e' my @array = ( sub { print "one: $_[0]\n" }, sub { print "two: $_[0]\n" }, sub { print "three: $_[0]\n" }, sub { print "four: $_[0]\n" }, ); for my $sub ( @array ) { $sub->( ++$a ); } $array[0]->( "end" ); ' one: 1 two: 2 three: 3 four: 4 one: end Another way to do it: $ perl -e' sub one { print "one: $_[0]\n" } sub two { print "two: $_[0]\n" } sub three { print "three: $_[0]\n" } sub four { print "four: $_[0]\n" } my @array = ( \&one, \&two, \&three, \&four ); for my $sub ( @array ) { $sub->( ++$a ); } $array[0]->( "end" ); ' one: 1 two: 2 three: 3 four: 4 one: end John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]