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:
lib/Our/Code/Customer.pm lib/Our/Code/Datacenter.pm lib/Our/Code/Item.pm lib/Our/Code/Office.pm lib/Our/Code/Order.pm lib/Our/Code/Server.pm lib/Our/Code/Server/Dedicated.pm
The above 'aliased' line would give you aliases like this:
use aliased 'Our::Code::*'; my $cust = Customer->new; my $datacenter = Datacenter->new; my $item = Item->new; my $office = Office->new; my $order = Order->new; my $server = Server->new; my $ded_server = ServerDedicated->new; # unsure of this
Traditionally, that would be something like this:
use Our::Code::Customer; use Our::Code::Datacenter; use Our::Code::Item; use Our::Code::Office; use Our::Code::Order; use Our::Code::Server; use Our::Code::Server::Dedicated; my $cust = Our::Code::Customer->new; my $datacenter = Our::Code::Datacenter->new; my $item = Our::Code::Item->new; my $office = Our::Code::Office->new; my $order = Our::Code::Order->new; my $server = Our::Code::Server->new; my $ded_server = Our::Code::Server::Dedicated->new;
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:
sub Order { my $class = 'Our::Customer::Order'; if ( exists $INC{'lib/Order.pm'} ) { # die and noisily complain about a conflict } # load the class if not loaded return $class; }
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.
Cheers,
Ovid
New address of my CGI Course.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A Perl Version of Java's import
by jhourcle (Prior) on Aug 07, 2007 at 13:12 UTC | |
Re: A Perl Version of Java's import
by ForgotPasswordAgain (Priest) on Aug 07, 2007 at 10:23 UTC | |
by Ovid (Cardinal) on Aug 07, 2007 at 10:32 UTC | |
Re: A Perl Version of Java's import (lexicals > globals)
by tye (Sage) on Aug 08, 2007 at 03:48 UTC | |
by Anonymous Monk on Dec 29, 2007 at 04:06 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |