Section 3.5.  Object-Oriented Interfaces

Table of Contents

3.5. Object-Oriented Interfaces

Contrast the subroutines imported by File::Basename with what another core module has by looking at File::Spec. The File::Spec module is designed to support operations commonly performed on file specifications. (A file specification is usually a file or directory name, but it may be a name of a file that doesn't existin which case, it's not really a filename, is it?)

Unlike the File::Basename module, the File::Spec module has a primarily objectoriented interface. We load the module with use, as we did before.

use File::Spec;

However, since this module has an object-oriented interface,[Section 3.5.  Object-Oriented Interfaces] it doesn't import any subroutines. Instead, the interface tells us to access the functionality of the module using its class methods. The catfile method joins a list of strings with the appropriate directory separator:

[Section 3.5.  Object-Oriented Interfaces] We can use File::Spec::Functions if we want a functional interface.

my $filespec = File::Spec->catfile( $homedir{gilligan},
        'web_docs', 'photos', 'USS_Minnow.gif' );

This calls the class method catfile of the File::Spec class, which builds a path appropriate for the local operating system and returns a single string.[Section 3.5.  Object-Oriented Interfaces] This is similar in syntax to the nearly two dozen other operations provided by File::Spec.

[Section 3.5.  Object-Oriented Interfaces] That string might be something like /home/gilligan/web_docs/photos/USS_Minnow.gif on a Unix system. On a Windows system, it would typically use backslashes as directory separators . This module lets us write portable code easily, at least where file specs are concerned.

The File::Spec module provides several other methods for dealing with file paths in a portable manner. You can read more about portability issues in the perlport documentation.

Table of Contents
© 2000- NIV