Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read in the contents of a directory in Perl?

How do I get Perl to read the contents of a given directory into an array?

Backticks can do it, but is there some method using 'scandir' or a similar term?

like image 994
kaybenleroll Avatar asked Aug 22 '08 14:08

kaybenleroll


People also ask

How do I view the contents of a directory?

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

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 .

What does Readdir do in Perl?

The Perl readdir is one of the function that can be used to read the directory files and the content of the data are checked and validated in line by line. Also, it will return the next directory which is already related to the same dir handle.

How do I open a file in path in Perl?

The filename argument is a path and you associate that with a filehandle to access its data: my $path = '/alias/a/1/sameName. txt'; open my $fh, '<', $path or die "Could not open $path: $!"; Perl doesn't care if another file in a different directory has the same name.


2 Answers

opendir(D, "/path/to/directory") || die "Can't open directory: $!\n"; while (my $f = readdir(D)) {     print "\$f = $f\n"; } closedir(D); 

EDIT: Oh, sorry, missed the "into an array" part:

my $d = shift;  opendir(D, "$d") || die "Can't open directory $d: $!\n"; my @list = readdir(D); closedir(D);  foreach my $f (@list) {     print "\$f = $f\n"; } 

EDIT2: Most of the other answers are valid, but I wanted to comment on this answer specifically, in which this solution is offered:

opendir(DIR, $somedir) || die "Can't open directory $somedir: $!"; @dots = grep { (!/^\./) && -f "$somedir/$_" } readdir(DIR); closedir DIR; 

First, to document what it's doing since the poster didn't: it's passing the returned list from readdir() through a grep() that only returns those values that are files (as opposed to directories, devices, named pipes, etc.) and that do not begin with a dot (which makes the list name @dots misleading, but that's due to the change he made when copying it over from the readdir() documentation). Since it limits the contents of the directory it returns, I don't think it's technically a correct answer to this question, but it illustrates a common idiom used to filter filenames in Perl, and I thought it would be valuable to document. Another example seen a lot is:

@list = grep !/^\.\.?$/, readdir(D); 

This snippet reads all contents from the directory handle D except '.' and '..', since those are very rarely desired to be used in the listing.

like image 148
jj33 Avatar answered Oct 14 '22 15:10

jj33


A quick and dirty solution is to use glob

@files = glob ('/path/to/dir/*'); 
like image 41
Pat Avatar answered Oct 14 '22 14:10

Pat