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