Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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]
[Dumu]: hello jedikaiti
[Dumu]: got it. Anyway. Perl question. Can you use binmode more than once with an open filehandle to read a file first in binary mode, then in utf8 mode?
[ambrus]: Dumu: you can use binmode multiple times, but mostly at the start before you read things. it would be strange to change the io layers after you've already read things, and might not work too well.
[ambrus]: Dumu: why would you want to read the same file both in binary mode and utf8 mode?
[Corion]: Dumu: Yes, but switching needs to be done with caution, as you might read half an UTF-8 character in binary mode, and then switch to utf8 mode...
[Corion]: Personally, I would do the decoding of data in the program and not in the filehandle
[Corion]: ambrus: Maybe a record-based file where some data is strings encoded as utf8

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2017-11-20 17:48 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (290 votes). Check out past polls.