我是 Perl 新手,我知道您可以按名称调用函数,如下所示:
&$functionName();
. 但是,我想按名称使用数组。这可能吗?
长代码:
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";
}
}
}
}
寻求与此类似的较短代码:
sub print_species_names {
my $species = shift(@_);
my @cats = ("Jeffry", "Owen");
my @dogs = ("Duke", "Lassie");
foreach (@<$species>) {
print $_ . "\n";
}
}