jmay has asked for the wisdom of the Perl Monks concerning the following question:
Subroutine definitions (and declarations, for that matter) need not necessarily be situated in the package whose symbol table they occupy. You can define a subroutine outside its package by explicitly qualifying the name of the subroutine:
package main; sub Some_package::foo { ... } # &foo defined in Some_package
This is just a shorthand for a typeglob assignment at compile time:
BEGIN { *Some_package::foo = sub { ... } }
OK, my question: Is there a clean way to add a subroutine to another package, where the "other" package name is not known at compile time?
The best I can do is:
eval "*${package}::foo = sub { ... }";
but I'd rather not use eval if I don't have to.
-Jason
|
|---|