home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning Perl, 3rd EditionSearch this book

B.5. Some Important Modules

We describe some of the most important features[403] of the most important modules[404] in this section. These modules that we discuss here should generally be found on every machine that has Perl, except where mentioned. You can always get the latest ones from CPAN.

[403]We're including here merely the most important features of each module; see the module's own documentation to learn more.

[404]To be sure, there are other important modules whose use is too complex for most readers of this book, typically because using the module requires understanding Perl's references or objects.

B.5.4. The File::Basename Module

We covered this module in Chapter 13, "Manipulating Files and Directories". It's primary uses are to portably pull the basename or directory name from a full filename:

use File::Basename;

for (@ARGV) {
  my $basename = basename $_;
  my $dirname = dirname $_;
  print "That's file $basename in directory $dirname.\n";
}

B.5.6. The File::Spec Module

When you need to manipulate a filename (more formally called a "file specification"), it's generally more portable and reliable to use the File::Spec module than to do the work yourself from Perl. For example, you can use the catfile function to put together a directory name and a filename to produce a long filename (as we saw in Chapter 13, "Manipulating Files and Directories"), but you don't have to know whether the system your program is running on uses a forward slash or some other character to separate those. Or you could use the curdir function to get the name of the current directory (".", on Unix systems).

The File::Spec module is object-oriented, but you don't need to understand objects to use it. Just call each function ("method", really) by using File::Spec and a small arrow before the function's name, like this:

use File::Spec;

my $current_directory = File::Spec->curdir;
opendir DOT, $current_directory
  or die "Can't open current directory '$current_directory': $!";


Library Navigation Links

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