Hi Perl Monks, I'm trying to list the contents of a directory (/opt/tmp). I have tried the following options:
Option 1
my @files = grep(-f, <*.*>); foreach my $file (@files) { print "file = $file\n"; }
This code only list the files in my current directory /opt/, but i need to list the files in directory /opt/tmp/
Option 2
my @files = glob("/opt/tmp/*.*"); foreach my $file (@files) { print "file = $file\n"; }
This code doesn't print anything. I'm not even sure the glob works.
Option 3
my @files; opendir(DIR, "/opt/tmp/" or die "Cannot open dir"); @files = grep {-T "/opt/tmp/$_"} readdir DIR; foreach my $file (@files) { print "file = $file\n"; } closedir(DIR);
When i execute this code i always get an error: "Cannot open dir". However, the directory exists and has files in it.
I also tried some other options related to opendir, but the output was empty.
Can someone help me?
Thanks
In reply to Perl List files in directory by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |