1File::Spec(3pm) Perl Programmers Reference Guide File::Spec(3pm)
2
3
4
6 File::Spec - portably perform operations on file names
7
9 use File::Spec;
10
11 $x=File::Spec->catfile('a', 'b', 'c');
12
13 which returns 'a/b/c' under Unix. Or:
14
15 use File::Spec::Functions;
16
17 $x = catfile('a', 'b', 'c');
18
20 This module is designed to support operations commonly performed on
21 file specifications (usually called "file names", but not to be
22 confused with the contents of a file, or Perl's file handles), such as
23 concatenating several directory and file names into a single path, or
24 determining whether a path is rooted. It is based on code directly
25 taken from MakeMaker 5.17, code written by Andreas Koenig, Andy
26 Dougherty, Charles Bailey, Ilya Zakharevich, Paul Schinder, and others.
27
28 Since these functions are different for most operating systems, each
29 set of OS specific routines is available in a separate module,
30 including:
31
32 File::Spec::Unix
33 File::Spec::Mac
34 File::Spec::OS2
35 File::Spec::Win32
36 File::Spec::VMS
37
38 The module appropriate for the current OS is automatically loaded by
39 File::Spec. Since some modules (like VMS) make use of facilities
40 available only under that OS, it may not be possible to load all
41 modules under all operating systems.
42
43 Since File::Spec is object oriented, subroutines should not be called
44 directly, as in:
45
46 File::Spec::catfile('a','b');
47
48 but rather as class methods:
49
50 File::Spec->catfile('a','b');
51
52 For simple uses, File::Spec::Functions provides convenient functional
53 forms of these methods.
54
56 canonpath
57 No physical check on the filesystem, but a logical cleanup of a path.
58
59 $cpath = File::Spec->canonpath( $path ) ;
60
61 Note that this does *not* collapse x/../y sections into y. This is
62 by design. If /foo on your system is a symlink to /bar/baz, then
63 /foo/../quux is actually /bar/quux, not /quux as a naive ../-removal
64 would give you. If you want to do this kind of processing, you
65 probably want "Cwd"'s "realpath()" function to actually traverse the
66 filesystem cleaning up paths like this.
67
68 catdir
69 Concatenate two or more directory names to form a complete path
70 ending with a directory. But remove the trailing slash from the
71 resulting string, because it doesn't look good, isn't necessary and
72 confuses OS/2. Of course, if this is the root directory, don't cut
73 off the trailing slash :-)
74
75 $path = File::Spec->catdir( @directories );
76
77 catfile
78 Concatenate one or more directory names and a filename to form a
79 complete path ending with a filename
80
81 $path = File::Spec->catfile( @directories, $filename );
82
83 curdir
84 Returns a string representation of the current directory.
85
86 $curdir = File::Spec->curdir();
87
88 devnull
89 Returns a string representation of the null device.
90
91 $devnull = File::Spec->devnull();
92
93 rootdir
94 Returns a string representation of the root directory.
95
96 $rootdir = File::Spec->rootdir();
97
98 tmpdir
99 Returns a string representation of the first writable directory from
100 a list of possible temporary directories. Returns the current
101 directory if no writable temporary directories are found. The list
102 of directories checked depends on the platform; e.g. File::Spec::Unix
103 checks $ENV{TMPDIR} (unless taint is on) and /tmp.
104
105 $tmpdir = File::Spec->tmpdir();
106
107 updir
108 Returns a string representation of the parent directory.
109
110 $updir = File::Spec->updir();
111
112 no_upwards
113 Given a list of file names, strip out those that refer to a parent
114 directory. (Does not strip symlinks, only '.', '..', and
115 equivalents.)
116
117 @paths = File::Spec->no_upwards( @paths );
118
119 case_tolerant
120 Returns a true or false value indicating, respectively, that
121 alphabetic case is not or is significant when comparing file
122 specifications.
123
124 $is_case_tolerant = File::Spec->case_tolerant();
125
126 file_name_is_absolute
127 Takes as its argument a path, and returns true if it is an absolute
128 path.
129
130 $is_absolute = File::Spec->file_name_is_absolute( $path );
131
132 This does not consult the local filesystem on Unix, Win32, OS/2, or
133 Mac OS (Classic). It does consult the working environment for VMS
134 (see "file_name_is_absolute" in File::Spec::VMS).
135
136 path
137 Takes no argument. Returns the environment variable "PATH" (or the
138 local platform's equivalent) as a list.
139
140 @PATH = File::Spec->path();
141
142 join
143 join is the same as catfile.
144
145 splitpath
146 Splits a path in to volume, directory, and filename portions. On
147 systems with no concept of volume, returns '' for volume.
148
149 ($volume,$directories,$file) = File::Spec->splitpath( $path );
150 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
151
152 For systems with no syntax differentiating filenames from
153 directories, assumes that the last file is a path unless $no_file is
154 true or a trailing separator or /. or /.. is present. On Unix, this
155 means that $no_file true makes this return ( '', $path, '' ).
156
157 The directory portion may or may not be returned with a trailing '/'.
158
159 The results can be passed to "catpath()" to get back a path
160 equivalent to (usually identical to) the original path.
161
162 splitdir
163 The opposite of "catdir()".
164
165 @dirs = File::Spec->splitdir( $directories );
166
167 $directories must be only the directory portion of the path on
168 systems that have the concept of a volume or that have path syntax
169 that differentiates files from directories.
170
171 Unlike just splitting the directories on the separator, empty
172 directory names ('') can be returned, because these are significant
173 on some OSes.
174
175 catpath()
176 Takes volume, directory and file portions and returns an entire path.
177 Under Unix, $volume is ignored, and directory and file are
178 concatenated. A '/' is inserted if need be. On other OSes, $volume
179 is significant.
180
181 $full_path = File::Spec->catpath( $volume, $directory, $file );
182
183 abs2rel
184 Takes a destination path and an optional base path returns a relative
185 path from the base path to the destination path:
186
187 $rel_path = File::Spec->abs2rel( $path ) ;
188 $rel_path = File::Spec->abs2rel( $path, $base ) ;
189
190 If $base is not present or '', then Cwd::cwd() is used. If $base is
191 relative, then it is converted to absolute form using "rel2abs()".
192 This means that it is taken to be relative to Cwd::cwd().
193
194 On systems with the concept of volume, if $path and $base appear to
195 be on two different volumes, we will not attempt to resolve the two
196 paths, and we will instead simply return $path. Note that previous
197 versions of this module ignored the volume of $base, which resulted
198 in garbage results part of the time.
199
200 On systems that have a grammar that indicates filenames, this ignores
201 the $base filename as well. Otherwise all path components are assumed
202 to be directories.
203
204 If $path is relative, it is converted to absolute form using
205 "rel2abs()". This means that it is taken to be relative to
206 Cwd::cwd().
207
208 No checks against the filesystem are made. On VMS, there is
209 interaction with the working environment, as logicals and macros are
210 expanded.
211
212 Based on code written by Shigio Yamaguchi.
213
214 rel2abs()
215 Converts a relative path to an absolute path.
216
217 $abs_path = File::Spec->rel2abs( $path ) ;
218 $abs_path = File::Spec->rel2abs( $path, $base ) ;
219
220 If $base is not present or '', then Cwd::cwd() is used. If $base is
221 relative, then it is converted to absolute form using "rel2abs()".
222 This means that it is taken to be relative to Cwd::cwd().
223
224 On systems with the concept of volume, if $path and $base appear to
225 be on two different volumes, we will not attempt to resolve the two
226 paths, and we will instead simply return $path. Note that previous
227 versions of this module ignored the volume of $base, which resulted
228 in garbage results part of the time.
229
230 On systems that have a grammar that indicates filenames, this ignores
231 the $base filename as well. Otherwise all path components are assumed
232 to be directories.
233
234 If $path is absolute, it is cleaned up and returned using
235 "canonpath()".
236
237 No checks against the filesystem are made. On VMS, there is
238 interaction with the working environment, as logicals and macros are
239 expanded.
240
241 Based on code written by Shigio Yamaguchi.
242
243 For further information, please see File::Spec::Unix, File::Spec::Mac,
244 File::Spec::OS2, File::Spec::Win32, or File::Spec::VMS.
245
247 File::Spec::Unix, File::Spec::Mac, File::Spec::OS2, File::Spec::Win32,
248 File::Spec::VMS, File::Spec::Functions, ExtUtils::MakeMaker
249
251 Currently maintained by Ken Williams "<KWILLIAMS@cpan.org>".
252
253 The vast majority of the code was written by Kenneth Albanowski
254 "<kjahds@kjahds.com>", Andy Dougherty "<doughera@lafayette.edu>",
255 Andreas Koenig "<A.Koenig@franz.ww.TU-Berlin.DE>", Tim Bunce
256 "<Tim.Bunce@ig.co.uk>". VMS support by Charles Bailey
257 "<bailey@newman.upenn.edu>". OS/2 support by Ilya Zakharevich
258 "<ilya@math.ohio-state.edu>". Mac support by Paul Schinder
259 "<schinder@pobox.com>", and Thomas Wegner "<wegner_thomas@yahoo.com>".
260 abs2rel() and rel2abs() written by Shigio Yamaguchi
261 "<shigio@tamacom.com>", modified by Barrie Slaymaker
262 "<barries@slaysys.com>". splitpath(), splitdir(), catpath() and
263 catdir() by Barrie Slaymaker.
264
266 Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
267
268 This program is free software; you can redistribute it and/or modify it
269 under the same terms as Perl itself.
270
271
272
273perl v5.12.4 2011-06-20 File::Spec(3pm)