Hello ... I am writing using Active Perl 5.12.4 and the File::DirWalk module I installed using Active State's ppm. The OS in Windows 7.

I am writing a script to run up and down a directory tree and count how many of a few types of file I find and how much space they take up. As it runs, PERL reports the following errors repeatedly:

readdir() attempted on invalid dirhandle at C:/Perl/site/lib/File/Dir +Walk.pm line 100. closedir() attempted on invalid dirhandle at C:/Perl/site/lib/File/Di +rWalk.pm line 117.
This appears to happen on empty directories. The directory I am searching starts at C:\Users\Peter Lutz\Documents, which also contains some hidden and (perhaps) system files.

The code in DirWalk that the errors point to is:

opendir my $dirh, $path || return FAILED; my @dir_contents = readdir $dirh; @dir_contents = File::Spec->no_upwards(@dir_contents); foreach my $f (@dir_contents) { # be portable. my @dirs = File::Spec->splitdir($path); my $path = File::Spec->catfile(@dirs, $f); my $r = $self->walk($path); if ($r == PRUNE) { last; } elsif ($r != SUCCESS) { return $r; } } closedir $dirh;
Finally, MY code is:
#!/usr/bin/perl use strict; use warnings; ##### # TreeWalker.pl # This script walks a directory tree starting at the directory given # on the command line (uses the current directory if none given). # It keeps track of the number and total size of: # Files # Files whose name ends in ~ # Files whose name ends in .pl # Files whose name ends in .txt # Directories # # and reports these numbers at the end of the script. ##### use Path::Class qw(file dir); use File::DirWalk; 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 ##### # d o F i l e # Process one file as the tree is walked ##### sub doFile { my $path = shift; my $file = file(split("/[\\\/]", $path)); # Make the path a 'fi +le' my $stat = $file->stat(); # stat the file if (not defined $stat) { return; } # wierd error ... ig +nore file $fileN++; # Incr. # of files $fileSize += $stat->size; # Incr. file size my $base = $file->basename; # Get the base to c +heck for special names if($base =~ m/~$/) { $backupN++; # backup files (end + with ~) $backupSize += $stat->size; } elsif($base =~ m/\.txt$/) { $txtN++; # text files (end with + .txt) $txtSize += $stat->size; } elsif($base =~ m/\.pl$/) { $plN++; # perl files (end w +ith .pl) $plSize += $stat->size; } return File::DirWalk::SUCCESS; # return TRUE so wal +k continues } ##### # d o D i r # Process one directory as the tree is walked ##### sub doDir { my $path = shift; my $dir = dir(split(/[\\\/]/, $path)); # Make the path a 'd +ir' my $stat = $dir->stat(); # Stat the dir if (not defined $stat) { return; } # Wierd error ... ig +nore the dir $dirN++; # Increment the # of d +irs $dirSize += $stat->size; # Increment the size o +f dirs return File::DirWalk::SUCCESS; # return TRUE so wal +k continues } ##### # 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 %-20d\n", "Directories", $dirN, $dirSize; } ##### M a i n P r o g r a m ##### my $currdir = "."; $currdir = $ARGV[0] unless scalar(@ARGV) < 1; $currdir = dir(split("/[\\\/]", $currdir)); die "$currdir is NOT a directory" if not $currdir->is_dir(); my $walker = File::DirWalk->new; $walker->onFile(\&doFile); $walker->onDirEnter(\&doDir); $walker->walk($currdir); printStats();
I know that was long, but I figure better more info than less. Please understand!

Does anyone have experience with this, or insight into what is going on?

Thanks ... phlpittsny


In reply to 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.