in reply to Alternative to varvarname
"The subroutines are also machine generated" hints to me that all those subroutines are similar. So without any deeper reasons as to why not, I would propose removing the machine generated code and replacing it with a few, parametrized subroutines. If you don't want to do that, here's how you can easily hang yourself by creating horrible code that is short of goto EXPR:
use strict; use Data::Dumper; ... my %results; foreach my $name (@diffarray) { my $code_name = "${name}_diff"; no strict 'refs'; my $code = \&{ $code_name }; $results{ $name } = [ $code->() ]; }; print Dumper \%results;
Your error comes from the fact that you are not properly interpolating the names of the subroutines.
Of course, you should really avoid the construction of symbolic references, for all the reasons that varvarname lists. In your case, this is done by using (for example) a second hash that lists all the subroutines and the names under which they may be accessed:
use strict; use Data::Dumper; ... my %results; my %dispatch = ( test => \&test_diff, info => \&info_diff, path => \&path_diff, ... ); foreach my $name (@diffarray) { if ( ! exists $dispatch{$name}) { die "Don't know how to handle name '$name'."; }; my $code = $dispatch{$name}; $results{ $name } = [ $code->() ]; }; print Dumper \%results;
Doing it this way, the code becomes cleaner and you get more convenient error handling.
|
|---|