Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Difference between use Module::Name and use Module::Name qw /method1 method2/

by tobyink (Abbot)
on Jan 29, 2013 at 11:18 UTC ( #1015853=note: print w/replies, xml ) Need Help??

in reply to Difference between use Module::Name and use Module::Name qw /method1 method2/

Formally, the syntax of the use statement is such that...

use Foo::Bar; # is equivalent to: BEGIN { require Foo::Bar; Foo::Bar->import(); }; # and with parameters: use Foo::Bar ...; # is equivalent to: BEGIN { require Foo::Bar; Foo::Bar->import(...); };

The only slightly surprising case is an explicit empty list:

use Foo::Bar (); # is equivalent to: BEGIN { require Foo::Bar; };

... and the import method is not called at all!

So the question comes down to; what is the difference between these:

Algorithm::LUHN->import(); Algorithm::LUHN->import(qw/is_valid/);

import is just a regular old class method; nothing special about it. So the difference between those is whatever the author of Algorithm::LUHN wants it to be.

Nonetheless, there are some conventions for the parameters to the import method. While they're just conventions, they're quite widely followed...

If you supply a list of words (like "is_valid") to the import method, it indicates that you want to import the listed functions into your own namespace. So, given:


You're saying you want to copy Algorithm::LUHN::is_valid to Your::Package::is_valid. This is really an alias rather than a copy, so uses negligible memory, and negligible CPU (unless you're aliasing dozens and dozens of functions). A bigger concern is often namespace pollution - it means you no longer have the freedom to define your own is_valid function in your package.

If the list of words includes some prefixed with ":" or "-", these often refer to "bundles" of functions. So for example, Algorithm::LUHN could define a :validity bundle which included "is_valid" and also "get_validation_errors". So calling:

Algorithm::LUHN->import(qw/ :validity /);

... would import both functions.

If you don't pass any import parameters at all, the convention is that the module will choose some "default" list of things to export to your namespace - which may be nothing; or may be everything; or may be somewhere in between.

Of course, just because you request some function to be exported; there's no guarantee that it actually will - the module may not offer the function you requested. In these cases it is convention for the import method to croak

Those are the conventions; there's no rule that says anybody must follow them, but if one does deviate from them, then it's polite to document ones deviation!

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015853]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2018-05-24 10:28 GMT
Find Nodes?
    Voting Booth?