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



"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( ... ) ;

just make sure all those methods expect (and probably ignore) the class
name as their first argument.

remember:

symrefs are for munging the symbol table, not for flow control or data
structures.

uri

--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.



Relevant Pages