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.


In reply to A Perl Version of Java's import by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.