Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a directory listing in Perl? [duplicate]

I would like to execute ls in a Perl program as part of a CGI script. For this I used exec(ls), but this does not return from the exec call.

Is there a better way to get a listing of a directory in Perl?

like image 538
user28280 Avatar asked Oct 15 '08 15:10

user28280


People also ask

How do I get a list of files in a directory in Perl?

If you want to get content of given directory, and only it (i.e. no subdirectories), the best way is to use opendir/readdir/closedir: opendir my $dir, "/some/path" or die "Cannot open directory: $!"; my @files = readdir $dir; closedir $dir; You can also use: my @files = glob( $dir .

How do I get the directory path in Perl?

The dirname() method in Perl is used to get the directory of the folder name of a file.

What is chdir in Perl?

chdir EXPR chdir FILEHANDLE chdir DIRHANDLE chdir. Changes the working directory to EXPR, if possible. If EXPR is omitted, changes to the directory specified by $ENV{HOME} , if set; if not, changes to the directory specified by $ENV{LOGDIR} .

How do I open a directory in Perl?

To open the directory, we use a function called opendir. You use this much like the open function to open files. In the example below, we open the /tmp directory: #!/usr/bin/perl use strict; use warnings; my $directory = '/tmp'; opendir (DIR, $directory) or die $!;

How to get the content of an array in Perl?

First line of the code will extract the first parameter and store it in the $directory variable. The second line tries to open the provided directory. Third line reads the content of opened directory into an array @flist. Fourth line in this Perl code closes the directory. And then a for loop lists the content of @flist array.

How to traverse a directory tree in Perl?

For traversing a directory tree in Perl, there are several ways/methods. Traversing can be performed through function calls opendir and readdir which are a part of Perl programming language. Traversing files and directories in Perl can also be done through File::Find module which comes with the Perl language.

Which line in this Perl code closes the directory?

Fourth line in this Perl code closes the directory. And then a for loop lists the content of @flist array. I hope it was useful for you. Please feel free to ask if you have any questions on this topic.


4 Answers

Exec doesn't return at all. If you wanted that, use system.

If you just want to read a directory, open/read/close-dir may be more appropriate.

opendir my($dh), $dirname or die "Couldn't open dir '$dirname': $!";
my @files = readdir $dh;
closedir $dh;
#print files...
like image 100
Leon Timmermans Avatar answered Oct 17 '22 18:10

Leon Timmermans


Everyone else seems stuck on the exec portion of the question.

If you want a directory listing, use Perl's built-in glob or opendir. You don't need a separate process.

like image 43
brian d foy Avatar answered Oct 17 '22 18:10

brian d foy


exec does not give control back to the perl program. system will, but it does not return the results of an ls, it returns a status code. tick marks `` will give you the output of our command, but is considered by some as unsafe.

Use the built in dir functions. opendir, readdir, and so on.

http://perldoc.perl.org/functions/opendir.html

http://perldoc.perl.org/functions/readdir.html

like image 8
J.J. Avatar answered Oct 17 '22 19:10

J.J.


In order to get the output of a system command you need to use backticks.

$listing = `ls`;

However, Perl is good in dealing with directories for itself. I'd recommend using File::Find::Rule.

like image 6
holli Avatar answered Oct 17 '22 19:10

holli