This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix PathTools taint handling for Perl 5.6
[perl5.git] / dist / PathTools / lib / File / Spec.pm
CommitLineData
270d1e39
GS
1package File::Spec;
2
270d1e39 3use strict;
07824bd1 4use vars qw(@ISA $VERSION);
270d1e39 5
2ba334c7 6$VERSION = '3.68';
4f642d62 7$VERSION =~ tr/_//d;
270d1e39 8
cbc7acb0
JD
9my %module = (MacOS => 'Mac',
10 MSWin32 => 'Win32',
11 os2 => 'OS2',
fa6a1c44 12 VMS => 'VMS',
ecf68df6 13 epoc => 'Epoc',
ffa8448b 14 NetWare => 'Win32', # Yes, File::Spec::Win32 works on NetWare.
27da23d5 15 symbian => 'Win32', # Yes, File::Spec::Win32 works on symbian.
60598624 16 dos => 'OS2', # Yes, File::Spec::OS2 works on DJGPP.
36f66789
AB
17 cygwin => 'Cygwin',
18 amigaos => 'AmigaOS');
ecf68df6 19
cbc7acb0
JD
20
21my $module = $module{$^O} || 'Unix';
ecf68df6 22
cbc7acb0
JD
23require "File/Spec/$module.pm";
24@ISA = ("File::Spec::$module");
270d1e39
GS
25
261;
ae1c33a1 27
270d1e39
GS
28__END__
29
30=head1 NAME
31
32File::Spec - portably perform operations on file names
33
34=head1 SYNOPSIS
35
5c609535 36 use File::Spec;
270d1e39 37
5c609535 38 $x=File::Spec->catfile('a', 'b', 'c');
270d1e39 39
5c609535
JD
40which returns 'a/b/c' under Unix. Or:
41
42 use File::Spec::Functions;
43
44 $x = catfile('a', 'b', 'c');
270d1e39
GS
45
46=head1 DESCRIPTION
47
48This module is designed to support operations commonly performed on file
49specifications (usually called "file names", but not to be confused with the
50contents of a file, or Perl's file handles), such as concatenating several
51directory and file names into a single path, or determining whether a path
52is rooted. It is based on code directly taken from MakeMaker 5.17, code
53written by Andreas KE<ouml>nig, Andy Dougherty, Charles Bailey, Ilya
54Zakharevich, Paul Schinder, and others.
55
56Since these functions are different for most operating systems, each set of
57OS specific routines is available in a separate module, including:
58
59 File::Spec::Unix
60 File::Spec::Mac
61 File::Spec::OS2
62 File::Spec::Win32
63 File::Spec::VMS
64
65The module appropriate for the current OS is automatically loaded by
5c609535
JD
66File::Spec. Since some modules (like VMS) make use of facilities available
67only under that OS, it may not be possible to load all modules under all
68operating systems.
270d1e39 69
2586ba89 70Since File::Spec is object oriented, subroutines should not be called directly,
270d1e39
GS
71as in:
72
73 File::Spec::catfile('a','b');
5c609535 74
270d1e39
GS
75but rather as class methods:
76
77 File::Spec->catfile('a','b');
78
5c609535
JD
79For simple uses, L<File::Spec::Functions> provides convenient functional
80forms of these methods.
81
ae1c33a1
RK
82=head1 METHODS
83
84=over 2
85
5813de03 86=item canonpath
110c90cc 87X<canonpath>
ae1c33a1
RK
88
89No physical check on the filesystem, but a logical cleanup of a
90path.
91
92 $cpath = File::Spec->canonpath( $path ) ;
93
60598624
RGS
94Note that this does *not* collapse F<x/../y> sections into F<y>. This
95is by design. If F</foo> on your system is a symlink to F</bar/baz>,
96then F</foo/../quux> is actually F</bar/quux>, not F</quux> as a naive
97F<../>-removal would give you. If you want to do this kind of
98processing, you probably want C<Cwd>'s C<realpath()> function to
99actually traverse the filesystem cleaning up paths like this.
100
5813de03 101=item catdir
110c90cc 102X<catdir>
ae1c33a1
RK
103
104Concatenate two or more directory names to form a complete path ending
105with a directory. But remove the trailing slash from the resulting
106string, because it doesn't look good, isn't necessary and confuses
5b287435 107OS/2. Of course, if this is the root directory, don't cut off the
ae1c33a1
RK
108trailing slash :-)
109
110 $path = File::Spec->catdir( @directories );
111
112=item catfile
110c90cc 113X<catfile>
ae1c33a1
RK
114
115Concatenate one or more directory names and a filename to form a
116complete path ending with a filename
117
118 $path = File::Spec->catfile( @directories, $filename );
119
120=item curdir
110c90cc 121X<curdir>
ae1c33a1
RK
122
123Returns a string representation of the current directory.
124
125 $curdir = File::Spec->curdir();
126
127=item devnull
110c90cc 128X<devnull>
ae1c33a1
RK
129
130Returns a string representation of the null device.
131
132 $devnull = File::Spec->devnull();
133
134=item rootdir
110c90cc 135X<rootdir>
ae1c33a1
RK
136
137Returns a string representation of the root directory.
138
139 $rootdir = File::Spec->rootdir();
140
141=item tmpdir
110c90cc 142X<tmpdir>
ae1c33a1
RK
143
144Returns a string representation of the first writable directory from a
07824bd1
JH
145list of possible temporary directories. Returns the current directory
146if no writable temporary directories are found. The list of directories
5b287435
RGS
147checked depends on the platform; e.g. File::Spec::Unix checks C<$ENV{TMPDIR}>
148(unless taint is on) and F</tmp>.
ae1c33a1
RK
149
150 $tmpdir = File::Spec->tmpdir();
151
152=item updir
110c90cc 153X<updir>
ae1c33a1
RK
154
155Returns a string representation of the parent directory.
156
157 $updir = File::Spec->updir();
158
159=item no_upwards
160
daa60504
DG
161Given a list of files in a directory (such as from C<readdir()>),
162strip out C<'.'> and C<'..'>.
ae1c33a1 163
daa60504
DG
164B<SECURITY NOTE:> This does NOT filter paths containing C<'..'>, like
165C<'../../../../etc/passwd'>, only literal matches to C<'.'> and C<'..'>.
166
167 @paths = File::Spec->no_upwards( readdir $dirhandle );
ae1c33a1
RK
168
169=item case_tolerant
170
171Returns a true or false value indicating, respectively, that alphabetic
5b287435 172case is not or is significant when comparing file specifications.
8e58c70c 173Cygwin and Win32 accept an optional drive argument.
ae1c33a1
RK
174
175 $is_case_tolerant = File::Spec->case_tolerant();
176
177=item file_name_is_absolute
178
5b287435 179Takes as its argument a path, and returns true if it is an absolute path.
ae1c33a1
RK
180
181 $is_absolute = File::Spec->file_name_is_absolute( $path );
182
2586ba89
JH
183This does not consult the local filesystem on Unix, Win32, OS/2, or
184Mac OS (Classic). It does consult the working environment for VMS
185(see L<File::Spec::VMS/file_name_is_absolute>).
ae1c33a1
RK
186
187=item path
110c90cc 188X<path>
ae1c33a1 189
5b287435 190Takes no argument. Returns the environment variable C<PATH> (or the local
638113eb 191platform's equivalent) as a list.
ae1c33a1
RK
192
193 @PATH = File::Spec->path();
194
195=item join
110c90cc 196X<join, path>
ae1c33a1
RK
197
198join is the same as catfile.
199
200=item splitpath
110c90cc 201X<splitpath> X<split, path>
ae1c33a1
RK
202
203Splits a path in to volume, directory, and filename portions. On systems
40d020d9 204with no concept of volume, returns '' for volume.
ae1c33a1 205
2674d7ed
FC
206 ($volume,$directories,$file) =
207 File::Spec->splitpath( $path );
208 ($volume,$directories,$file) =
209 File::Spec->splitpath( $path, $no_file );
ae1c33a1
RK
210
211For systems with no syntax differentiating filenames from directories,
5b287435
RGS
212assumes that the last file is a path unless C<$no_file> is true or a
213trailing separator or F</.> or F</..> is present. On Unix, this means that C<$no_file>
ae1c33a1
RK
214true makes this return ( '', $path, '' ).
215
216The directory portion may or may not be returned with a trailing '/'.
217
218The results can be passed to L</catpath()> to get back a path equivalent to
219(usually identical to) the original path.
220
221=item splitdir
110c90cc 222X<splitdir> X<split, dir>
ae1c33a1 223
2a6dc374 224The opposite of L</catdir>.
ae1c33a1
RK
225
226 @dirs = File::Spec->splitdir( $directories );
227
5b287435 228C<$directories> must be only the directory portion of the path on systems
ae1c33a1
RK
229that have the concept of a volume or that have path syntax that differentiates
230files from directories.
231
232Unlike just splitting the directories on the separator, empty
233directory names (C<''>) can be returned, because these are significant
5b287435 234on some OSes.
ae1c33a1 235
59605c55 236=item catpath()
ae1c33a1
RK
237
238Takes volume, directory and file portions and returns an entire path. Under
5b287435
RGS
239Unix, C<$volume> is ignored, and directory and file are concatenated. A '/' is
240inserted if need be. On other OSes, C<$volume> is significant.
ae1c33a1
RK
241
242 $full_path = File::Spec->catpath( $volume, $directory, $file );
243
244=item abs2rel
110c90cc 245X<abs2rel> X<absolute, path> X<relative, path>
ae1c33a1
RK
246
247Takes a destination path and an optional base path returns a relative path
248from the base path to the destination path:
249
250 $rel_path = File::Spec->abs2rel( $path ) ;
251 $rel_path = File::Spec->abs2rel( $path, $base ) ;
252
23bb49fa 253If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is
8d48b1f5
JH
254relative, then it is converted to absolute form using
255L</rel2abs()>. This means that it is taken to be relative to
23bb49fa 256L<Cwd::cwd()|Cwd>.
8d48b1f5 257
5b287435 258On systems with the concept of volume, if C<$path> and C<$base> appear to be
638113eb 259on two different volumes, we will not attempt to resolve the two
5b287435
RGS
260paths, and we will instead simply return C<$path>. Note that previous
261versions of this module ignored the volume of C<$base>, which resulted in
638113eb 262garbage results part of the time.
ae1c33a1
RK
263
264On systems that have a grammar that indicates filenames, this ignores the
5b287435 265C<$base> filename as well. Otherwise all path components are assumed to be
ae1c33a1
RK
266directories.
267
5b287435 268If C<$path> is relative, it is converted to absolute form using L</rel2abs()>.
23bb49fa 269This means that it is taken to be relative to L<Cwd::cwd()|Cwd>.
ae1c33a1 270
2586ba89 271No checks against the filesystem are made. On VMS, there is
ae1c33a1
RK
272interaction with the working environment, as logicals and
273macros are expanded.
274
275Based on code written by Shigio Yamaguchi.
276
59605c55 277=item rel2abs()
110c90cc 278X<rel2abs> X<absolute, path> X<relative, path>
ae1c33a1
RK
279
280Converts a relative path to an absolute path.
281
282 $abs_path = File::Spec->rel2abs( $path ) ;
283 $abs_path = File::Spec->rel2abs( $path, $base ) ;
284
23bb49fa 285If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is relative,
ae1c33a1 286then it is converted to absolute form using L</rel2abs()>. This means that it
23bb49fa 287is taken to be relative to L<Cwd::cwd()|Cwd>.
ae1c33a1 288
5b287435 289On systems with the concept of volume, if C<$path> and C<$base> appear to be
638113eb 290on two different volumes, we will not attempt to resolve the two
5b287435
RGS
291paths, and we will instead simply return C<$path>. Note that previous
292versions of this module ignored the volume of C<$base>, which resulted in
638113eb 293garbage results part of the time.
ae1c33a1
RK
294
295On systems that have a grammar that indicates filenames, this ignores the
5b287435 296C<$base> filename as well. Otherwise all path components are assumed to be
ae1c33a1
RK
297directories.
298
2a6dc374 299If C<$path> is absolute, it is cleaned up and returned using L</canonpath>.
ae1c33a1 300
2586ba89 301No checks against the filesystem are made. On VMS, there is
ae1c33a1
RK
302interaction with the working environment, as logicals and
303macros are expanded.
304
305Based on code written by Shigio Yamaguchi.
306
307=back
308
309For further information, please see L<File::Spec::Unix>,
310L<File::Spec::Mac>, L<File::Spec::OS2>, L<File::Spec::Win32>, or
311L<File::Spec::VMS>.
270d1e39
GS
312
313=head1 SEE ALSO
314
ae1c33a1
RK
315L<File::Spec::Unix>, L<File::Spec::Mac>, L<File::Spec::OS2>,
316L<File::Spec::Win32>, L<File::Spec::VMS>, L<File::Spec::Functions>,
317L<ExtUtils::MakeMaker>
270d1e39 318
5b287435
RGS
319=head1 AUTHOR
320
321Currently maintained by Ken Williams C<< <KWILLIAMS@cpan.org> >>.
322
323The vast majority of the code was written by
324Kenneth Albanowski C<< <kjahds@kjahds.com> >>,
325Andy Dougherty C<< <doughera@lafayette.edu> >>,
326Andreas KE<ouml>nig C<< <A.Koenig@franz.ww.TU-Berlin.DE> >>,
327Tim Bunce C<< <Tim.Bunce@ig.co.uk> >>.
328VMS support by Charles Bailey C<< <bailey@newman.upenn.edu> >>.
329OS/2 support by Ilya Zakharevich C<< <ilya@math.ohio-state.edu> >>.
330Mac support by Paul Schinder C<< <schinder@pobox.com> >>, and
331Thomas Wegner C<< <wegner_thomas@yahoo.com> >>.
332abs2rel() and rel2abs() written by Shigio Yamaguchi C<< <shigio@tamacom.com> >>,
333modified by Barrie Slaymaker C<< <barries@slaysys.com> >>.
334splitpath(), splitdir(), catpath() and catdir() by Barrie Slaymaker.
e021ab8e 335
99f36a73
RGS
336=head1 COPYRIGHT
337
d10039d7 338Copyright (c) 2004-2013 by the Perl 5 Porters. All rights reserved.
99f36a73
RGS
339
340This program is free software; you can redistribute it and/or modify
341it under the same terms as Perl itself.
342
e021ab8e 343=cut