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?
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 .
The dirname() method in Perl is used to get the directory of the folder name of a file.
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} .
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 $!;
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.
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.
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.
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...
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.
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
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.
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