Syntactic Confectionery Delight | |
PerlMonks |
Re: Name of subpackage dependent on main packageby davido (Cardinal) |
on Dec 29, 2019 at 23:34 UTC ( [id://11110740]=note: print w/replies, xml ) | Need Help?? |
It obviously doesn't work quite how you're thinking. One of the problems is that package does not accept the namespace as a dynamic argument. The namespace must be a hard-coded string of characters. However, all is not lost. You can have everything you want, just not the way you're doing it:
You can bless a reference into any string you want. And in this case, you can assign a subroutine reference to any typeglob you want. In the example above I'm creating My::A::new, and My::A::greet. Yes, it's a little more work, but where there's work, there's a need for automation:
This hash based method has the advantage of releasing strict 'refs' in a much smaller scope. To be clear, you don't need a package My::A; statement to be able to create subroutines and methods in the My::A namespace; you just have to do without the conveniences package provides, crafting the subroutine namespaces manually. And, in fact, this method is how Class:Builder works (snippet from Class::Builder):
Dave
In Section
Seekers of Perl Wisdom
|
|