glytchbinary has asked for the wisdom of the Perl Monks concerning the following question:

i have a variable that will contain the name of a function i want to run. how do i get it to run? i have sub func {...} and a variable that will have a string "func" is there an easy way to run a function that has the same name as a string variable's contents?
  • Comment on calling functions from data in variables

Replies are listed 'Best First'.
Re: calling functions from data in variables
by broquaint (Abbot) on Aug 08, 2003 at 14:28 UTC
    Your best bet is a dispatch table, which is a hash where the keys are the names of the subroutines you want to execute and the values are the subroutine references (in perl at least) e.g
    sub foo { print "in foo\n" } my %dispatch = ( foo => \&foo, bar => sub { print "in bar\n" }, ); my @data = qw/ foo bar /; $dispatch{$_}->() for @data; __output__ in foo in bar
    Or if you're feeling particularly devilish you can just abuse the lack of checking for strictures when creating subroutine references e.g
    sub foo { print "in foo\n" } (\&{"foo"})->(); __output__ in foo
    Ok, so you probably wouldn't want to do it like that, but it's always an option ;)
    HTH

    _________
    broquaint

Re: calling functions from data in variables
by ctilmes (Vicar) on Aug 08, 2003 at 14:28 UTC
    if $var = 'func'; just call &$var();

    but don't do that. It will complain (as well it should) under use strict;

    Use a dispatch table instead:

    use strict; sub func { print "func ran\n"; } my %dispatch = ( func => \&func ); my $var = 'func'; &{$dispatch{$var}}();
Re: calling functions from data in variables
by tall_man (Parson) on Aug 08, 2003 at 14:44 UTC
    Another possibility is to make use of the object-oriented capabilities of perl. Objects are a more extensible approach because you don't have to keep adding to a dispatch table. The restriction is that the function names must be the same, but within different packages.
    use strict; package A; sub doit { print "Called A\n"; } package B; sub doit { print "Called B\n"; } my $ref = []; bless $ref,"A"; $ref->doit(); bless $ref,"B"; $ref->doit();
Re: calling functions from data in variables
by glytchbinary (Initiate) on Aug 08, 2003 at 14:24 UTC
    nevermind.... i feel dumb. eval()...
Re: calling functions from data in variables
by BrowserUk (Patriarch) on Aug 08, 2003 at 14:36 UTC

    Update: I managed to fool myself but not broquiant. The -MStrict below is completely useless as there is no module Strict, only strict. So the following will only work without stricture. Which is one reason why there is probably a better way to do this.

    I wish there was a switch to enable stricture!

    You could do

    P:\test>perl -wMStrict -le"sub func{ print 'Hello world'; } my $funcna +me='func'; &$funcname;" Hello world P:\test>perl -wMStrict -le"sub func{ print 'Hello world'; } &{ $ARGV[0 +] };" func Hello world

    but there are probably better ways of achieving your aims, but it would requires a better description of those aims.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.