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

In reply to Re: Problem with File::DirWalk and Active Perl by Marshall
in thread Problem with File::DirWalk and Active Perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.