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