in reply to Problem with File::DirWalk and Active Perl

Odd, this first line  opendir my $dirh, $path || return FAILED;
should be opendir (my $dirh, $path) || return FAILED; or opendir my $dirh, $path or return FAILED; for it to work right. The || has a higher precedence than "or", so the parens are needed if that operator is used.

Update:

I recoded your routine with File::Find to show how to use this critter. I was not sure what this "Directory Size" thing was about? The size of the directory is the sum of the sizes of the files.

I did use the special _ (underscore, not $_) variable. When Perl does a file test, it does a stat() and if you use the _ variable for another file test, it uses the cached results from stat() from the previous file test. This speeds things up considerably if you are doing a lot of file tests. $File::Find::name is the name of the current file, $File::Find::dir is the name of the current directory.

I did not put an explicit stat() call in the wanted subroutine. I've seen this in older code. There was a reason for this as some obscure thing could happen that could cause stat() to fail. I did some searching around for that reason but wasn't able to find it. Maybe some other Monk knows? Anyway, I don't think that is necessary anymore - so I didn't do it. Also, I think if the stat fails, the file test fails, so the way the code below is written, this would have the effect of ignoring that file.

This Dir::Walk thing looks pretty worthless to me.
Anyway this code runs on my machine... have fun!

#!/usr/bin/perl use strict; use warnings; use File::Find; my $fileN = 0; # Counter for number of files my $fileSize = 0; # Total size of files my $backupN = 0; # Counter for number of backup files my $backupSize = 0; # Total size of backup files my $txtN = 0; # Counter for number of text files my $txtSize = 0; # Total size of text files my $plN = 0; # Counter for number of PERL scripts my $plSize = 0; # Total size of PERL scripts my $dirN = 0; # Counter for number of directories my $dirSize = 0; # Total size of directories ??? what ??? find (\&wanted, ('C:/temp')); #note: find takes a list of directories printStats(); sub wanted { if (-d) { $dirN++; return; } if (-f _) { $fileN++; $fileSize += -s _; if ($File::Find::name =~ m/~$/) #backup { $backupN++; $backupSize += -s _; } elsif ($File::Find::name =~ m/\.txt$/) { $txtN++; $txtSize += -s _; } elsif($File::Find::name =~ m/\.pl$/) { $plN++; $plSize += -s _; } } } ##### # p r i n t S t a t s # Print out all counters at the end of the walk ##### sub printStats { printf "%-12.12s %-15d %-20d\n", "Files", $fileN, $fileSize; printf " %-8.8s %-15d %-20d\n", "Backup", $backupN, $backupSize +; printf " %-8.8s %-15d %-20d\n", "Text", $txtN, $txtSize; printf " %-8.8s %-15d %-20d\n", "PERL", $plN, $plSize; print "\n"; printf "%-12.12s %-15d\n", "Directories", $dirN; } __END__ .....prints on my machine:..... Files 2620 671260264 Backup 0 0 Text 155 15244026 PERL 974 6015816 Directories 46