Re: How to dynamically generate function name and call it?
- From: Uri Guttman <uri@xxxxxxxxxxxxxxx>
- Date: Thu, 28 Sep 2006 14:25:37 -0400
"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
.
- 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?
- From: Brian McCauley
- Re: How to dynamically generate function name and call it?
- Prev by Date: Re: matching date
- Next by Date: Re: FAQ 3.1 How do I do (anything)?
- 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
|