12.5. Directory handlesFor a platform independent approach use opendir and readdir. Example 12-3. examples/shell/list_directory.pl #!/usr/bin/perl
use strict;
use warnings;
my $dir = shift or die "Usage: $0 DIRECTORY\n";
opendir my $dh, $dir or die "Cannot open $dir: $!\n";
while (my $entry = readdir $dh) {
if ($entry eq "." or $entry eq "..") {
next;
}
print "$entry\n";
}
closedir $dh;
in LIST context readdir returns all the files in the directory. opendir(my $dh, "/etc") or die $!; @files = readdir $dh;
|
Follow me: