53

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
masterial
  • 2,166
  • 9
  • 33
  • 48

5 Answers5

87

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

print $0;

man perlvar for other special variables.

DVK
  • 126,886
  • 32
  • 213
  • 327
Zac Sprackett
  • 906
  • 6
  • 3
  • 1
    Sometimes the running program will not be the pl file, but a batch job that loaded and ran the pl file. If your filenames are similar to your package names, print "(" . caller . ")"; is most useful. – HoldOffHunger Jun 27 '17 at 18:54
50
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).

Matt K
  • 13,370
  • 2
  • 32
  • 51
  • 1
    This is incorrect; `dirname($0)` also gives the relative path. If you run a script with `perl script` or `./script`, it will return '.'. – felwithe Nov 02 '16 at 20:36
12

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

msb
  • 3,899
  • 3
  • 31
  • 38
5

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Winston Smith
  • 367
  • 1
  • 5
  • 11
1

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.

Laura
  • 154
  • 2
  • 5