I just tried this from the command line:
my $name = "do"; sub do { print "I am doing it\n"; } &$name;
It printed "I am doing it". There is more than one way to do it:
my $name = "do"; sub do { print "I am doing this now\n"; } $name->();
Notice that since both methods use symbolic references, they fail under use strict;. Another approach is:
$name = \& {"main::$name"}; &$name;
That depends on being in the main package, however. It does work under use strict.

A better approach might be building a hash of references to subroutines:

sub foo { print "Doing foo (whatever that means)\n"; } sub bar { print "Now doing bar (isn't this fun?)\n"; } my %sub_table = ( 'foo' => \&foo, 'bar' => \&bar, ); foreach ( qw (foo bar foobar ) ) { $sub_table{$_}->(); }
Nicer, isn't it?

In reply to Re: Variable Sub Calls by chromatic
in thread Variable Sub Calls by Anonymous Monk

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.