Learning Perl Objects, References & ModulesLearning Perl Objects, References & ModulesSearch this book

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:[63]

[63]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."[64] If the module's author has done her job well, the default will probably be exactly what you want.

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



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.