4

I’m trying to figure out how to iterate over an array of subroutine refs.

What’s wrong with this syntax?

use strict;
use warnings;

sub yell { print "Ahh!\n"; }
sub kick { print "Boot!\n"; }
sub scream { print "Eeek!\n"; }

my @routines = (\&yell, \&kick, \&scream);
foreach my $routine_ref (@routines) {
  my &routine = &{$routine_ref};
  &routine;
}

Thanks in advance!

cdleary
  • 69,512
  • 53
  • 163
  • 191

3 Answers3

10

In your foreach loop, the following is a syntax error:

my &routine;

Your variable $routine_ref already has a reference to the subroutine, so all you need to do at that point is call it:

for my $routine_ref (@routines) {
    &{$routine_ref};
}

As always with Perl, "There's More Than One Way to Do It." For example, if any of those subroutines took parameters, you could pass them inside parenthesis like this:

for my $routine_ref (@routines) {
  $routine_ref->();
}

Also note that I've used for instead of foreach, which is a best pratice put forth by Damian Conway in Perl Best Practices.

cowgod
  • 8,626
  • 5
  • 40
  • 57
  • I've always preferred the latter example, its less-ambiguous in behaviour to me. ( +1 ) – Kent Fredric Jan 17 '09 at 01:43
  • You probably don't want to dereference it with just the &. Without the parens, that uses the current value of @_ as the implicit argument list, and that's usually never what you mean to do. – brian d foy Jan 17 '09 at 03:09
4
foreach my $routine_ref (@routines) {
        $routine_ref->();
}
0

Try this:

use strict;
use warnings;

sub yell { print "Ahh!\n"; }
sub kick { print "Boot!\n"; }
sub scream { print "Eeek!\n"; }

my @routines = (\&yell, \&kick, \&scream);
foreach my $routine_ref (@routines) {
  &$routine_ref ();
}
Paul Beckingham
  • 14,495
  • 5
  • 33
  • 67