Perl (Scripting) - scriptname $0

by
Jeremy Canfield |
Updated: May 23 2023
| Perl (Scripting) articles
The $0 variable can be used to get the name of the Perl script being invoked. For example, let's say you have a Perl script named foo.pl. Here is how you would show that foo.pl is the name of the script.
#!/usr/bin/perl
use strict;
use warnings;
print "Script name = $0 \n";
Or, more commonly, scriptname is stored in a constant variable.
#!/usr/bin/perl
use strict;
use warnings;
my $scriptname = $0;
print "Script name = $scriptname \n";
Which should return the following.
Script name = foo.pl
Did you find this article helpful?
If so, consider buying me a coffee over at