Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of the running script without the extension

Tags:

perl

I am interested in getting the name of the running script without the .pl
This: my ($script) = $0 =~ /(.*)\.pl/; seems to work. I was wondering if there is another more standard way for this

like image 978
Cratylus Avatar asked Oct 29 '25 07:10

Cratylus


2 Answers

Use fileparse from File::Basename. This is a core module so it's always available to your distribution.

use File::Basename;
my ( $name, $path, $suffix ) = fileparse( $0, qr{\.[^.]*$} );
print "NAME=$name\n";
print "PATH=$path\n";
print "SFFX=$suffix\n";

The regular expression to find the suffix looks for a dot, followed by anything that isn't a dot, zero or more times, anchored to the end of the string.

If the running script was/mypath/myfile.pl this returns:

NAME=myfile
PATH=/mypath/
SFFX=.pl
like image 158
JRFerguson Avatar answered Oct 31 '25 11:10

JRFerguson


the scriptname is in

print $0;

like you wrote..

if you want to have only the name without .pl

-i- would do this:

my $script = $0;
$script =~ s/\.pl//;

i dont think that here is any performance issue to choose this or that one ;)

see perlvar

like image 28
Alex Tape Avatar answered Oct 31 '25 13:10

Alex Tape



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!