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

Very simply, I have a scalar variable, $filename, read from a config file, and I want to call a subroutine whose name is identical to the value of the variable. (The subroutine extracts data from a database, formats it, and prints it to the file, $filename.) I am using strict and this code yields a syntax error. I've tried calling it as &$i($dbh), and many other variations, but nothing has worked. Here is what I "want" to do:
sub filename($){ #do some stuff } $filename = somefile.ext $filename($dbh);
I've tried every way I can to no avail. Please show me the way...Thanks! A (very) humble Apprentice

Replies are listed 'Best First'.
Re: How to use a scalar variable to call a subroutine?
by chromatic (Archbishop) on Feb 05, 2001 at 23:13 UTC
    Others have warned you that this is a bad idea, though no one as of yet has explained why.

    If someone malicious guesses the name of your subroutine, he can execute anything at will. That could be bad.

    More likely, if there's a typo, it could call an undefined subroutine, which will have unexpected results. strict mode warns you about typos and other hazardous constructs to reduce these nasty surprises.

    The oddly-named Re: use slack; presents one solution. Another would be to call UNIVERSAL::can() on an object, if you're using an object.

    Otherwise, you might look through the package symbol table for a named glob, then check it for a code reference and call the code that way. It still requires disabling strict references, but it is a little safer and you can earn a Perl merit badge for knowing how:

    sub get_coderef { my $subname = shift; my $coderef; no strict 'refs'; $coderef = *{"$subname"}{CODE}; return $coderef if ($coderef and defined(&$coderef)); }
    Add the following to test:
    #!/usr/bin/perl -w use strict; sub do_this { print "doing this!\n" } sub do_that { print "doing that!\n" } my $code = get_coderef('do_this'); $code->(); $code = get_coderef('do_that'); $code->(); $code = get_coderef('do_nothing'); if (defined($code)) { $code->(); } else { print "Not defined!\n"; }
Re: How to use a scalar variable to call a subroutine?
by mirod (Canon) on Feb 05, 2001 at 22:49 UTC
    sub mysub { print "mysub $_[0]\n"; }; my $s="mysub"; &{$s}( "toto"); # This is what you want

    Note that this is a bad idea though, as it breaks under strict and does not give you any control over which sub is called.

    A better way is to use a hash to link a string to a subroutine:

    my %h=( mysub1 => \&mysub_1, # \&mysub_1 is a reference to the subrou +tine mysub2 => \&my_sub_2); my $s="mysub1"; # or however you get the string if( $h{$s}) { $h{$s}->( "toto"); } # this is how you call the subroutine wit +h its arguments else { die "wrong sub called: $s"; } sub mysub_1 { print "mysub 1 $_[0]\n"; }; sub mysub_2 { print "mysub 2 $_[0]\n"; };
Re: How to use a scalar variable to call a subroutine?
by Fastolfe (Vicar) on Feb 06, 2001 at 00:56 UTC
    I might approach this by using tokens and associating them with their respective coderefs:
    my %CODE = ( one => \&one, two => \&two, three => \&three, _default => \&not_found, ); my $input = whatever; ($CODE{$input} || $CODE{_default})->(@arguments);
Re: How to use a scalar variable to call a subroutine?
by kilinrax (Deacon) on Feb 05, 2001 at 22:45 UTC
    In order to use a 'soft' reference such as this, you'll have to turn off strict refs:
    #!/usr/bin/perl -w use strict; sub filename($){ print "it worked!\n"; } my $dbh; my $filename = 'filename'; no strict 'refs'; &{$filename}($dbh); use strict 'refs';
Re: How to use a scalar variable to call a subroutine?
by jeroenes (Priest) on Feb 05, 2001 at 22:48 UTC
    Read perlref. You need to do something like &{$filename}. BTW, your example code won't work anyway.
    sub mysub($) { my $str = shift; #code } $filename = 'somefile.ext'; open IN, $filename; $subref = <$filename>; # maybe it's 'mysub'? chomp $subref; no strict 'refs'; no strict 'subs'; &{$subref}( 'text' );
    Hope this helps,

    Jeroen
    "We are not alone"(FZ)