laziness, impatience, and hubris | |
PerlMonks |
Re: Solving the SUPER problem in Mixins with String Evalby hv (Prior) |
on Oct 11, 2004 at 10:50 UTC ( [id://398130]=note: print w/replies, xml ) | Need Help?? |
Is there a way to avoid string evals here by rebinding a clone of a subroutine to a new package, so that SUPER resolution will start from that other class? I'm not aware of any way of retargetting SUPER resolution. I have some similar but different needs in my work application, and I solve them by injection: I construct a class specific to the caller which is injected into the caller's @ISA, and use string eval to recompile each of the methods supplied by the mixin into that class. So in one module I might have: and NVC::DB::attr will create a class NVC::DB::cache::User into which to inject the extra methods, and set:
Since I'm using the string eval anyway, I take advantage of it to construct mixin methods specific to the adopting class rather than creating catch-all methods that do runtime tests to determine the correct behaviour for the class. So, for example, the cache mixin uses different code depending whether the adopting class has a single key field or multiple key fields. Since this is all transparent to the adopter, I can turn caching on or off by adding or removing use NVC::DB::attr qw/ cache /; in the class definition. Hugo
In Section
Meditations
|
|