How to get the name of Perl script that is running

Perl

Perl Problem Overview


How can I get the name of the script?

For example, I have a Perl script with the name XXX.pl. This file contains:

$name = #some function that obtains the script's own name
print $name;

Output:

XXX.pl

I would like to liken this to the CWD function that obtains the scripts directory. I need a function that obtains the script's name as well.

Perl Solutions


Solution 1 - Perl

The name of the running program can be found in the $0 variable:

print $0;

man perlvar for other special variables.

Solution 2 - Perl

use File::Basename;
my $name = basename($0);

PS. getcwd() and friends don't give you the script's directory! They give you the working directory. If the script is in your PATH and you just call it by name, not by its full path, then getcwd() won't do what you say. You want dirname($0) (dirname also is in File::Basename).

Solution 3 - Perl

You might also be interested in learning more about __FILE__, and possibly __LINE__, which gives you the current line and I frequently use together.

If you want this for debugging purposes, you might also want to learn about "warn", "Carp", "caller".

Solution 4 - Perl

Check out module FindBin, part of the core Perl distribution. It exports variables you're looking for

  EXPORTABLE VARIABLES
        $Bin         - path to bin directory from where script was invoked
        $Script      - basename of script from which Perl was invoked
        $RealBin     - $Bin with all links resolved
        $RealScript  - $Script with all links resolved

Solution 5 - Perl

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmasterialView Question on Stackoverflow
Solution 1 - PerlZac SprackettView Answer on Stackoverflow
Solution 2 - PerlMatt KView Answer on Stackoverflow
Solution 3 - PerlmsbView Answer on Stackoverflow
Solution 4 - PerlWinston SmithView Answer on Stackoverflow
Solution 5 - PerlLauraView Answer on Stackoverflow