Re: How to dynamically generate function name and call it?
- From: "Brian McCauley" <nobull67@xxxxxxxxx>
- Date: 28 Sep 2006 10:15:13 -0700
Uri Guttman wrote:
"BM" == Brian McCauley <nobull67@xxxxxxxxx> writes:
BM> Tad McClellan wrote:
>> my %actions = ( # a "dispatch table", untested
>> TRAC => \&CreateTRACTable,
>> TRCO => \&CreateTRCOTable
>> );
BM> There are two entries in that hash. A soon as you add another one
BM> you'll hit the rule-of-3. You'll be manually doing the same thing three
BM> times in succession. In programming this is generally a sign that you
BM> are doing it wrong. This is no exception. You probably should be
BM> going down the symbolic references path. There are a lot of reasons not
BM> to use symbolic references in a lot of contexts. This leads some
BM> people, most vocally Tad, to say they are always a bad thing. And
BM> indeed I'll admit that in some purist sense they are always a bad
BM> thing. But in this case they are not as bad as breaking the rule-of-3
BM> (IMNSHO).
i agree with tad about avoiding symrefs. if the command is truly part
of the function name, then another choice is to use class method calls
which can be generated without symrefs or breaking strict.
my $meth = "Create${command}able" ;
MyClass->$meth( ... ) ;
So long as @MyClass::ISA is non-empty and $command doesn't contain '::'
(or similar) that's equivalent to
no strict 'refs';
"MyClass::Create${command}able"->('MyClass', ... ) ;
Uri's solution is still using a symref (i.e.a runtime-lookup of a
string from _data_ in the Perl symbol table) under the hood, but it
doesn't carry the red flag of a "no strict" in front of it.
Now as it happens in this case Uri's solution happens not to open up a
secirty hole in this particular instance because we're augementing
$command. But consider, if you will, the (more common) situation where
you're not.
MyClass->$command( ... ) ;
Now I have to agree that this is looks a whole lot cleaner than my
alternative.
no strict 'refs';
"MyClass::$command"->( ... ) ;
But looks can be deceptive.
remember:
symrefs are for munging the symbol table, not for flow control or data
structures.
Remember, symrefs are very dangerous things, and remain dangerous even
if you pretend not to use them. In fact they become more dangerous
because people looking at the code will not see "no strict" and may be
fooled into thinking you aren't using symrefs so not realize the
danger.
Consider what happens in the code below when fed
'main::private_function' on STDIN
#!perl -T
use strict;
use warnings;
sub private_function { die "Bang!" };
sub AllowedOperations::allowed { print "yes, you can do that" }
while (<>) {
chomp;
AllowedOperations->$_();
}
__END__
.
- Follow-Ups:
- Re: How to dynamically generate function name and call it?
- From: Uri Guttman
- Re: How to dynamically generate function name and call it?
- References:
- Re: How to dynamically generate function name and call it?
- From: Tad McClellan
- Re: How to dynamically generate function name and call it?
- From: Brian McCauley
- Re: How to dynamically generate function name and call it?
- From: Uri Guttman
- Re: How to dynamically generate function name and call it?
- Prev by Date: Re: cookie problem.
- Next by Date: Re: Net::SMTP module error
- Previous by thread: Re: How to dynamically generate function name and call it?
- Next by thread: Re: How to dynamically generate function name and call it?
- Index(es):
Relevant Pages
|