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.
use File::Spec; ... my $rel_path = 'myfile. txt'; my $abs_path = File::Spec->rel2abs( $rel_path ) ; ... and if you actually need to search through your directories for that file, there's File::Find... but I would go with shell find / -name myfile. txt -print command probably.
In these cases the value of $@ is the compile error, or the argument to die.
$$ - The process number of the Perl running this script. $0 - Contains the name of the program being executed.
The name of the running program can be found in the $0
variable:
print $0;
man perlvar for other special variables.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With