FindBin::Parents(3pm)
β¨ NAME β¨
FindBin::Parents β bestie, it lists the parent dirs of whatever path you hand it, all the way from the current dir up to root. That's it. That's the whole job. π
πΈ SYNOPSIS πΈ
okay so first you gotta pull in the one function you need:
use FindBin::Parents qw( dir_paths );
now the fun part β watch it climb the tree like it's on main character energy:
# on *NIX (incl. OSX)
# '/foo/bar/bim/bam' yields
# /foo/bar/bim/bam
# /foo/bar/bim
# /foo/bar
# /foo
#
# on VMS
# 'Bletch$Blort:[foo.bar.bim.bam]' yields
# Bletch$Blort:[foo.bar.bim.bam]
# Bletch$Blort:[foo.bar.bim]
# Bletch$Blort:[foo.bar]
# Bletch$Blort:[foo]
#
# on MSW
# 'z:/foo/bar/bim/bam' yields
# z:/foo/bar/bim/bam
# z:/foo/bar/bim
# z:/foo/bar
# z:/foo
#
# $path is first passed through rel2abs and canonpath
# which should yield clean, absolute paths.
#
# note that the return vlaue is context-sensitive:
that last bit is the important tea β β the return value is context-sensitive, so look what you get back depends on how you ask for it:
my $array_ref = dir_paths $path;
my @array = dir_paths $path;
scalar context? you get an array ref. list context? you get the actual array. pick your fighter π
and here's where it gets delulu but make it correct β if you point it at something that is not a directory, like /foo/bar/bletch/blort, the final blort gets dropped and you just get the paths that lead to it:
# for any non-directory /foo/bar/bletch/blort, the final
# 'blort' is dropped and the paths leading to it are returned:
dir_paths $0;
# /foo/bar/bletch # parent dir of blort
# /foo/bar
# /foo
now for the plot twist: paths that don't even exist still get processed, no cap:
# Note: non-existant paths are processed, but may require
# an extra assume-dir argument to treat the argument as a
# directory (or not, no way to tell unless it exists, eh?).
#
# the default is true, these are equivalent:
my @found
= dir_paths '/foo/bar/bletch/blort/non-existent';
my @found
= dir_paths '/foo/bar/bletch/blort/non-existent', 1;
# /foo/bar/bletch/blort/non-existant
# /foo/bar/bletch/blort
# /foo/bar/bletch
# /foo/bar/bletch
# /foo/bar
# /foo
π«Ά little heads up bestie: when the path doesn't exist, you might need that extra assume-dir argument to tell it "yeah treat this like a directory" (or don't, because honestly there's no way to tell unless it exists, eh?). The default is true, which is why those two calls up there are literally the same thing.
and if you pass a false value, the last entry gets dropped entirely:
# false value drops the last entry:
my @found
= dir_paths '/foo/bar/bletch/blort/non-existant', 0;
# /foo/bar/bletch/blort
# /foo/bar/bletch
# /foo/bar/bletch
# /foo/bar
# /foo
and that's the whole page, she's short but she ate π
perl v5.40.1 Β· 2025-11-09 Β· FindBin::Parents(3pm) β mwah, go climb some directories, ily π¦β¨π
π¦ where this page comes from
- ubuntu resolute: libfindbin-libs-perl 4.0.4-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libfindbin-libs-perl 4.0.4-1 Β· Artistic-1.0-Perl OR 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