Re: How to dynamically generate function name and call it?




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__

.



Relevant Pages

  • Re: How to dynamically generate function name and call it?
    ... >> symrefs are for munging the symbol table, not for flow control or data ... BM> Remember, symrefs are very dangerous things, and remain dangerous even ... BM> because people looking at the code will not see "no strict" and may be ... BM> danger. ...
    (comp.lang.perl.misc)
  • Re: how to call sub by value in variable
    ... The above code is using symrefs but not _obviously_ ... The easiest way to do this to put it in a a "no strict 'refs'" ... better to not play with strict on/off and use this syntax of sub ... sufficient reason to use symrefs, walk up to the counter and in a loud ...
    (comp.lang.perl.misc)
  • Re: how to call a function using variable
    ... BM> 1) It has a big red flag saying "I'm using symrefs" ... i think a dispatch table is even better. ... strict refs being needed) than hard code refs. ...
    (comp.lang.perl.misc)
  • Re: how to call sub by value in variable
    ... Brian McCauley wrote: ... If you are using symrefs your code should look like it ... Of course, no sctrict 'refs' is valid for some block, say while, foreach etc., but if I use it then I must not forget to switch strict "on" if I add some code in the same block in future. ... Petr Vileta, Czech republic ...
    (comp.lang.perl.misc)
  • Re: How to dynamically generate function name and call it?
    ... BM> you'll hit the rule-of-3. ... BM> to use symbolic references in a lot of contexts. ... BM> people, most vocally Tad, to say they are always a bad thing. ... which can be generated without symrefs or breaking strict. ...
    (comp.lang.perl.misc)