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



"BM" == Brian McCauley <nobull67@xxxxxxxxx> writes:

BM> MyClass->$command( ... ) ;

BM> Now I have to agree that this is looks a whole lot cleaner than my
BM> alternative.

BM> no strict 'refs';
BM> "MyClass::$command"->( ... ) ;

BM> But looks can be deceptive.

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

BM> Remember, symrefs are very dangerous things, and remain dangerous even
BM> if you pretend not to use them. In fact they become more dangerous
BM> because people looking at the code will not see "no strict" and may be
BM> fooled into thinking you aren't using symrefs so not realize the
BM> danger.

in general i would use a dispatch table just so i can control which subs
can be called. even if it was uber symmetrical i would have created it
with hard refs. good editors make that easy.

and if i do the dynamic method call i will usually check for existance
with can() or something. you can even have a hash table of allowed
method names and check that first (no hard refs needed as values).

BM> Consider what happens in the code below when fed
BM> 'main::private_function' on STDIN

BM> #!perl -T
BM> use strict;
BM> use warnings;

BM> sub private_function { die "Bang!" };

BM> sub AllowedOperations::allowed { print "yes, you can do that" }

BM> while (<>) {
BM> chomp;
AllowedOperations-> $_();
BM> }

sure you can break a simple version. i was just showing how to do it
without no strict. i reserve no strict for direct symbol table munging
and not basic flow control.

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

  • Re: How to dynamically generate function name and call it?
    ... BM> people, most vocally Tad, to say they are always a bad thing. ... which can be generated without symrefs or breaking strict. ... 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)
  • question about forked processes writing to the same file
    ... is there ever a danger of race ... use strict; use warnings; ...
    (comp.lang.perl.misc)