1File::Spec::Mac(3pm) Perl Programmers Reference Guide File::Spec::Mac(3pm)
2
3
4
6 File::Spec::Mac - File::Spec for Mac OS (Classic)
7
9 require File::Spec::Mac; # Done internally by File::Spec if needed
10
12 Methods for manipulating file specifications.
13
15 canonpath
16 On Mac OS, there's nothing to be done. Returns what it's given.
17
18 catdir()
19 Concatenate two or more directory names to form a path separated by
20 colons (":") ending with a directory. Resulting paths are relative by
21 default, but can be forced to be absolute (but avoid this, see
22 below). Automatically puts a trailing ":" on the end of the complete
23 path, because that's what's done in MacPerl's environment and helps
24 to distinguish a file path from a directory path.
25
26 IMPORTANT NOTE: Beginning with version 1.3 of this module, the
27 resulting path is relative by default and not absolute. This decision
28 was made due to portability reasons. Since "File::Spec->catdir()"
29 returns relative paths on all other operating systems, it will now
30 also follow this convention on Mac OS. Note that this may break some
31 existing scripts.
32
33 The intended purpose of this routine is to concatenate directory
34 names. But because of the nature of Macintosh paths, some additional
35 possibilities are allowed to make using this routine give reasonable
36 results for some common situations. In other words, you are also
37 allowed to concatenate paths instead of directory names (strictly
38 speaking, a string like ":a" is a path, but not a name, since it con‐
39 tains a punctuation character ":").
40
41 So, beside calls like
42
43 catdir("a") = ":a:"
44 catdir("a","b") = ":a:b:"
45 catdir() = "" (special case)
46
47 calls like the following
48
49 catdir(":a:") = ":a:"
50 catdir(":a","b") = ":a:b:"
51 catdir(":a:","b") = ":a:b:"
52 catdir(":a:",":b:") = ":a:b:"
53 catdir(":") = ":"
54
55 are allowed.
56
57 Here are the rules that are used in "catdir()"; note that we try to
58 be as compatible as possible to Unix:
59
60 1.
61 The resulting path is relative by default, i.e. the resulting path
62 will have a leading colon.
63
64 2.
65 A trailing colon is added automatically to the resulting path, to
66 denote a directory.
67
68 3.
69 Generally, each argument has one leading ":" and one trailing ":"
70 removed (if any). They are then joined together by a ":". Special
71 treatment applies for arguments denoting updir paths like "::lib:",
72 see (4), or arguments consisting solely of colons ("colon paths"),
73 see (5).
74
75 4.
76 When an updir path like ":::lib::" is passed as argument, the num‐
77 ber of directories to climb up is handled correctly, not removing
78 leading or trailing colons when necessary. E.g.
79
80 catdir(":::a","::b","c") = ":::a::b:c:"
81 catdir(":::a::","::b","c") = ":::a:::b:c:"
82
83 5.
84 Adding a colon ":" or empty string "" to a path at any position
85 doesn't alter the path, i.e. these arguments are ignored. (When a
86 "" is passed as the first argument, it has a special meaning, see
87 (6)). This way, a colon ":" is handled like a "." (curdir) on Unix,
88 while an empty string "" is generally ignored (see "Unix->canon‐
89 path()" ). Likewise, a "::" is handled like a ".." (updir), and a
90 ":::" is handled like a "../.." etc. E.g.
91
92 catdir("a",":",":","b") = ":a:b:"
93 catdir("a",":","::",":b") = ":a::b:"
94
95 6.
96 If the first argument is an empty string "" or is a volume name,
97 i.e. matches the pattern /^[^:]+:/, the resulting path is absolute.
98
99 7.
100 Passing an empty string "" as the first argument to "catdir()" is
101 like passing"File::Spec->rootdir()" as the first argument, i.e.
102
103 catdir("","a","b") is the same as
104
105 catdir(rootdir(),"a","b").
106
107 This is true on Unix, where "catdir("","a","b")" yields "/a/b" and
108 "rootdir()" is "/". Note that "rootdir()" on Mac OS is the startup
109 volume, which is the closest in concept to Unix' "/". This should
110 help to run existing scripts originally written for Unix.
111
112 8.
113 For absolute paths, some cleanup is done, to ensure that the volume
114 name isn't immediately followed by updirs. This is invalid, because
115 this would go beyond "root". Generally, these cases are handled
116 like their Unix counterparts:
117
118 Unix:
119 Unix->catdir("","") = "/"
120 Unix->catdir("",".") = "/"
121 Unix->catdir("","..") = "/" # can't go beyond root
122 Unix->catdir("",".","..","..","a") = "/a"
123 Mac:
124 Mac->catdir("","") = rootdir() # (e.g. "HD:")
125 Mac->catdir("",":") = rootdir()
126 Mac->catdir("","::") = rootdir() # can't go beyond root
127 Mac->catdir("",":","::","::","a") = rootdir() . "a:" # (e.g. "HD:a:")
128
129 However, this approach is limited to the first arguments following
130 "root" (again, see "Unix->canonpath()" ). If there are more argu‐
131 ments that move up the directory tree, an invalid path going beyond
132 root can be created.
133
134 As you've seen, you can force "catdir()" to create an absolute path
135 by passing either an empty string or a path that begins with a volume
136 name as the first argument. However, you are strongly encouraged not
137 to do so, since this is done only for backward compatibility. Newer
138 versions of File::Spec come with a method called "catpath()" (see
139 below), that is designed to offer a portable solution for the cre‐
140 ation of absolute paths. It takes volume, directory and file por‐
141 tions and returns an entire path. While "catdir()" is still suitable
142 for the concatenation of directory names, you are encouraged to use
143 "catpath()" to concatenate volume names and directory paths. E.g.
144
145 $dir = File::Spec->catdir("tmp","sources");
146 $abs_path = File::Spec->catpath("MacintoshHD:", $dir,"");
147
148 yields
149
150 "MacintoshHD:tmp:sources:" .
151
152 catfile
153 Concatenate one or more directory names and a filename to form a com‐
154 plete path ending with a filename. Resulting paths are relative by
155 default, but can be forced to be absolute (but avoid this).
156
157 IMPORTANT NOTE: Beginning with version 1.3 of this module, the
158 resulting path is relative by default and not absolute. This decision
159 was made due to portability reasons. Since "File::Spec->catfile()"
160 returns relative paths on all other operating systems, it will now
161 also follow this convention on Mac OS. Note that this may break some
162 existing scripts.
163
164 The last argument is always considered to be the file portion. Since
165 "catfile()" uses "catdir()" (see above) for the concatenation of the
166 directory portions (if any), the following with regard to relative
167 and absolute paths is true:
168
169 catfile("") = ""
170 catfile("file") = "file"
171
172 but
173
174 catfile("","") = rootdir() # (e.g. "HD:")
175 catfile("","file") = rootdir() . file # (e.g. "HD:file")
176 catfile("HD:","file") = "HD:file"
177
178 This means that "catdir()" is called only when there are two or more
179 arguments, as one might expect.
180
181 Note that the leading ":" is removed from the filename, so that
182
183 catfile("a","b","file") = ":a:b:file" and
184
185 catfile("a","b",":file") = ":a:b:file"
186
187 give the same answer.
188
189 To concatenate volume names, directory paths and filenames, you are
190 encouraged to use "catpath()" (see below).
191
192 curdir
193 Returns a string representing the current directory. On Mac OS, this
194 is ":".
195
196 devnull
197 Returns a string representing the null device. On Mac OS, this is
198 "Dev:Null".
199
200 rootdir
201 Returns a string representing the root directory. Under MacPerl,
202 returns the name of the startup volume, since that's the closest in
203 concept, although other volumes aren't rooted there. The name has a
204 trailing ":", because that's the correct specification for a volume
205 name on Mac OS.
206
207 If Mac::Files could not be loaded, the empty string is returned.
208
209 tmpdir
210 Returns the contents of $ENV{TMPDIR}, if that directory exits or the
211 current working directory otherwise. Under MacPerl, $ENV{TMPDIR} will
212 contain a path like "MacintoshHD:Temporary Items:", which is a hidden
213 directory on your startup volume.
214
215 updir
216 Returns a string representing the parent directory. On Mac OS, this
217 is "::".
218
219 file_name_is_absolute
220 Takes as argument a path and returns true, if it is an absolute path.
221 If the path has a leading ":", it's a relative path. Otherwise, it's
222 an absolute path, unless the path doesn't contain any colons, i.e.
223 it's a name like "a". In this particular case, the path is considered
224 to be relative (i.e. it is considered to be a filename). Use ":" in
225 the appropriate place in the path if you want to distinguish unam‐
226 biguously. As a special case, the filename '' is always considered to
227 be absolute. Note that with version 1.2 of File::Spec::Mac, this does
228 no longer consult the local filesystem.
229
230 E.g.
231
232 File::Spec->file_name_is_absolute("a"); # false (relative)
233 File::Spec->file_name_is_absolute(":a:b:"); # false (relative)
234 File::Spec->file_name_is_absolute("MacintoshHD:"); # true (absolute)
235 File::Spec->file_name_is_absolute(""); # true (absolute)
236
237 path
238 Returns the null list for the MacPerl application, since the concept
239 is usually meaningless under Mac OS. But if you're using the MacPerl
240 tool under MPW, it gives back $ENV{Commands} suitably split, as is
241 done in :lib:ExtUtils:MM_Mac.pm.
242
243 splitpath
244 ($volume,$directories,$file) = File::Spec->splitpath( $path );
245 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
246
247 Splits a path into volume, directory, and filename portions.
248
249 On Mac OS, assumes that the last part of the path is a filename
250 unless $no_file is true or a trailing separator ":" is present.
251
252 The volume portion is always returned with a trailing ":". The direc‐
253 tory portion is always returned with a leading (to denote a relative
254 path) and a trailing ":" (to denote a directory). The file portion is
255 always returned without a leading ":". Empty portions are returned
256 as empty string ''.
257
258 The results can be passed to "catpath()" to get back a path equiva‐
259 lent to (usually identical to) the original path.
260
261 splitdir
262 The opposite of "catdir()".
263
264 @dirs = File::Spec->splitdir( $directories );
265
266 $directories should be only the directory portion of the path on sys‐
267 tems that have the concept of a volume or that have path syntax that
268 differentiates files from directories. Consider using "splitpath()"
269 otherwise.
270
271 Unlike just splitting the directories on the separator, empty direc‐
272 tory names ("") can be returned. Since "catdir()" on Mac OS always
273 appends a trailing colon to distinguish a directory path from a file
274 path, a single trailing colon will be ignored, i.e. there's no empty
275 directory name after it.
276
277 Hence, on Mac OS, both
278
279 File::Spec->splitdir( ":a:b::c:" ); and
280 File::Spec->splitdir( ":a:b::c" );
281
282 yield:
283
284 ( "a", "b", "::", "c")
285
286 while
287
288 File::Spec->splitdir( ":a:b::c::" );
289
290 yields:
291
292 ( "a", "b", "::", "c", "::")
293
294 catpath
295 $path = File::Spec->catpath($volume,$directory,$file);
296
297 Takes volume, directory and file portions and returns an entire path.
298 On Mac OS, $volume, $directory and $file are concatenated. A ':' is
299 inserted if need be. You may pass an empty string for each portion.
300 If all portions are empty, the empty string is returned. If $volume
301 is empty, the result will be a relative path, beginning with a ':'.
302 If $volume and $directory are empty, a leading ":" (if any) is
303 removed form $file and the remainder is returned. If $file is empty,
304 the resulting path will have a trailing ':'.
305
306 abs2rel
307 Takes a destination path and an optional base path and returns a rel‐
308 ative path from the base path to the destination path:
309
310 $rel_path = File::Spec->abs2rel( $path ) ;
311 $rel_path = File::Spec->abs2rel( $path, $base ) ;
312
313 Note that both paths are assumed to have a notation that distin‐
314 guishes a directory path (with trailing ':') from a file path (with‐
315 out trailing ':').
316
317 If $base is not present or '', then the current working directory is
318 used. If $base is relative, then it is converted to absolute form
319 using "rel2abs()". This means that it is taken to be relative to the
320 current working directory.
321
322 If $path and $base appear to be on two different volumes, we will not
323 attempt to resolve the two paths, and we will instead simply return
324 $path. Note that previous versions of this module ignored the volume
325 of $base, which resulted in garbage results part of the time.
326
327 If $base doesn't have a trailing colon, the last element of $base is
328 assumed to be a filename. This filename is ignored. Otherwise all
329 path components are assumed to be directories.
330
331 If $path is relative, it is converted to absolute form using
332 "rel2abs()". This means that it is taken to be relative to the cur‐
333 rent working directory.
334
335 Based on code written by Shigio Yamaguchi.
336
337 rel2abs
338 Converts a relative path to an absolute path:
339
340 $abs_path = File::Spec->rel2abs( $path ) ;
341 $abs_path = File::Spec->rel2abs( $path, $base ) ;
342
343 Note that both paths are assumed to have a notation that distin‐
344 guishes a directory path (with trailing ':') from a file path (with‐
345 out trailing ':').
346
347 If $base is not present or '', then $base is set to the current work‐
348 ing directory. If $base is relative, then it is converted to absolute
349 form using "rel2abs()". This means that it is taken to be relative to
350 the current working directory.
351
352 If $base doesn't have a trailing colon, the last element of $base is
353 assumed to be a filename. This filename is ignored. Otherwise all
354 path components are assumed to be directories.
355
356 If $path is already absolute, it is returned and $base is ignored.
357
358 Based on code written by Shigio Yamaguchi.
359
361 See the authors list in File::Spec. Mac OS support by Paul Schinder
362 <schinder@pobox.com> and Thomas Wegner <wegner_thomas@yahoo.com>.
363
365 Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
366
367 This program is free software; you can redistribute it and/or modify it
368 under the same terms as Perl itself.
369
371 See File::Spec and File::Spec::Unix. This package overrides the imple‐
372 mentation of these methods, not the semantics.
373
374
375
376perl v5.8.8 2001-09-21 File::Spec::Mac(3pm)