Search code examples
perlsymbolic-references

In Perl, can refer to an array using its name?


I'm new to Perl and I understand you can call functions by name, like this: &$functionName();. However, I'd like to use an array by name. Is this possible?

Long code:

sub print_species_names {
    my $species = shift(@_);
    my @cats = ("Jeffry", "Owen");
    my @dogs = ("Duke", "Lassie");

    switch ($species) {
        case "cats" {
            foreach (@cats) {
                print $_ . "\n";
            }
        }
        case "dogs" {
            foreach (@dogs) {
                print $_ . "\n";
            }
        }
    }
}

Seeking shorter code similar to this:

sub print_species_names {
    my $species = shift(@_);
    my @cats = ("Jeffry", "Owen");
    my @dogs = ("Duke", "Lassie");

    foreach (@<$species>) {
        print $_ . "\n";
    }
}

Solution

  • Possible? Yes. Recommended? No. In general, using symbolic references is bad practice. Instead, use a hash to hold your arrays. That way you can look them up by name:

    sub print_species_names {
        my $species = shift;
        my %animals = (
            cats => [qw(Jeffry Owen)],
            dogs => [qw(Duke Lassie)],
        );
        if (my $array = $animals{$species}) {
            print "$_\n" for @$array
        }
        else {
            die "species '$species' not found"
        }
    }
    

    If you want to reduce that even more, you could replace the if/else block with:

        print "$_\n" for @{ $animals{$species}
            or die "species $species not found" };