Dear Monk, I've been away for awhile and my scoping skills are getting weak. Could I get some pointers on why I get the wrong directory size total on the code below? I wish to add up all the file sizes below my dad root.

package MyApache::SF_Upload; ... use vars qw($UPLOAD_DIR $dir_size); sub handler { ... # Check that the directory is not getting full. $dir_size = 0; my $dirsz = dir_size( $UPLOAD_DIR ."/$dad" ); if ( $dirsz > MAX_DIR_SIZE ) { $apr->log_error("Upload directory is nearly full, $dirsz" ); return 507; # HTTP_INSUFFICIENT_STORAGE } $apr->warn("Upload directory is nearly full, $dirsz" ); ... } sub dir_size { my $dir = shift; # Loop through files and sum the sizes; this will descend down sub +dirs opendir DIR, $dir or die "Unable to open $dir: $!"; while ( $_ = readdir DIR ) { stat("$dir/$_"); if (-d _ ) { $dir_size += dir_size("$dir/$_") unless ($_ eq "." || $_ eq ".."); } else { $dir_size += -s _ ; } warn("DIR filename: $dir/$_ at $dir_size"); } return $dir_size; }

Everything goes along fine, printing the warn messages to the log, then we hit the one and only subdirectory. It descends fine, accumulating the sizes into $dir_size and printing the warn messages, too.

Then, I get completely out of the dir_size sub, with a warn message of exactly double the bytes of the last directory warn message. Nothing is printed in between, and the total is missing quite a few files. I think it hit the return and exited both of the recursive paths.

Could it be the perl version that was shipped with Oracle Apache? (5.6.1) The OS is RedHat AS v2.1 2.4.9-e.27smp. This is a modperl program, if that matters.


In reply to Recursive tree walk with scoping issues by Lhamo Latso

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.