NAME
find_myconfig - find the nearest etc/myconfig dir walking up from the script
SYNOPSIS
find_myconfig
# To walk from the logical bin ($Bin_l) instead of the physical
# ($Bin_p), add "noabspath" to the FindBin::libs import:
#
# use FindBin::libs qw( base=etc subdir=myconfig subonly export=myconfig noabspath );
DESCRIPTION
Searches upward from the script's bin directory for etc/myconfig subdirectories. Exports them as @myconfig (nearest first) and picks the nearest existing one as the active configuration directory.
Uses all three modules:
- FindBin::Bin
-
Supplies
$Bin_land$Bin_pso the script can report and compare the logical and physical starting points of the walk. - FindBin::libs
-
Performs the upward search.
base=etc subdir=myconfig subonlyrestricts the results to paths ending inetc/myconfig; bareetcdirectories are excluded from@myconfig. - FindBin::Parents
-
dir_paths($Bin_p)returns the full ancestor chain so the script can show which directories were candidates.
subonly: etc/myconfig only vs. etc and etc/myconfig
Without subonly:
use FindBin::libs qw( base=etc subdir=myconfig export=myconfig );
# @myconfig contains both etc dirs and etc/myconfig subdirs (where found):
# /work/projects/app/etc/myconfig
# /work/projects/app/etc
# /work/projects/etc/myconfig
# /work/projects/etc
With subonly:
use FindBin::libs qw( base=etc subdir=myconfig subonly export=myconfig );
# @myconfig contains only etc/myconfig subdirs:
# /work/projects/app/etc/myconfig
# /work/projects/etc/myconfig
Use subonly when you want the named subdirectory exclusively -- for example, when etc holds mixed content and only etc/myconfig is relevant to this script.
abspath vs. noabspath
FindBin::libs starts its walk from $Bin_p (default, physical) or $Bin_l (with noabspath, logical). For config directories, the usual goal is to find the config closest to where the source code lives on disk, so the abspath default is normally correct.
Use noabspath when the script is invoked through a directory symlink pointing into a test sandbox that has its own etc/myconfig tree:
/sandbox/
+-- etc/
| \-- myconfig/ # test configuration here
\-- app -> /work/projects/app
noabspath walk from $Bin_l: /sandbox/app/bin -> /sandbox/app -> /sandbox
finds: /sandbox/etc/myconfig (test configuration)
abspath walk from $Bin_p: /work/projects/app/bin -> ...
finds: /work/projects/etc/myconfig (production configuration)
scalar: pick only the nearest
To export only the nearest etc/myconfig as a scalar rather than a list:
use FindBin::libs qw( base=etc subdir=myconfig subonly scalar export=myconfig );
# $myconfig holds the path to the nearest etc/myconfig dir (or undef)