perl

How to get the name of Perl script that is running


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.


Solution

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

    print $0;
    

    man perlvar for other special variables.