I'm looking for a way to get two pieces of information:
I know you can use $0
to get the file name, but are there any other reserved variables that are native to Perl that will give me what I'm looking for?
I'd rather not use any special modules, but if it is the only way, then so be it.
Using FindBin
and Cwd
:
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd ();
use FindBin ();
use File::Spec ();
my $full_path = File::Spec->catfile( $FindBin::Bin, $FindBin::Script );
my $executed_from_path = Cwd::getcwd();
print <<OUTPUT;
Full path to script: $full_path
Executed from path: $executed_from_path
OUTPUT
Sample output (script saved as /tmp/test.pl
):
alanhaggai@love:/usr/share$ /tmp/test.pl
Full path to script: /tmp/test.pl
Executed from path: /usr/share
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