xref: /openbsd-src/gnu/usr.bin/perl/dist/PathTools/lib/File/Spec.pm (revision 897fc685943471cf985a0fe38ba076ea6fe74fa5)
1package File::Spec;
2
3use strict;
4use vars qw(@ISA $VERSION);
5
6$VERSION = '3.63_01';
7$VERSION =~ tr/_//d;
8
9my %module = (MacOS   => 'Mac',
10	      MSWin32 => 'Win32',
11	      os2     => 'OS2',
12	      VMS     => 'VMS',
13	      epoc    => 'Epoc',
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.
17	      cygwin  => 'Cygwin',
18	      amigaos => 'AmigaOS');
19
20
21my $module = $module{$^O} || 'Unix';
22
23require "File/Spec/$module.pm";
24@ISA = ("File::Spec::$module");
25
261;
27
28__END__
29
30=head1 NAME
31
32File::Spec - portably perform operations on file names
33
34=head1 SYNOPSIS
35
36	use File::Spec;
37
38	$x=File::Spec->catfile('a', 'b', 'c');
39
40which returns 'a/b/c' under Unix. Or:
41
42	use File::Spec::Functions;
43
44	$x = catfile('a', 'b', 'c');
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
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.
69
70Since File::Spec is object oriented, subroutines should not be called directly,
71as in:
72
73	File::Spec::catfile('a','b');
74
75but rather as class methods:
76
77	File::Spec->catfile('a','b');
78
79For simple uses, L<File::Spec::Functions> provides convenient functional
80forms of these methods.
81
82=head1 METHODS
83
84=over 2
85
86=item canonpath
87X<canonpath>
88
89No physical check on the filesystem, but a logical cleanup of a
90path.
91
92    $cpath = File::Spec->canonpath( $path ) ;
93
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
101=item catdir
102X<catdir>
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
107OS/2. Of course, if this is the root directory, don't cut off the
108trailing slash :-)
109
110    $path = File::Spec->catdir( @directories );
111
112=item catfile
113X<catfile>
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
121X<curdir>
122
123Returns a string representation of the current directory.
124
125    $curdir = File::Spec->curdir();
126
127=item devnull
128X<devnull>
129
130Returns a string representation of the null device.
131
132    $devnull = File::Spec->devnull();
133
134=item rootdir
135X<rootdir>
136
137Returns a string representation of the root directory.
138
139    $rootdir = File::Spec->rootdir();
140
141=item tmpdir
142X<tmpdir>
143
144Returns a string representation of the first writable directory from a
145list of possible temporary directories.  Returns the current directory
146if no writable temporary directories are found.  The list of directories
147checked depends on the platform; e.g. File::Spec::Unix checks C<$ENV{TMPDIR}>
148(unless taint is on) and F</tmp>.
149
150    $tmpdir = File::Spec->tmpdir();
151
152=item updir
153X<updir>
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
169case is not or is significant when comparing file specifications.
170Cygwin and Win32 accept an optional drive argument.
171
172    $is_case_tolerant = File::Spec->case_tolerant();
173
174=item file_name_is_absolute
175
176Takes as its argument a path, and returns true if it is an absolute path.
177
178    $is_absolute = File::Spec->file_name_is_absolute( $path );
179
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>).
183
184=item path
185X<path>
186
187Takes no argument.  Returns the environment variable C<PATH> (or the local
188platform's equivalent) as a list.
189
190    @PATH = File::Spec->path();
191
192=item join
193X<join, path>
194
195join is the same as catfile.
196
197=item splitpath
198X<splitpath> X<split, path>
199
200Splits a path in to volume, directory, and filename portions. On systems
201with no concept of volume, returns '' for volume.
202
203    ($volume,$directories,$file) =
204                       File::Spec->splitpath( $path );
205    ($volume,$directories,$file) =
206                       File::Spec->splitpath( $path, $no_file );
207
208For systems with no syntax differentiating filenames from directories,
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>
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
219X<splitdir> X<split, dir>
220
221The opposite of L</catdir>.
222
223    @dirs = File::Spec->splitdir( $directories );
224
225C<$directories> must be only the directory portion of the path on systems
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
231on some OSes.
232
233=item catpath()
234
235Takes volume, directory and file portions and returns an entire path. Under
236Unix, C<$volume> is ignored, and directory and file are concatenated.  A '/' is
237inserted if need be.  On other OSes, C<$volume> is significant.
238
239    $full_path = File::Spec->catpath( $volume, $directory, $file );
240
241=item abs2rel
242X<abs2rel> X<absolute, path> X<relative, path>
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
250If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is
251relative, then it is converted to absolute form using
252L</rel2abs()>. This means that it is taken to be relative to
253L<Cwd::cwd()|Cwd>.
254
255On systems with the concept of volume, if C<$path> and C<$base> appear to be
256on two different volumes, we will not attempt to resolve the two
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
259garbage results part of the time.
260
261On systems that have a grammar that indicates filenames, this ignores the
262C<$base> filename as well. Otherwise all path components are assumed to be
263directories.
264
265If C<$path> is relative, it is converted to absolute form using L</rel2abs()>.
266This means that it is taken to be relative to L<Cwd::cwd()|Cwd>.
267
268No checks against the filesystem are made.  On VMS, there is
269interaction with the working environment, as logicals and
270macros are expanded.
271
272Based on code written by Shigio Yamaguchi.
273
274=item rel2abs()
275X<rel2abs> X<absolute, path> X<relative, path>
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
282If C<$base> is not present or '', then L<Cwd::cwd()|Cwd> is used. If C<$base> is relative,
283then it is converted to absolute form using L</rel2abs()>. This means that it
284is taken to be relative to L<Cwd::cwd()|Cwd>.
285
286On systems with the concept of volume, if C<$path> and C<$base> appear to be
287on two different volumes, we will not attempt to resolve the two
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
290garbage results part of the time.
291
292On systems that have a grammar that indicates filenames, this ignores the
293C<$base> filename as well. Otherwise all path components are assumed to be
294directories.
295
296If C<$path> is absolute, it is cleaned up and returned using L</canonpath>.
297
298No checks against the filesystem are made.  On VMS, there is
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>.
309
310=head1 SEE ALSO
311
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>
315
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.
332
333=head1 COPYRIGHT
334
335Copyright (c) 2004-2013 by the Perl 5 Porters.  All rights reserved.
336
337This program is free software; you can redistribute it and/or modify
338it under the same terms as Perl itself.
339
340=cut
341