8 my %module = (MacOS => 'Mac',
14 my $module = $module{$^O} || 'Unix';
15 require "File/Spec/$module.pm";
16 @ISA = ("File::Spec::$module");
24 File::Spec - portably perform operations on file names
30 $x=File::Spec->catfile('a', 'b', 'c');
32 which returns 'a/b/c' under Unix. Or:
34 use File::Spec::Functions;
36 $x = catfile('a', 'b', 'c');
40 This module is designed to support operations commonly performed on file
41 specifications (usually called "file names", but not to be confused with the
42 contents of a file, or Perl's file handles), such as concatenating several
43 directory and file names into a single path, or determining whether a path
44 is rooted. It is based on code directly taken from MakeMaker 5.17, code
45 written by Andreas KE<ouml>nig, Andy Dougherty, Charles Bailey, Ilya
46 Zakharevich, Paul Schinder, and others.
48 Since these functions are different for most operating systems, each set of
49 OS specific routines is available in a separate module, including:
57 The module appropriate for the current OS is automatically loaded by
58 File::Spec. Since some modules (like VMS) make use of facilities available
59 only under that OS, it may not be possible to load all modules under all
62 Since File::Spec is object oriented, subroutines should not called directly,
65 File::Spec::catfile('a','b');
67 but rather as class methods:
69 File::Spec->catfile('a','b');
71 For simple uses, L<File::Spec::Functions> provides convenient functional
72 forms of these methods.
80 No physical check on the filesystem, but a logical cleanup of a
83 $cpath = File::Spec->canonpath( $path ) ;
87 Concatenate two or more directory names to form a complete path ending
88 with a directory. But remove the trailing slash from the resulting
89 string, because it doesn't look good, isn't necessary and confuses
90 OS2. Of course, if this is the root directory, don't cut off the
93 $path = File::Spec->catdir( @directories );
97 Concatenate one or more directory names and a filename to form a
98 complete path ending with a filename
100 $path = File::Spec->catfile( @directories, $filename );
104 Returns a string representation of the current directory.
106 $curdir = File::Spec->curdir();
110 Returns a string representation of the null device.
112 $devnull = File::Spec->devnull();
116 Returns a string representation of the root directory.
118 $rootdir = File::Spec->rootdir();
122 Returns a string representation of the first writable directory from a
123 list of possible temporary directories. Returns "" if no writable
124 temporary directories are found. The list of directories checked
125 depends on the platform; e.g. File::Spec::Unix checks $ENV{TMPDIR} and
128 $tmpdir = File::Spec->tmpdir();
132 Returns a string representation of the parent directory.
134 $updir = File::Spec->updir();
138 Given a list of file names, strip out those that refer to a parent
139 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
141 @paths = File::Spec->no_upwards( @paths );
145 Returns a true or false value indicating, respectively, that alphabetic
146 is not or is significant when comparing file specifications.
148 $is_case_tolerant = File::Spec->case_tolerant();
150 =item file_name_is_absolute
152 Takes as argument a path and returns true if it is an absolute path.
154 $is_absolute = File::Spec->file_name_is_absolute( $path );
156 This does not consult the local filesystem on Unix, Win32, or OS/2. It
157 does sometimes on MacOS (see L<File::Spec::MacOS/file_name_is_absolute>).
158 It does consult the working environment for VMS (see
159 L<File::Spec::VMS/file_name_is_absolute>).
163 Takes no argument, returns the environment variable PATH as an array.
165 @PATH = File::Spec->path();
169 join is the same as catfile.
173 Splits a path in to volume, directory, and filename portions. On systems
174 with no concept of volume, returns undef for volume.
176 ($volume,$directories,$file) = File::Spec->splitpath( $path );
177 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
179 For systems with no syntax differentiating filenames from directories,
180 assumes that the last file is a path unless $no_file is true or a
181 trailing separator or /. or /.. is present. On Unix this means that $no_file
182 true makes this return ( '', $path, '' ).
184 The directory portion may or may not be returned with a trailing '/'.
186 The results can be passed to L</catpath()> to get back a path equivalent to
187 (usually identical to) the original path.
191 The opposite of L</catdir()>.
193 @dirs = File::Spec->splitdir( $directories );
195 $directories must be only the directory portion of the path on systems
196 that have the concept of a volume or that have path syntax that differentiates
197 files from directories.
199 Unlike just splitting the directories on the separator, empty
200 directory names (C<''>) can be returned, because these are significant
201 on some OSs (e.g. MacOS).
205 Takes volume, directory and file portions and returns an entire path. Under
206 Unix, $volume is ignored, and directory and file are catenated. A '/' is
207 inserted if need be. On other OSs, $volume is significant.
209 $full_path = File::Spec->catpath( $volume, $directory, $file );
213 Takes a destination path and an optional base path returns a relative path
214 from the base path to the destination path:
216 $rel_path = File::Spec->abs2rel( $path ) ;
217 $rel_path = File::Spec->abs2rel( $path, $base ) ;
219 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
220 then it is converted to absolute form using L</rel2abs()>. This means that it
221 is taken to be relative to L<cwd()|Cwd>.
223 On systems with the concept of a volume, this assumes that both paths
224 are on the $destination volume, and ignores the $base volume.
226 On systems that have a grammar that indicates filenames, this ignores the
227 $base filename as well. Otherwise all path components are assumed to be
230 If $path is relative, it is converted to absolute form using L</rel2abs()>.
231 This means that it is taken to be relative to L<cwd()|Cwd>.
233 No checks against the filesystem are made on most systems. On MacOS,
234 the filesystem may be consulted (see
235 L<File::Spec::MacOS/file_name_is_absolute>). On VMS, there is
236 interaction with the working environment, as logicals and
239 Based on code written by Shigio Yamaguchi.
243 Converts a relative path to an absolute path.
245 $abs_path = File::Spec->rel2abs( $path ) ;
246 $abs_path = File::Spec->rel2abs( $path, $base ) ;
248 If $base is not present or '', then L<cwd()|Cwd> is used. If $base is relative,
249 then it is converted to absolute form using L</rel2abs()>. This means that it
250 is taken to be relative to L<cwd()|Cwd>.
252 On systems with the concept of a volume, this assumes that both paths
253 are on the $base volume, and ignores the $path volume.
255 On systems that have a grammar that indicates filenames, this ignores the
256 $base filename as well. Otherwise all path components are assumed to be
259 If $path is absolute, it is cleaned up and returned using L</canonpath()>.
261 No checks against the filesystem are made on most systems. On MacOS,
262 the filesystem may be consulted (see
263 L<File::Spec::MacOS/file_name_is_absolute>). On VMS, there is
264 interaction with the working environment, as logicals and
267 Based on code written by Shigio Yamaguchi.
271 For further information, please see L<File::Spec::Unix>,
272 L<File::Spec::Mac>, L<File::Spec::OS2>, L<File::Spec::Win32>, or
277 L<File::Spec::Unix>, L<File::Spec::Mac>, L<File::Spec::OS2>,
278 L<File::Spec::Win32>, L<File::Spec::VMS>, L<File::Spec::Functions>,
279 L<ExtUtils::MakeMaker>
283 Kenneth Albanowski <kjahds@kjahds.com>, Andy Dougherty
284 <doughera@lafcol.lafayette.edu>, Andreas KE<ouml>nig
285 <A.Koenig@franz.ww.TU-Berlin.DE>, Tim Bunce <Tim.Bunce@ig.co.uk. VMS
286 support by Charles Bailey <bailey@newman.upenn.edu>. OS/2 support by
287 Ilya Zakharevich <ilya@math.ohio-state.edu>. Mac support by Paul Schinder
288 <schinder@pobox.com>. abs2rel() and rel2abs() written by
289 Shigio Yamaguchi <shigio@tamacom.com>, modified by Barrie Slaymaker
290 <barries@slaysys.com>. splitpath(), splitdir(), catpath() and catdir()