File::Spec::Mac(3perl)

✨ NAME ✨

File::Spec::Mac β€” it's the File::Spec situation for Mac OS (Classic), bestie πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

require File::Spec::Mac; # Done internally by File::Spec if needed

🌸 DESCRIPTION 🌸

Okay so, methods for manipulating file specifications. That's the whole vibe. πŸŽ€


πŸ‘‘ METHODS πŸ‘‘

πŸ’… canonpath

On Mac OS, there's nothing to be done. It just returns what you gave it. Slay, zero effort, no notes. ✨

πŸ“ catdir()

Concatenate two or more directory names to form a path separated by colons (":") ending with a directory. Resulting paths are relative by default, but can be forced to be absolute (but avoid this, see below!!). It automatically puts a trailing ":" on the end of the complete path, because that's what's done in MacPerl's environment and it helps distinguish a file path from a directory path. 🫢

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 ":"). iykyk πŸ’€

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:

  1. The resulting path is relative by default, i.e. the resulting path will have a leading colon.
  2. A trailing colon is added automatically to the resulting path, to denote a directory.
  3. Generally, each argument has one leading ":" and one trailing ":" removed (if any). They are then joined together by a ":". Special treatment applies for arguments denoting updir paths like "::lib:", see (4), or arguments consisting solely of colons ("colon paths"), see (5).
  4. When an updir path like ":::lib::" is passed as argument, the number of directories to climb up is handled correctly, not removing leading or trailing colons when necessary. E.g.
catdir(":::a","::b","c")    = ":::a::b:c:"
catdir(":::a::","::b","c")  = ":::a:::b:c:"
  1. Adding a colon ":" or empty string "" to a path at any position doesn't alter the path, i.e. these arguments are ignored. (When a "" is passed as the first argument, it has a special meaning, see (6)). This way, a colon ":" is handled like a "." (curdir) on Unix, while an empty string "" is generally ignored (see "canonpath()" in File::Spec::Unix ). Likewise, a "::" is handled like a ".." (updir), and a ":::" is handled like a "../.." etc. E.g.
catdir("a",":",":","b")   = ":a:b:"
catdir("a",":","::",":b") = ":a::b:"
  1. If the first argument is an empty string "" or is a volume name, i.e. matches the pattern /^[^:]+:/, the resulting path is absolute. πŸ‘‘
  2. Passing an empty string "" as the first argument to catdir() is like passing "File::Spec->rootdir()" as the first argument, i.e.
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. πŸ’–

  1. For absolute paths, some cleanup is done, to ensure that the volume name isn't immediately followed by updirs. This is invalid, because this would go beyond "root". Generally, these cases are handled like their Unix counterparts:
 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:" .

🌈 catfile

Concatenate one or more directory names and a filename to form a complete path ending with a filename. Resulting paths are relative by default, but can be forced to be absolute (but avoid this). 🫢

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).

🧚 curdir

Returns a string representing the current directory. On Mac OS, this is ":". πŸŽ€

πŸ’œ devnull

Returns a string representing the null device. On Mac OS, this is "Dev:Null".

⭐ rootdir

Returns the empty string. Mac OS has no real root directory. it's giving void, honestly 🌌

🌸 tmpdir

Returns the contents of $ENV{TMPDIR}, if that directory exits or the current working directory otherwise. Under MacPerl, $ENV{TMPDIR} will contain a path like "MacintoshHD:Temporary Items:", which is a hidden directory on your startup volume. 🫢

πŸ¦‹ updir

Returns a string representing the parent directory. On Mac OS, this is "::".

πŸ‘‘ file_name_is_absolute

Takes as argument a path and returns true, if it is an absolute path. If the path has a leading ":", it's a relative path. Otherwise, it's an absolute path, unless the path doesn't contain any colons, i.e. it's a name like "a". In this particular case, the path is considered to be relative (i.e. it is considered to be a filename). Use ":" in the appropriate place in the path if you want to distinguish unambiguously. As a special case, the filename '' is always considered to be absolute. Note that with version 1.2 of File::Spec::Mac, this does no longer consult the local filesystem. πŸ’€

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)

πŸ“ path

Returns the null list for the MacPerl application, since the concept is usually meaningless under Mac OS. But if you're using the MacPerl tool under MPW, it gives back $ENV{Commands} suitably split, as is done in :lib:ExtUtils:MM_Mac.pm. πŸŽ€

πŸ’– splitpath

($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. πŸ”₯

🌈 splitdir

The opposite of catdir().

@dirs = File::Spec->splitdir( $directories );

okay so, real talk for a sec bestie: $directories should be only the directory chunk of the path β€” and that's specifically on systems that have the whole volume concept, or whose path syntax actually differentiates files from directories. Otherwise?? Just use splitpath() instead, fr fr. πŸ™ˆ

Unlike just yeeting a split on the separator, empty directory names ("") can come back at you. And here's the twist: since catdir() on Mac OS always tacks on a trailing colon to tell a directory path apart from a file path, a single trailing colon gets ignored β€” like, there's no empty directory name sitting after it. No cap. ✨

So on Mac OS, both of these:

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", "::")

See?? the audacity of that last one πŸ’€ literally just one extra colon and the whole vibe shifts.


🌸 catpath 🌸

$path = File::Spec->catpath($volume,$directory,$file);

This one takes a volume, a directory and a file portion, and hands you back a whole entire path. On Mac OS, $volume, $directory and $file just get concatenated together β€” a : gets wedged in if it's needed. πŸ’…

You can pass an empty string for each portion, no drama. Here's the breakdown:

  • If all the portions are empty β†’ you get the empty string back. πŸ«₯
  • If $volume is empty β†’ the result is a relative path, starting with a :.
  • If $volume and $directory are empty β†’ a leading : (if there is one) gets removed from $file, and whatever's left is returned.
  • If $file is empty β†’ the resulting path will have a trailing :. πŸŽ€

πŸ¦‹ abs2rel πŸ¦‹

Takes a destination path, plus an optional base path, and gives you a relative path going from the base on over to the destination:

$rel_path = File::Spec->abs2rel( $path ) ;
$rel_path = File::Spec->abs2rel( $path, $base ) ;

Quick note, bestie: both paths are assumed to use a notation that distinguishes a directory path (trailing :) from a file path (no trailing :). iykyk. πŸ‘‘

If $base isn't there, or it's '', then the current working directory gets used instead. And if $base is relative, it gets converted into absolute form using rel2abs(). Which means it's taken to be relative to the current working directory. πŸ’œ

If $path and $base look like they're chillin' on two different volumes, we are not even going to attempt to resolve them β€” we just return $path as-is. And lowkey important: previous versions of this module ignored the volume of $base, which gave you straight-up garbage results part of the time. 😭 so glad that's fixed.

If $base doesn't have a trailing colon, the last element of $base is assumed to be a filename, and that filename is ignored. Otherwise, all the path components are assumed to be directories.

If $path is relative, it gets converted to absolute form using rel2abs() β€” meaning it's taken to be relative to the current working directory.

Based on code written by Shigio Yamaguchi. ✨


πŸ’– rel2abs πŸ’–

Converts a relative path into an absolute path:

$abs_path = File::Spec->rel2abs( $path ) ;
$abs_path = File::Spec->rel2abs( $path, $base ) ;

Same deal as before, bestie: both paths are assumed to have a notation that distinguishes a directory path (trailing :) from a file path (no trailing :). πŸŽ€

If $base isn't present or it's '', then $base gets set to the current working directory. If $base is relative, it gets converted to absolute form using rel2abs(). This means it's 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, and that filename is ignored. Otherwise, all path components are assumed to be directories.

And if $path is already absolute? It just gets returned and $base is completely ignored. main character behaviour honestly. πŸ‘‘

Based on code written by Shigio Yamaguchi.


πŸ‘‘ AUTHORS πŸ‘‘

Check the authors list over in File::Spec. The Mac OS support was brought to you by Paul Schinder <schinder@pobox.com> and Thomas Wegner <wegner_thomas@yahoo.com>. absolute icons, we thank them for their service 🫢


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 ALSO 🌈

Go peek at File::Spec and File::Spec::Unix. Just so we're clear though: this package overrides the implementation of these methods β€” not the semantics. πŸ’… periodt.


aaand that's a wrap on the Mac OS chapter of the File::Spec saga, bestie πŸ’– This was perl v5.44.0, dated 2026-10-17 β€” go forth and concatenate responsibly, you absolute legend. πŸ¦‹βœ¨πŸŒΈ

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms