[VPM] Symbolic references with use strict in effect.

Adam Parkin pzelnip at telus.net
Sat Jun 3 22:10:27 PDT 2006


Hi all, I have a question for the Perl guru's on this list.  What I want 
to do is have my Perl script read from a text file some config data that 
looks something like:

[DataEntry]
SubRoutineToHandleThisEntry="foo"
ArgumentsToSub="hello world"

The idea is that the string "foo" is supposed to be the name of a 
subroutine in my Perl script, and the string "hello world" is the 
argument to pass to this subroutine.  I can do something like this:

$nameOfSub = "foo";       # name of sub to call
$args = "hello world!";   # args to pass to sub

&$nameOfSub ($args);      # call foo() via a symbolic reference

sub foo {
      print "In foo with args: @_\n";
}

And this works, but there is (IMHO) one very major problem with this: it 
is a symbolic reference, and thus if I put "use strict" at the top of my 
script, this trick no longer works.  Does anybody have a way of getting 
around this limitation?  I *very* much want "use strict" to be in effect 
for my script, but I still want the flexibility of being able to read 
from a file the name of a subroutine to call.  I could do something like 
have a hash in my script which maps string tokens to subroutine 
references, like so:

my %tokenToSubHash = ( "foo" => \&foo, "bar" => \&bar);

&{$tokenToSubHash{"foo"}}("hello world");

but this just seems a bit awkward as I now have to make sure I have an 
entry in this hash for every subroutine which is "visible" from the 
config file.

Anybody have any other creative ideas?

Thanks in advance.
-- 
Adam Parkin
E-mail: pzelnip at telus.net
--------------------------
At the source of every error which is blamed on the computer you will
find at least two human errors, including the error of blaming it on
the computer.



More information about the Victoria-pm mailing list