NAME

list_etc_dirs - list all etc dirs found walking up from the script

SYNOPSIS

list_etc_dirs

# 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 export noabspath );

DESCRIPTION

Exports @etc containing every etc directory found walking up the directory tree from the script's bin directory, nearest first. Also calls FindBin::Parents::dir_paths to display the full list of parent directories that were searched.

The three modules work together here:

FindBin::Bin

Provides $Bin_l (logical path, symlinks preserved) and $Bin_p (physical path, symlinks resolved). These are the two possible starting points for the upward walk.

FindBin::libs

Walks up from $Bin_p (default) or $Bin_l (with noabspath), collecting directories named etc and exporting them as @etc.

FindBin::Parents

dir_paths($path) returns every ancestor directory of $path, from $path itself up to the root. Passing the same root that FindBin::libs used shows exactly which directories were candidates for an etc entry.

abspath vs. noabspath

When no directory symlinks are present, $Bin_l eq $Bin_p and the choice makes no difference. They diverge when the script is invoked via a directory symlink.

Consider:

/home/user/                         # logical subtree
+-- etc/
|   \-- sandbox.conf                # test configuration
\-- app -> /work/projects/app       # directory symlink

/work/projects/                     # physical subtree
+-- etc/
|   \-- production.conf             # production configuration
\-- app/
    \-- bin/
        \-- myscript                # $0

Invoked as /home/user/app/bin/myscript:

$Bin_l = /home/user/app/bin    (symlink preserved)
$Bin_p = /work/projects/app/bin  (symlink resolved)

With abspath (default):

walk up from $Bin_p: /work/projects/app/bin -> /work/projects/app
                     -> /work/projects -> /work -> /
@etc = ( '/work/projects/etc' )   # production.conf

With noabspath:

walk up from $Bin_l: /home/user/app/bin -> /home/user/app
                     -> /home/user -> /home -> /
@etc = ( '/home/user/etc' )       # sandbox.conf

export and the variable name

export without an argument names the exported array after the base value. base=etc therefore exports @etc. To use a different name:

use FindBin::libs qw( base=etc export=config_dirs );
# exports @config_dirs

SEE ALSO

FindBin::Bin, FindBin::libs, FindBin::Parents