There are a few ways:
$0is the currently executing script as provided by POSIX, relative to the current working directory if the script is at or below the CWD- Additionally,
cwd(),getcwd()andabs_path()are provided by theCwdmodule and tell you where the script is being run from - The module
FindBinprovides the$Bin&$RealBinvariables that usually are the path to the executing script; this module also provides$Script&$RealScriptthat are the name of the script __FILE__is the actual file that the Perl interpreter deals with during compilation, including its full path.
I’ve seen the first three ($0, the Cwd module and the FindBin module) fail under mod_perl spectacularly, producing worthless output such as '.' or an empty string. In such environments, I use __FILE__ and get the path from that using the File::Basename module:
use File::Basename;
my $dirname = dirname(__FILE__);