NAME

FindBin::Bin - Find the execution directory, exported via BIN()

SYNOSIS

# the most common case is splitting off a volume and 
# directory list from $0. This avoids resolving any 
# symlinks in the path so that symlinked executables 
# can access other files relative to their execution
# path not where they are stored.
#
# to get the absolute path to the executable use 
# Cwd::abs_path.
#
# this gets messier if $0 is any one of:
# '-e' or '-' or 'interactive'. the common fix of 
# "cwd" or "getcwd" or "curdir" from Cwd or File::Spec
# return the resolved path to the working directory 
# and we want the symlinks left in place.
#
# the fix there is system specific:
#
# VMS: $ENV{ DEFAULT }
# MSW: curdir (which doesn't resolve symlinks)
# *NIX or Cygwin use a version of the logic in "pwd" 
# which trusts $ENV{ PWD } IFF it references the same
# device and inode as '.'.
#
# the fall back without DEFAULT or PWD or if PWD 
# doesn't match '.' is living with 'getcwd' and the
# resolved symlinks.
#
# $Bin is defined via ref-to-constant, Bin is a 
# constant sub. they will remain constant through 
# execution unless someone twiddles them with refs.

#!/usr/bin/env perl

use FindBin::Bin qw( $Bin );

say $Bin;
# e.g., /tmp/FindBin-libs/t/10-working-dir

# sub call

use FindBin::Bin qw(  Bin );

say Bin;
# e.g., /tmp/FindBin-libs/t/10-working-dir

my $path = Bin;

say $path;
# e.g., /tmp/FindBin-libs/t/10-working-dir

# command line execution shows the difference between
# using Bin and getcwd: cd into a symlink, Bin and getcwd
# return different results: Bin.pm returns the same result
# from "perl -E" as it does with the #! invocation and an
# executable for $0.

perl -MFindBin::bin=Bin 'say Bin';
# e.g., /tmp/FindBin-libs/10-working-dir

$ cd /tmp;
$ ln -fsv FindBin-libs foobar;
'foobar' -> '/scratch/lembark/sandbox/Modules/Perl/FindBin-libs'

$ cd foobar;
$ perl -MCwd=getcwd -MFindBin::Bin=Bin -E 'say Bin; say getcwd';
/tmp/foobar
/scratch/lembark/sandbox/Modules/Perl/FindBin-libs