NAME
FindBin::Files - locate files relative to the executable's parent directories
SYNOPSIS
# every ./etc/app.conf found walking up from $FindBin::Bin,
# nearest first, exported as @app_conf.
use FindBin::Files qw( file=etc/app.conf );
# nearest one only, exported as $conf ('' if none found).
use FindBin::Files qw( file=etc/app.conf scalar export=conf );
# a basename alone looks in each parent directory itself.
use FindBin::Files qw( file=.perltidyrc scalar export=tidyrc );
# accept a dangling symlink, e.g. to report or repair it.
use FindBin::Files qw( file=etc/app.conf nostrict );
DESCRIPTION
FindBin::Files is FindBin::libs for files: it walks up from the executable's directory and exports the paths of a file found below each parent directory. Nothing is added to @INC or the environment, the module only exports variables.
The parent directories come from dir_paths in FindBin::Parents, starting with the executable's directory and ending with the root. file is split with splitpath into its subdirectory and basename, with dir, if given, in front of the subdirectory; for each parent the subdirectory is appended with catdir, the path assembled with catpath, and the result checked with -f (see strict).
ARGUMENTS
Arguments are strings of the form name=value. A name without a value is true; a leading no sets it false (nostrict is strict='').
- dir=NAME
-
A single directory name prefixed to
file:dir=t file=50-foo/bar.tsearches each parent fort/50-foo/bar.t. It is checked withsplitdir, which returns one element for a single name. A compound or absolute path croaks, as does.orFile::Spec->updir(..on Unix):use FindBin::Files qw( dir=t/50-foobar file=bletch.t ); # croaks use FindBin::Files qw( dir=t file=50-foobar/bletch.t ); # works(
dirnamecannot do the check:dirname 't'is'.'.) The default''adds nothing, so the subdirectory comes fromfilealone. - file=PATH
-
Required. A bare
use FindBin::Files;croaks; userequireto load the module without searching.A relative path: a basename (
file=app.conf) or one with a leading subdirectory (file=etc/app.conf). An absolute path, or one whose directory containsFile::Spec->updir(..on Unix), croaks: either would find a file outside of the parent directory being searched.Any basename the filesystem allows is accepted (
file=-, UTF-8 names) except.andFile::Spec->updir, which are always directories. A missing basename (file=etc/) also croaks. - export=NAME
-
The variable exported into the caller's package; a qualified name (
export=Foo::conf) exports into that package. Withoutexport, the name comes from the basename offile; any directory is ignored. Leading and trailing non-word characters are stripped and each embedded run of them becomes one_. Configuration files often start with a dot and have an extension:use FindBin::Files qw( file=.myconfig.yaml ); # @myconfig_yaml use FindBin::Files qw( file=etc/app.conf ); # @app_confA name that is not an identifier croaks; pass
export=NAME. That includes a basename with no word characters (file=-gives'';debugwarns about it), one starting with a digit (file=1app.confgives1app_conf), and UTF-8 bytes that\Wdoes not see as word characters.use FindBin::Files qw( file=- export=dash ); # @dash - scalar
-
Export
$NAMEholding the nearest match instead of@NAMEholding all of them. If nothing is found$NAMEis''. - append
-
Push the matches onto an existing
@NAMEinstead of replacing it. Ignored withscalar. - strict (default true)
-
A match is a plain file,
-f, or a symlink to one.-fis undef for a missing path or a symlink whose target does not exist, so by default dangling links are skipped.nostrictchecks the undef case with-l, accepting dangling links:my $exists = $strict ? -f $path : -f $path // -l $path;A directory gives
-fof'', not undef, so it is never a match.A quick sanity check for dangling configuration links: with
nostrictevery.myconfigis returned, live or dead, and-epicks out the dead ones.use FindBin::Files qw( file=.myconfig export=config_f nostrict ); my @dead = grep { ! -e } @config_f; @dead and die "Dangling config links: @dead\n"; - abspath (default true)
-
Search from
$Bin_p, the physical path with symlinks resolved.noabspathuses$Bin_l, the logical path, which follows the directories as named in$0. See FindBin::Bin. - ignore=DIR,DIR... (default
/,/usr) -
Parent directories that are not searched: the default skips
/FILEand/usr/FILE. Entries are absolute directory paths compared as given.ignore=(empty) searches every parent. - fail (default true)
-
Carp if no file is found.
nofailis silent; the export still happens, empty. - verbose, print
-
verbosereports the search paths and the export on STDERR;printreports the files found. Both default from$ENV{ VERBOSE_FINDBIN_FILES }, then$ENV{ VERBOSE }. - debug
-
True if
debugis passed or$ENV{ FBF_DEBUG }is true. WarnsBasename '...' lacks any word charswhen the default export name cannot come from the basename, and sets$DB::singlesoperl -dstops while handling the arguments.
OUTCOMES
- One or more matches
-
@NAMEholds every match nearest first;$NAMEthe nearest. - No matches
-
@NAMEis empty,$NAMEis'', andfailcarps. - Dangling symlink
-
Skipped by default; returned with
nostrict.