4 use vars qw(@ISA $VERSION);
7 $VERSION = eval $VERSION;
9 my %module = (MacOS => 'Mac',
14 NetWare => 'Win32', # Yes, File::Spec::Win32 works on NetWare.
15 symbian => 'Win32', # Yes, File::Spec::Win32 works on symbian.
16 dos => 'OS2', # Yes, File::Spec::OS2 works on DJGPP.
20 my $module = $module{$^O} || 'Unix';
22 require "File/Spec/$module.pm";
23 @ISA = ("File::Spec::$module");
31 File::Spec - portably perform operations on file names
37 $x=File::Spec->catfile('a', 'b', 'c');
39 which returns 'a/b/c' under Unix. Or:
41 use File::Spec::Functions;
43 $x = catfile('a', 'b', 'c');
47 This module is designed to support operations commonly performed on file
48 specifications (usually called "file names", but not to be confused with the
49 contents of a file, or Perl's file handles), such as concatenating several
50 directory and file names into a single path, or determining whether a path
51 is rooted. It is based on code directly taken from MakeMaker 5.17, code
52 written by Andreas KE<ouml>nig, Andy Dougherty, Charles Bailey, Ilya
53 Zakharevich, Paul Schinder, and others.
55 Since these functions are different for most operating systems, each set of
56 OS specific routines is available in a separate module, including:
64 The module appropriate for the current OS is automatically loaded by
65 File::Spec. Since some modules (like VMS) make use of facilities available
66 only under that OS, it may not be possible to load all modules under all
69 Since File::Spec is object oriented, subroutines should not be called directly,
72 File::Spec::catfile('a','b');
74 but rather as class methods:
76 File::Spec->catfile('a','b');
78 For simple uses, L<File::Spec::Functions> provides convenient functional
79 forms of these methods.
87 No physical check on the filesystem, but a logical cleanup of a
90 $cpath = File::Spec->canonpath( $path ) ;
92 Note that this does *not* collapse F<x/../y> sections into F<y>. This
93 is by design. If F</foo> on your system is a symlink to F</bar/baz>,
94 then F</foo/../quux> is actually F</bar/quux>, not F</quux> as a naive
95 F<../>-removal would give you. If you want to do this kind of
96 processing, you probably want C<Cwd>'s C<realpath()> function to
97 actually traverse the filesystem cleaning up paths like this.
101 Concatenate two or more directory names to form a complete path ending
102 with a directory. But remove the trailing slash from the resulting
103 string, because it doesn't look good, isn't necessary and confuses
104 OS/2. Of course, if this is the root directory, don't cut off the
107 $path = File::Spec->catdir( @directories );
111 Concatenate one or more directory names and a filename to form a
112 complete path ending with a filename
114 $path = File::Spec->catfile( @directories, $filename );
118 Returns a string representation of the current directory.
120 $curdir = File::Spec->curdir();
124 Returns a string representation of the null device.
126 $devnull = File::Spec->devnull();
130 Returns a string representation of the root directory.
132 $rootdir = File::Spec->rootdir();
136 Returns a string representation of the first writable directory from a
137 list of possible temporary directories. Returns the current directory
138 if no writable temporary directories are found. The list of directories
139 checked depends on the platform; e.g. File::Spec::Unix checks C<$ENV{TMPDIR}>
140 (unless taint is on) and F</tmp>.
142 $tmpdir = File::Spec->tmpdir();
146 Returns a string representation of the parent directory.
148 $updir = File::Spec->updir();
152 Given a list of file names, strip out those that refer to a parent
153 directory. (Does not strip symlinks, only '.', '..', and equivalents.)
155 @paths = File::Spec->no_upwards( @paths );
159 Returns a true or false value indicating, respectively, that alphabetic
160 case is not or is significant when comparing file specifications.
162 $is_case_tolerant = File::Spec->case_tolerant();
164 =item file_name_is_absolute
166 Takes as its argument a path, and returns true if it is an absolute path.
168 $is_absolute = File::Spec->file_name_is_absolute( $path );
170 This does not consult the local filesystem on Unix, Win32, OS/2, or
171 Mac OS (Classic). It does consult the working environment for VMS
172 (see L<File::Spec::VMS/file_name_is_absolute>).
176 Takes no argument. Returns the environment variable C<PATH> (or the local
177 platform's equivalent) as a list.
179 @PATH = File::Spec->path();
183 join is the same as catfile.
187 Splits a path in to volume, directory, and filename portions. On systems
188 with no concept of volume, returns '' for volume.
190 ($volume,$directories,$file) = File::Spec->splitpath( $path );
191 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
193 For systems with no syntax differentiating filenames from directories,
194 assumes that the last file is a path unless C<$no_file> is true or a
195 trailing separator or F</.> or F</..> is present. On Unix, this means that C<$no_file>
196 true makes this return ( '', $path, '' ).
198 The directory portion may or may not be returned with a trailing '/'.
200 The results can be passed to L</catpath()> to get back a path equivalent to
201 (usually identical to) the original path.
205 The opposite of L</catdir()>.
207 @dirs = File::Spec->splitdir( $directories );
209 C<$directories> must be only the directory portion of the path on systems
210 that have the concept of a volume or that have path syntax that differentiates
211 files from directories.
213 Unlike just splitting the directories on the separator, empty
214 directory names (C<''>) can be returned, because these are significant
219 Takes volume, directory and file portions and returns an entire path. Under
220 Unix, C<$volume> is ignored, and directory and file are concatenated. A '/' is
221 inserted if need be. On other OSes, C<$volume> is significant.
223 $full_path = File::Spec->catpath( $volume, $directory, $file );
227 Takes a destination path and an optional base path returns a relative path
228 from the base path to the destination path:
230 $rel_path = File::Spec->abs2rel( $path ) ;
231 $rel_path = File::Spec->abs2rel( $path, $base ) ;
233 If C<$base> is not present or '', then L<cwd()|Cwd> is used. If C<$base> is
234 relative, then it is converted to absolute form using
235 L</rel2abs()>. This means that it is taken to be relative to
238 On systems with the concept of volume, if C<$path> and C<$base> appear to be
239 on two different volumes, we will not attempt to resolve the two
240 paths, and we will instead simply return C<$path>. Note that previous
241 versions of this module ignored the volume of C<$base>, which resulted in
242 garbage results part of the time.
244 On systems that have a grammar that indicates filenames, this ignores the
245 C<$base> filename as well. Otherwise all path components are assumed to be
248 If C<$path> is relative, it is converted to absolute form using L</rel2abs()>.
249 This means that it is taken to be relative to L<cwd()|Cwd>.
251 No checks against the filesystem are made. On VMS, there is
252 interaction with the working environment, as logicals and
255 Based on code written by Shigio Yamaguchi.
259 Converts a relative path to an absolute path.
261 $abs_path = File::Spec->rel2abs( $path ) ;
262 $abs_path = File::Spec->rel2abs( $path, $base ) ;
264 If C<$base> is not present or '', then L<cwd()|Cwd> is used. If C<$base> is relative,
265 then it is converted to absolute form using L</rel2abs()>. This means that it
266 is taken to be relative to L<cwd()|Cwd>.
268 On systems with the concept of volume, if C<$path> and C<$base> appear to be
269 on two different volumes, we will not attempt to resolve the two
270 paths, and we will instead simply return C<$path>. Note that previous
271 versions of this module ignored the volume of C<$base>, which resulted in
272 garbage results part of the time.
274 On systems that have a grammar that indicates filenames, this ignores the
275 C<$base> filename as well. Otherwise all path components are assumed to be
278 If C<$path> is absolute, it is cleaned up and returned using L</canonpath()>.
280 No checks against the filesystem are made. On VMS, there is
281 interaction with the working environment, as logicals and
284 Based on code written by Shigio Yamaguchi.
288 For further information, please see L<File::Spec::Unix>,
289 L<File::Spec::Mac>, L<File::Spec::OS2>, L<File::Spec::Win32>, or
294 L<File::Spec::Unix>, L<File::Spec::Mac>, L<File::Spec::OS2>,
295 L<File::Spec::Win32>, L<File::Spec::VMS>, L<File::Spec::Functions>,
296 L<ExtUtils::MakeMaker>
300 Currently maintained by Ken Williams C<< <KWILLIAMS@cpan.org> >>.
302 The vast majority of the code was written by
303 Kenneth Albanowski C<< <kjahds@kjahds.com> >>,
304 Andy Dougherty C<< <doughera@lafayette.edu> >>,
305 Andreas KE<ouml>nig C<< <A.Koenig@franz.ww.TU-Berlin.DE> >>,
306 Tim Bunce C<< <Tim.Bunce@ig.co.uk> >>.
307 VMS support by Charles Bailey C<< <bailey@newman.upenn.edu> >>.
308 OS/2 support by Ilya Zakharevich C<< <ilya@math.ohio-state.edu> >>.
309 Mac support by Paul Schinder C<< <schinder@pobox.com> >>, and
310 Thomas Wegner C<< <wegner_thomas@yahoo.com> >>.
311 abs2rel() and rel2abs() written by Shigio Yamaguchi C<< <shigio@tamacom.com> >>,
312 modified by Barrie Slaymaker C<< <barries@slaysys.com> >>.
313 splitpath(), splitdir(), catpath() and catdir() by Barrie Slaymaker.
317 Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
319 This program is free software; you can redistribute it and/or modify
320 it under the same terms as Perl itself.