"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.


In reply to Re: Alternative to varvarname by Corion
in thread Alternative to varvarname by tsk1979

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.