|Think about Loose Coupling|
How to fool caller() / use NEXT within a dynamic subby betterworld (Deacon)
|on Jul 26, 2005 at 18:49 UTC||Need Help??|
betterworld has asked for the
wisdom of the Perl Monks concerning the following question:
I was looking for a way to change the current subroutine name to an arbitrary value (which is unknown at compile time), in order to have caller() return this value.
In particular, it is my ambition to inject a subroutine into an arbitrary package, and that subroutine should use NEXT, as the following code suggests:
Unfortunately, this piece of code does not work as expected, because NEXT will use (caller(1)) to determine the calling function, and that won't yield $package . '::function'.
You may verify this using the following script:
The output is main::__ANON__, however I'd like it to be testpackage::function.
One possible solution would be eval, but I don't like this much:
I know that I can trick caller() with goto &sub, however, I don't think that this gets me any further.
Does anyone know a solution to my problem?