12.2 Selecting What to Import


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

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

Now define the two given subroutines from the module, leaving your own dirname alone. Of course, this is awkward to type, so more often you'll see this written as:

 use File::Basename qw( fileparse basename ); 

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

You've protected the local dirname routine, but what if you still want the functionality provided by File::Basename 's dirname ? No problem. Just spell it out in full:

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

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

[4] 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, you 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 in my current package," while an absent list says "give me the defaults." [5] If the module's author has done her job well, the default will probably be exactly what you want.

[5] As you'll see later in this chapter, the default list comes from the module's @EXPORT array.



Learning Perl Objects, References & Modules
Learning Perl Objects, References, and Modules
ISBN: 0596004788
EAN: 2147483647
Year: 2003
Pages: 199

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