- if (-d $file)
- {
- print $file." is a directory\n";
- } else {
- print $file." is a file\n";
- }
When this is used in pair with IO:Dir, which helps you to enumerate contents of a given directory, one non-obvious step should not be forgotten:
- tie %dir, 'IO::Dir', $dir;
- foreach my $entry(keys %dir) {
- next if ($entry eq '.' or $entry eq '..');
- # important part is here: concatenation with the full path
- if (-d $dir."/".$entry)
- {
- print $entry." is a directory\n";
- }
- }
2 comments:
Before declaring $file to be a file, you may also want to test (-f $file) to verify that it is in fact a regular file. Especially if you're on a unix-type system, where it could be a symbolic link, device node, etc. which are neither directories nor (regular) files.
Thanks, Dave, that is an excellent addition.
Post a Comment