Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of Perl script that is running

Tags:

perl

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.

like image 960
masterial Avatar asked Jan 05 '11 01:01

masterial


People also ask

How do I find the path of a file in Perl?

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.

What does $@ mean in Perl?

In these cases the value of $@ is the compile error, or the argument to die.

What is $$ in Perl?

$$ - The process number of the Perl running this script. $0 - Contains the name of the program being executed.


2 Answers

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

print $0; 

man perlvar for other special variables.

like image 96
Zac Sprackett Avatar answered Sep 28 '22 16:09

Zac Sprackett


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).

like image 26
Matt K Avatar answered Sep 28 '22 15:09

Matt K