I have Perl script and need to determine the full path and filename of the script during execution. I discovered that depending on how you call the script $0
va
Getting the absolute path to $0
or __FILE__
is what you want. The only trouble is if someone did a chdir()
and the $0
was relative -- then you need to get the absolute path in a BEGIN{}
to prevent any surprises.
FindBin
tries to go one better and grovel around in the $PATH
for something matching the basename($0)
, but there are times when that does far-too-surprising things (specifically: when the file is "right in front of you" in the cwd.)
File::Fu
has File::Fu->program_name
and File::Fu->program_dir
for this.