How to get the name of Perl script that is running
Solution 1:
The name of the running program can be found in the $0
variable:
print $0;
man perlvar for other special variables.
Solution 2:
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:
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
".