Section 3.4. Selecting What to Import


3.4. Selecting What to Import

Fortunately, we can tell the use operation to limit its actions by specifying a list of subroutine names following the module name, called the import list:

 use File::Basename ('fileparse', 'basename'); 

Now the module only gives us those two subroutines and leaves our own dirname alone. Of course, this is awkward to type, so more often we'll see this written with the quotewords operator:

 use File::Basename qw( fileparse basename ); 

In fact, even if there's only one item, we tend to write it with a qw( ) list for consistency and maintenance; often we'll go back to say "give me another one from here," and it's simpler if it's already a qw( ) list.

We've protected the local dirname routine, but what if we still want the functionality provided by File::Basename's dirname? No problem. We just spell it out with its full package specification:

 my $dirname = File::Basename::dirname($some_path); 

The list of names following use doesn't change which subroutines are defined in the module's package (in this case, File::Basename). We can always use the full name regardless of the import list, as in:[*]

[*] You don't need the ampersand in front of any of these subroutine invocations, because the subroutine name is already known to the compiler following use.

 my $basename = File::Basename::basename($some_path); 

In an extreme (but extremely useful) case, we can specify an empty list for the import list, as in:

 use File::Basename (  );              # no import my $base = File::Basename::basename($some_path); 

An empty list is different from an absent list. An empty list says "don't give me anything," while an absent list says "give me the defaults." If the module's author has done her job well, the default will probably be exactly what we want.




Intermediate Perl
Intermediate Perl
ISBN: 0596102062
EAN: 2147483647
Year: N/A
Pages: 238

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net