[VPM] Symbolic references with use strict in effect.

abez abez at abez.ca
Sat Jun 3 22:19:33 PDT 2006


no strict "subs";

perldoc strict will tell you the rest.

abram

On Sat, 3 Jun 2006, Adam Parkin wrote:

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

-- 
abez ------------------------------------------
http://www.abez.ca/ Abram Hindle (abez at abez.ca)
------------------------------------------ abez



More information about the Victoria-pm mailing list