Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Generic/Variable/Dynamic Subroutines? (Not a redefine)by haukex (Archbishop) |
on Aug 18, 2017 at 19:27 UTC ( [id://1197627]=note: print w/replies, xml ) | Need Help?? |
This will generate those three subs you showed (Update: see Function Templates and Method Names as Strings):
Optionally put that in a BEGIN { ... } block if you want to be able to call the subs without parens. Here's a simple wrapper class that uses Autoloading, although autoloading can be a bit dangerous and have some caveats (e.g. the following doesn't overload UNIVERSAL methods like can, although it probably should for completeness; Update: also this example always calls the method in scalar context, although that could be changed by inspecting wantarray; plus obviously wrapper and wrapped objects have completely different classes, so isa and especially ref checks would break), so I would recommend the above solution instead. Use this like my $wrapper = Wrapper->new($object); and you should be able to call the original methods from $object on $wrapper instead. Update 2: I guess I should be more clear that the following is really just a demonstration of the (sometimes scary) power of AUTOLOADing and not something I'd recommend for wide use. You might want to look at something like around from Class::Method::Modifiers instead.
In Section
Seekers of Perl Wisdom
|
|