Section 3.3.  Functional Interfaces

Table of Contents

3.3. Functional Interfaces

To load a module, we use the Perl built-in use. We're not going to go into all of the details here, but we'll get to those in Chapters 10 and 15. At the moment, we just want to use the module. Let's start with File::Basename, that same module from the core distribution. To load it into our script, we say:

use File::Basename;

When we do this, File::Basename introduces three subroutines, fileparse, basename, and dirname,[*] into our script.[Section 3.3.  Functional Interfaces] From this point forward, we can say:

[*] As well as a utility routine, fileparse_set_fstype.

[Section 3.3.  Functional Interfaces] Actually, it imports them into the current package, but we haven't told you about those yet.

my $basename = basename( $some_full_path );
my $dirname  = dirname(  $some_full_path );

as if we had written the basename and dirname subroutines ourselves, or (nearly) as if they were built-in Perl functions. These routines pick out the filename and the directory parts of a pathname. For example, if $some_full_path were D:\Projects\Island Rescue\plan7.rtf (presumably, the program is running on a Windows machine), then $basename would be plan 7.rtf and the $dirname would be D:\Projects\Island Rescue.

The File::Basename module knows what sort of system it's on, and thus its functions figure out how to correctly parse the strings for the different delimiters we might encounter.

However, suppose we already had a dirname subroutine. We've now overwritten it with the definition provided by File::Basename! If we had turned on warnings, we would have seen a message stating that; but otherwise, Perl really doesn't care.

Table of Contents
© 2000- NIV