Your skill will accomplish
what the force of many cannot
A Perl Version of Java's importby Ovid (Cardinal)
|on Aug 07, 2007 at 10:07 UTC||Need Help??|
Ovid has asked for the
wisdom of the Perl Monks concerning the following question:
A long time ago, Matt Trout mentioned that he's started using aliased a bit and wanted a feature like this:use aliased 'Our::Code::*';
That would be conceptually equivalent to the following Java code:import Our.Code.*;
Now imagine you have a directory structure like this:
The above 'aliased' line would give you aliases like this:
Traditionally, that would be something like this:
Needless to say, you can see which one folks might like.
Of course, the devil's in the details and I think a module other than aliased would be called for with this experiment. There would also have to be customization options. For example, I think by default it shouldn't use those modules until the alias is actually invoked. Also, by default, we'd want to ensure that only one directory structure is searched and classes loaded from that directory. We might want descending into subdirectories to be optional. One also might want to be able to load and alias all classes in the format Our::Code::O*.
The biggest objection I expect to hear from folks is "mysterious action at a distance is bad!" I can understand that point and I would have raised it myself a long time ago, but I've been around the block long enough to realize why this works for Java so well: after you've been working on your business code long enough, you know what's in lib/Our/Code/*. Of course, it will have issues when you try to do this with lib/Our/Code/DateTime.pm in there. I think a CHECK block might help, not to mention invoking the alias should check if there's a competing entry in %INC. For example, with Our::Code::Order:
Unfortunately, whether the class method call dispatches to the namespace or would invoke the alias would depend on how the code was organized. This could be the biggest problem.
I have enough sympathy for the 'action at a distance' argument and a firm enough belief that this is different enough from aliased that the experiment definitely deserves to be in a different namespace.
Side Note: It's also worth nothing that there's a broken import module on the CPAN and it hasn't been updated in 8 years.
Do you have suggestions or comments about such a module's usefulness or implementation? What namespace would be good? How would you get around the DateTime/Our::Code::DateTime problem?
Update: Wow. That was quick. I was having misgivings about this idea, but now I might just drop it completely.