[tpm] eval oddness

Stuart Watt stuart at morungos.com
Thu Dec 10 10:16:37 PST 2009


If what you're after is changing the parent class dynamically, something 
like:

push @ISA, 'Net::DBus::Object';

is likely to be a better bet than eval "use base ...". This should add 
Net::DBus::Object to the parent list. @ISA is a funny variable, which 
has some magic behind it, and it would not surprise me if the eval 
technique somehow failed to do the right magic. eval() does do some odd 
things, and @ISA is not just an ordinary variable. One hack that is 
mentioned is that setting @ISA  = @ISA can clear some of the caching on 
@ISA, and could be that adding that will make things work better.

However, generally, changing the inheritance of an object at runtime is 
an easy way to get in lots of trouble. I did it once, but I didn't get 
away with it completely, and things were never the same afterwards.

In the end, it was much safer to either (a) use a factory style pattern 
which returns an instance of the class you need, when this class is 
chosen dynamically, or (b) store the instance of Net::DBus::Object 
somewhere in a lexical in a facade style instance, and defer method 
calls to other objects as you need. Inventive use of AUTOLOAD can pass 
on any method not explicitly defined to this lexical, even checking it 
can handle it. These increase the number of objects, but should be 
easier to handle.

All the best
Stuart




More information about the toronto-pm mailing list