In Perl6, I suspect you'll solve this problem with code something like:

my $ArrayList= use Perl::Util::ArrayList; my $a= $ArrayList->new();
because Larry mentioned that he'd like to have use return the package that was just created (I think he meant/said "package object" not "package name" but that doesn't really change the above code much).

In Perl5, I solve this problem by having the module be able to export a factory object (for OO modules) and by supporting aliasing of functions during export (for procedural modules):

my $Widget; use My::Widget::Module( Factory=>\$Widget ); my $dumper; use Devel::Peek( { Dump=>\&DumpSV } ); use Data::Dumper( { Dump=>\&Dumper } ); use My::Module( { Dump=>\$dumper } ); my $flange= $Widget->new(); $dumper->( $flange ); Dumper( $flange );
Note that you have to write your own sub import to support the above (some of the newer replacements for Exporter also support aliasing of imports, IIRC).

And, although Perl5 doesn't support my $x= use ..., it does support (but doesn't document) the following rather neat trick:

my $factory= require My::Object::Module;
via a module like, for example:
package My::Object::Module; # ... sub factory { # ... } # ... \&factory; # a 'true' value
The only thing I don't like about this trick (besides it being undocumented, though that didn't stop me from guessing that it might work) is that you can't pass in any arguments to require like you can with use.

                - tye

In reply to Re: Pragma (more) like Java's 'import'? (yes!) by tye
in thread Pragma (more) like Java's 'import'? by djantzen

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.