I have this code to print the current directory using Perl:
use Cwd qw(abs_path); my $path = abs_path($0); print "$path\n";
But it is displaying the filename of my script along with the directory.
Like this:
C:\Perl\duration.pl
I want it only to display C:\Perl\
.
How can I do it?
use Cwd qw(abs_path); my $path = abs_path($0); print "$path\n"; But it is displaying the filename of my script along with the directory.
getcwd and friends Each of these functions are called without arguments and return the absolute path of the current working directory. my $cwd = getcwd(); Returns the current working directory.
To get the current working directory (pwd
on many systems), you could use cwd()
instead of abs_path
:
use Cwd qw(); my $path = Cwd::cwd(); print "$path\n";
Or abs_path
without an argument:
use Cwd qw(); my $path = Cwd::abs_path(); print "$path\n";
See the Cwd docs for details.
To get the directory your perl file is in from outside of the directory:
use File::Basename qw(); my ($name, $path, $suffix) = File::Basename::fileparse($0); print "$path\n";
See the File::Basename docs for more details.
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