Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Pragma (more) like Java's 'import'? (yes!)

by tye (Sage)
on May 12, 2003 at 17:36 UTC ( [id://257506]=note: print w/replies, xml ) Need Help??


in reply to Pragma (more) like Java's 'import'?

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

Replies are listed 'Best First'.
Re^2: Pragma (more) like Java's 'import'? (even better)
by Aristotle (Chancellor) on May 12, 2003 at 20:59 UTC
    Well, not directly.
    my $factory = (require My::Object::Module)->(foo => "bar");
    Obviously,
    package My::Object::Module; sub factory { ... } sub fake_import { # ... # ... return \&factory; } \&fake_import; __END__

    Makeshifts last the longest.

Re^2: Pragma (more) like Java's 'import'? (er, no!)
by tye (Sage) on Aug 18, 2003 at 21:50 UTC
    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;

    ...which you shouldn't use becase that will only work if you can guanantee that you are the only one who will ever require My::Object::Module. This undocumented return value only works the first time require is called. All subsequent requireings of that modules simply return 1.

    D'oh!

                    - tye

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://257506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found