A little late to this answer, but there's another way from the Perl environment hash %ENV
. And since this was the only useful page that came up when I searched, I'll add my way here for people from the future.
$ENV{'SCRIPT_FILENAME'}
provides the full path to the script you're running /var/www/cgi-bin/perlscript.pl
$ENV{'SCRIPT_NAME'}
provides the chroot path /cgi-bin/perlscript.pl
If you want to see all of the environment variables your system provides use the following code:
foreach $key (sort(keys(%ENV)))
{
printf("%16s : %s\n", $key, $ENV{$key});
}
I use that loop quite often when I'm examining a new environment.
The reason I'm reading this question is because my work uses ActiveState Perl, and they don't provide the script filename in the environment hash so I was trying to find other ways to get it. Of all the answers the only one that worked in our environment was the __FILE__
one listed above.