[VPM] Symbolic references with use strict in effect.
Peter Scott
Peter at psdt.com
Mon Jun 5 07:57:53 PDT 2006
At 1:19 AM -0400 6/4/06, abez wrote:
>no strict "subs";
I beg to differ.
no strict 'refs';
The alternative without violating strict is to maintain a dispatch
table: hash of names vs subrefs. However, I have used the
strict-violating shortcut myself. It boils down to whether I want to
validate the routine to be called. If I find myself writing "if
defined &$sub2call" I know I'd be better off with the dispatch table.
>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
>
>_______________________________________________
>Victoria-pm mailing list
>Victoria-pm at pm.org
>http://mail.pm.org/mailman/listinfo/victoria-pm
More information about the Victoria-pm
mailing list