cdleary
cdleary

Reputation: 71424

How do I iterate over/dereference an array of subroutine refs in Perl?

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!

Upvotes: 4

Views: 1337

Answers (3)

cowgod
cowgod

Reputation: 8656

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.

Upvotes: 10

Oleksandr Tymoshenko
Oleksandr Tymoshenko

Reputation: 1340

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

Upvotes: 4

Paul Beckingham
Paul Beckingham

Reputation: 14905

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 ();
}

Upvotes: 0

Related Questions