File::Spec::Mac(3perl) | Perl Programmers Reference Guide | File::Spec::Mac(3perl) |
File::Spec::Mac - File::Spec for Mac OS (Classic)
require File::Spec::Mac; # Done internally by File::Spec if needed
Methods for manipulating file specifications.
IMPORTANT NOTE: Beginning with version 1.3 of this module, the resulting path is relative by default and not absolute. This decision was made due to portability reasons. Since "File::Spec->catdir()" returns relative paths on all other operating systems, it will now also follow this convention on Mac OS. Note that this may break some existing scripts.
The intended purpose of this routine is to concatenate directory names. But because of the nature of Macintosh paths, some additional possibilities are allowed to make using this routine give reasonable results for some common situations. In other words, you are also allowed to concatenate paths instead of directory names (strictly speaking, a string like ":a" is a path, but not a name, since it contains a punctuation character ":").
So, beside calls like
catdir("a") = ":a:" catdir("a","b") = ":a:b:" catdir() = "" (special case)
calls like the following
catdir(":a:") = ":a:" catdir(":a","b") = ":a:b:" catdir(":a:","b") = ":a:b:" catdir(":a:",":b:") = ":a:b:" catdir(":") = ":"
are allowed.
Here are the rules that are used in "catdir()"; note that we try to be as compatible as possible to Unix:
catdir(":::a","::b","c") = ":::a::b:c:" catdir(":::a::","::b","c") = ":::a:::b:c:"
catdir("a",":",":","b") = ":a:b:" catdir("a",":","::",":b") = ":a::b:"
catdir("","a","b") is the same as catdir(rootdir(),"a","b").
This is true on Unix, where "catdir("","a","b")" yields "/a/b" and "rootdir()" is "/". Note that "rootdir()" on Mac OS is the startup volume, which is the closest in concept to Unix' "/". This should help to run existing scripts originally written for Unix.
Unix: Unix->catdir("","") = "/" Unix->catdir("",".") = "/" Unix->catdir("","..") = "/" # can't go # beyond root Unix->catdir("",".","..","..","a") = "/a" Mac: Mac->catdir("","") = rootdir() # (e.g. "HD:") Mac->catdir("",":") = rootdir() Mac->catdir("","::") = rootdir() # can't go # beyond root Mac->catdir("",":","::","::","a") = rootdir() . "a:" # (e.g. "HD:a:")
However, this approach is limited to the first arguments following "root" (again, see "canonpath()" in File::Spec::Unix. If there are more arguments that move up the directory tree, an invalid path going beyond root can be created.
As you've seen, you can force "catdir()" to create an absolute path by passing either an empty string or a path that begins with a volume name as the first argument. However, you are strongly encouraged not to do so, since this is done only for backward compatibility. Newer versions of File::Spec come with a method called "catpath()" (see below), that is designed to offer a portable solution for the creation of absolute paths. It takes volume, directory and file portions and returns an entire path. While "catdir()" is still suitable for the concatenation of directory names, you are encouraged to use "catpath()" to concatenate volume names and directory paths. E.g.
$dir = File::Spec->catdir("tmp","sources"); $abs_path = File::Spec->catpath("MacintoshHD:", $dir,"");
yields
"MacintoshHD:tmp:sources:" .
IMPORTANT NOTE: Beginning with version 1.3 of this module, the resulting path is relative by default and not absolute. This decision was made due to portability reasons. Since "File::Spec->catfile()" returns relative paths on all other operating systems, it will now also follow this convention on Mac OS. Note that this may break some existing scripts.
The last argument is always considered to be the file portion. Since "catfile()" uses "catdir()" (see above) for the concatenation of the directory portions (if any), the following with regard to relative and absolute paths is true:
catfile("") = "" catfile("file") = "file"
but
catfile("","") = rootdir() # (e.g. "HD:") catfile("","file") = rootdir() . file # (e.g. "HD:file") catfile("HD:","file") = "HD:file"
This means that "catdir()" is called only when there are two or more arguments, as one might expect.
Note that the leading ":" is removed from the filename, so that
catfile("a","b","file") = ":a:b:file" and catfile("a","b",":file") = ":a:b:file"
give the same answer.
To concatenate volume names, directory paths and filenames, you are encouraged to use "catpath()" (see below).
E.g.
File::Spec->file_name_is_absolute("a"); # false (relative) File::Spec->file_name_is_absolute(":a:b:"); # false (relative) File::Spec->file_name_is_absolute("MacintoshHD:"); # true (absolute) File::Spec->file_name_is_absolute(""); # true (absolute)
($volume,$directories,$file) = File::Spec->splitpath( $path ); ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
Splits a path into volume, directory, and filename portions.
On Mac OS, assumes that the last part of the path is a filename unless $no_file is true or a trailing separator ":" is present.
The volume portion is always returned with a trailing ":". The directory portion is always returned with a leading (to denote a relative path) and a trailing ":" (to denote a directory). The file portion is always returned without a leading ":". Empty portions are returned as empty string ''.
The results can be passed to "catpath()" to get back a path equivalent to (usually identical to) the original path.
@dirs = File::Spec->splitdir( $directories );
$directories should be only the directory portion of the path on systems that have the concept of a volume or that have path syntax that differentiates files from directories. Consider using "splitpath()" otherwise.
Unlike just splitting the directories on the separator, empty directory names ("") can be returned. Since "catdir()" on Mac OS always appends a trailing colon to distinguish a directory path from a file path, a single trailing colon will be ignored, i.e. there's no empty directory name after it.
Hence, on Mac OS, both
File::Spec->splitdir( ":a:b::c:" ); and File::Spec->splitdir( ":a:b::c" );
yield:
( "a", "b", "::", "c")
while
File::Spec->splitdir( ":a:b::c::" );
yields:
( "a", "b", "::", "c", "::")
$path = File::Spec->catpath($volume,$directory,$file);
Takes volume, directory and file portions and returns an entire path. On Mac OS, $volume, $directory and $file are concatenated. A ':' is inserted if need be. You may pass an empty string for each portion. If all portions are empty, the empty string is returned. If $volume is empty, the result will be a relative path, beginning with a ':'. If $volume and $directory are empty, a leading ":" (if any) is removed form $file and the remainder is returned. If $file is empty, the resulting path will have a trailing ':'.
$rel_path = File::Spec->abs2rel( $path ) ; $rel_path = File::Spec->abs2rel( $path, $base ) ;
Note that both paths are assumed to have a notation that distinguishes a directory path (with trailing ':') from a file path (without trailing ':').
If $base is not present or '', then the current working directory is used. If $base is relative, then it is converted to absolute form using "rel2abs()". This means that it is taken to be relative to the current working directory.
If $path and $base appear to be on two different volumes, we will not attempt to resolve the two paths, and we will instead simply return $path. Note that previous versions of this module ignored the volume of $base, which resulted in garbage results part of the time.
If $base doesn't have a trailing colon, the last element of $base is assumed to be a filename. This filename is ignored. Otherwise all path components are assumed to be directories.
If $path is relative, it is converted to absolute form using "rel2abs()". This means that it is taken to be relative to the current working directory.
Based on code written by Shigio Yamaguchi.
$abs_path = File::Spec->rel2abs( $path ) ; $abs_path = File::Spec->rel2abs( $path, $base ) ;
Note that both paths are assumed to have a notation that distinguishes a directory path (with trailing ':') from a file path (without trailing ':').
If $base is not present or '', then $base is set to the current working directory. If $base is relative, then it is converted to absolute form using "rel2abs()". This means that it is taken to be relative to the current working directory.
If $base doesn't have a trailing colon, the last element of $base is assumed to be a filename. This filename is ignored. Otherwise all path components are assumed to be directories.
If $path is already absolute, it is returned and $base is ignored.
Based on code written by Shigio Yamaguchi.
See the authors list in File::Spec. Mac OS support by Paul Schinder <schinder@pobox.com> and Thomas Wegner <wegner_thomas@yahoo.com>.
Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See File::Spec and File::Spec::Unix. This package overrides the implementation of these methods, not the semantics.
2023-11-25 | perl v5.36.0 |