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.t searches each parent for t/50-foo/bar.t. It is checked with splitdir, which returns one element for a single name. A compound or absolute path croaks, as does . or File::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

(dirname cannot do the check: dirname 't' is '.'.) The default '' adds nothing, so the subdirectory comes from file alone.

file=PATH

Required. A bare use FindBin::Files; croaks; use require to 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 contains File::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 . and File::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. Without export, the name comes from the basename of file; 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_conf

A name that is not an identifier croaks; pass export=NAME. That includes a basename with no word characters (file=- gives ''; debug warns about it), one starting with a digit (file=1app.conf gives 1app_conf), and UTF-8 bytes that \W does not see as word characters.

use FindBin::Files qw( file=- export=dash );       # @dash
scalar

Export $NAME holding the nearest match instead of @NAME holding all of them. If nothing is found $NAME is ''.

append

Push the matches onto an existing @NAME instead of replacing it. Ignored with scalar.

strict (default true)

A match is a plain file, -f, or a symlink to one. -f is undef for a missing path or a symlink whose target does not exist, so by default dangling links are skipped. nostrict checks the undef case with -l, accepting dangling links:

my $exists = $strict ? -f $path : -f $path // -l $path;

A directory gives -f of '', not undef, so it is never a match.

A quick sanity check for dangling configuration links: with nostrict every .myconfig is returned, live or dead, and -e picks 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. noabspath uses $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 /FILE and /usr/FILE. Entries are absolute directory paths compared as given. ignore= (empty) searches every parent.

fail (default true)

Carp if no file is found. nofail is silent; the export still happens, empty.

verbose, print

verbose reports the search paths and the export on STDERR; print reports the files found. Both default from $ENV{ VERBOSE_FINDBIN_FILES }, then $ENV{ VERBOSE }.

debug

True if debug is passed or $ENV{ FBF_DEBUG } is true. Warns Basename '...' lacks any word chars when the default export name cannot come from the basename, and sets $DB::single so perl -d stops while handling the arguments.

OUTCOMES

One or more matches

@NAME holds every match nearest first; $NAME the nearest.

No matches

@NAME is empty, $NAME is '', and fail carps.

Skipped by default; returned with nostrict.

SEE ALSO

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