Possible Duplicate:
How can I list all of the files in a directory with Perl?
I want to loop through a few hundred files that are all contained in the same directory. How would I do this in Perl?
You can use readdir or glob. Or, Path::Tiny: @paths = path("/tmp")->children; @paths = path("/tmp")->children( qr/\. txt$/ );
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 .
Find modules in Perl has all the functions similar to the Unix Find command. Find function takes two arguments: 1st argument is a subroutine called for each file which we found through find function. 2nd argument is the list of the directories where find function is going to search the files.
#!/usr/bin/perl -w my @files = <*>; foreach my $file (@files) { print $file . "\n"; }
Where
@files = <*>;
can be
@files = </var/www/htdocs/*>; @files = </var/www/htdocs/*.html>;
etc.
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