>>>>> "KG" == Kenneth Graves <[EMAIL PROTECTED]> writes:
KG> <untested>
KG> $func = *{$Quizzer::Attempt::{$name}}{CODE} and return $func->($self) ;
KG> Works if $name is in the package, fails if not:
KG> Can't use an undefined value as a symbol reference at -e line 1.
so convert the whole thing to a symref instead of a stash lookup:
<tested>
perl -we 'sub f {print "@_\n"} $n = shift ; $f=*{"::$n"}{CODE} and $f->(1)' f
1
perl -we 'sub f {print "@_\n"} $n = shift ; $f=*{"::$n"}{CODE} and $f->(1)' f1
or for your example:
$func = *{"Quizzer::Attempt::$name"}{CODE} and return $func->($self) ;
now, advocating symrefs is not my style. i would say to make a dispatch
table anyway for several reasons. first, it is not symrefs. secondly you
can limit the functions called whereas with your method any method in
the class could be called. in fact, you can call the one liner with x::f
and it will work is x::f is defined. so a dispatch table limits the
calls to exactly the subset you want. a dispatch table allows you to
change the name of the function separately from the key used to call
it. it is just a better idea and using the stash for one is confusing to
others, plays around symrefs and globs, and is not as safe.
uri
--
Uri Guttman ------ [EMAIL PROTECTED] -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org