Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print current directory using Perl

Tags:

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?

like image 390
mac Avatar asked Apr 18 '11 13:04

mac


People also ask

How do I print the current directory in 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.

What is my current working directory in Perl?

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.


1 Answers

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.

like image 111
justkt Avatar answered Sep 21 '22 09:09

justkt