Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

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

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

In reply to Re: Difference between use Module::Name and use Module::Name qw /method1 method2/ by tobyink
in thread Difference between use Module::Name and use Module::Name qw /method1 method2/ by Doctrin

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-18 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found