This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
S_do_op_dump_bar(): don't print TRANS op_pv field
[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
7ebb7b4a 6$VERSION = '3.66';
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
161Given a list of file names, strip out those that refer to a parent
162directory. (Does not strip symlinks, only '.', '..', and equivalents.)
163
164 @paths = File::Spec->no_upwards( @paths );
165
166=item case_tolerant
167
168Returns a true or false value indicating, respectively, that alphabetic
5b287435 169case is not or is significant when comparing file specifications.
8e58c70c 170Cygwin and Win32 accept an optional drive argument.
ae1c33a1
RK
171
172 $is_case_tolerant = File::Spec->case_tolerant();
173
174=item file_name_is_absolute
175
5b287435 176Takes as its argument a path, and returns true if it is an absolute path.
ae1c33a1
RK
177
178 $is_absolute = File::Spec->file_name_is_absolute( $path );
179
2586ba89
JH
180This does not consult the local filesystem on Unix, Win32, OS/2, or
181Mac OS (Classic). It does consult the working environment for VMS
182(see L<File::Spec::VMS/file_name_is_absolute>).
ae1c33a1
RK
183
184=item path
110c90cc 185X<path>
ae1c33a1 186
5b287435 187Takes no argument. Returns the environment variable C<PATH> (or the local
638113eb 188platform's equivalent) as a list.
ae1c33a1
RK
189
190 @PATH = File::Spec->path();
191
192=item join
110c90cc 193X<join, path>
ae1c33a1
RK
194
195join is the same as catfile.
196
197=item splitpath
110c90cc 198X<splitpath> X<split, path>
ae1c33a1
RK
199
200Splits a path in to volume, directory, and filename portions. On systems
40d020d9 201with no concept of volume, returns '' for volume.
ae1c33a1 202
2674d7ed
FC
203 ($volume,$directories,$file) =
204 File::Spec->splitpath( $path );
205 ($volume,$directories,$file) =
206 File::Spec->splitpath( $path, $no_file );
ae1c33a1
RK
207
208For systems with no syntax differentiating filenames from directories,
5b287435
RGS
209assumes that the last file is a path unless C<$no_file> is true or a
210trailing separator or F</.> or F</..> is present. On Unix, this means that C<$no_file>
ae1c33a1
RK
211true makes this return ( '', $path, '' ).
212
213The directory portion may or may not be returned with a trailing '/'.
214
215The results can be passed to L</catpath()> to get back a path equivalent to
216(usually identical to) the original path.
217
218=item splitdir
110c90cc 219X<splitdir> X<split, dir>
ae1c33a1 220
2a6dc374 221The opposite of L</catdir>.
ae1c33a1
RK
222
223 @dirs = File::Spec->splitdir( $directories );
224
5b287435 225C<$directories> must be only the directory portion of the path on systems
ae1c33a1
RK
226that have the concept of a volume or that have path syntax that differentiates
227files from directories.
228
229Unlike just splitting the directories on the separator, empty
230directory names (C<''>) can be returned, because these are significant
5b287435 231on some OSes.
ae1c33a1 232
59605c55 233=item catpath()
ae1c33a1
RK
234
235Takes volume, directory and file portions and returns an entire path. Under
5b287435
RGS
236Unix, C<$volume> is ignored, and directory and file are concatenated. A '/' is
237inserted if need be. On other OSes, C<$volume> is significant.
ae1c33a1
RK
238
239 $full_path = File::Spec->catpath( $volume, $directory, $file );
240
241=item abs2rel
110c90cc 242X<abs2rel> X<absolute, path> X<relative, path>
ae1c33a1
RK
243
244Takes a destination path and an optional base path returns a relative path
245from the base path to the destination path:
246
247 $rel_path = File::Spec->abs2rel( $path ) ;
248 $rel_path = File::Spec->abs2rel( $path, $base ) ;
249
23bb49fa 250If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is
8d48b1f5
JH
251relative, then it is converted to absolute form using
252L</rel2abs()>. This means that it is taken to be relative to
23bb49fa 253L<Cwd::cwd()|Cwd>.
8d48b1f5 254
5b287435 255On systems with the concept of volume, if C<$path> and C<$base> appear to be
638113eb 256on two different volumes, we will not attempt to resolve the two
5b287435
RGS
257paths, and we will instead simply return C<$path>. Note that previous
258versions of this module ignored the volume of C<$base>, which resulted in
638113eb 259garbage results part of the time.
ae1c33a1
RK
260
261On systems that have a grammar that indicates filenames, this ignores the
5b287435 262C<$base> filename as well. Otherwise all path components are assumed to be
ae1c33a1
RK
263directories.
264
5b287435 265If C<$path> is relative, it is converted to absolute form using L</rel2abs()>.
23bb49fa 266This means that it is taken to be relative to L<Cwd::cwd()|Cwd>.
ae1c33a1 267
2586ba89 268No checks against the filesystem are made. On VMS, there is
ae1c33a1
RK
269interaction with the working environment, as logicals and
270macros are expanded.
271
272Based on code written by Shigio Yamaguchi.
273
59605c55 274=item rel2abs()
110c90cc 275X<rel2abs> X<absolute, path> X<relative, path>
ae1c33a1
RK
276
277Converts a relative path to an absolute path.
278
279 $abs_path = File::Spec->rel2abs( $path ) ;
280 $abs_path = File::Spec->rel2abs( $path, $base ) ;
281
23bb49fa 282If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is relative,
ae1c33a1 283then it is converted to absolute form using L</rel2abs()>. This means that it
23bb49fa 284is taken to be relative to L<Cwd::cwd()|Cwd>.
ae1c33a1 285
5b287435 286On systems with the concept of volume, if C<$path> and C<$base> appear to be
638113eb 287on two different volumes, we will not attempt to resolve the two
5b287435
RGS
288paths, and we will instead simply return C<$path>. Note that previous
289versions of this module ignored the volume of C<$base>, which resulted in
638113eb 290garbage results part of the time.
ae1c33a1
RK
291
292On systems that have a grammar that indicates filenames, this ignores the
5b287435 293C<$base> filename as well. Otherwise all path components are assumed to be
ae1c33a1
RK
294directories.
295
2a6dc374 296If C<$path> is absolute, it is cleaned up and returned using L</canonpath>.
ae1c33a1 297
2586ba89 298No checks against the filesystem are made. On VMS, there is
ae1c33a1
RK
299interaction with the working environment, as logicals and
300macros are expanded.
301
302Based on code written by Shigio Yamaguchi.
303
304=back
305
306For further information, please see L<File::Spec::Unix>,
307L<File::Spec::Mac>, L<File::Spec::OS2>, L<File::Spec::Win32>, or
308L<File::Spec::VMS>.
270d1e39
GS
309
310=head1 SEE ALSO
311
ae1c33a1
RK
312L<File::Spec::Unix>, L<File::Spec::Mac>, L<File::Spec::OS2>,
313L<File::Spec::Win32>, L<File::Spec::VMS>, L<File::Spec::Functions>,
314L<ExtUtils::MakeMaker>
270d1e39 315
5b287435
RGS
316=head1 AUTHOR
317
318Currently maintained by Ken Williams C<< <KWILLIAMS@cpan.org> >>.
319
320The vast majority of the code was written by
321Kenneth Albanowski C<< <kjahds@kjahds.com> >>,
322Andy Dougherty C<< <doughera@lafayette.edu> >>,
323Andreas KE<ouml>nig C<< <A.Koenig@franz.ww.TU-Berlin.DE> >>,
324Tim Bunce C<< <Tim.Bunce@ig.co.uk> >>.
325VMS support by Charles Bailey C<< <bailey@newman.upenn.edu> >>.
326OS/2 support by Ilya Zakharevich C<< <ilya@math.ohio-state.edu> >>.
327Mac support by Paul Schinder C<< <schinder@pobox.com> >>, and
328Thomas Wegner C<< <wegner_thomas@yahoo.com> >>.
329abs2rel() and rel2abs() written by Shigio Yamaguchi C<< <shigio@tamacom.com> >>,
330modified by Barrie Slaymaker C<< <barries@slaysys.com> >>.
331splitpath(), splitdir(), catpath() and catdir() by Barrie Slaymaker.
e021ab8e 332
99f36a73
RGS
333=head1 COPYRIGHT
334
d10039d7 335Copyright (c) 2004-2013 by the Perl 5 Porters. All rights reserved.
99f36a73
RGS
336
337This program is free software; you can redistribute it and/or modify
338it under the same terms as Perl itself.
339
e021ab8e 340=cut