The advice "don't roll your own solution - use a module" applies to system utilities as well. Perhaps even more so. Your solution is wrong, as it doesn't take links into account.
#!/usr/bin/perl use strict; use warnings; use File::Spec::Functions qw( no_upwards ); use File::Find; use List::Util qw( sum ); my $dir = "foo/bar"; # # Clear directory if it exists. # system "rm", "-rf", $dir and die; # # Create new directory # system "mkdir", "-p", $dir and die; # # Create big file. # system "dd", "if=/dev/zero", "of=$dir/big1", "bs=8096", "count=1024" a +nd die; # # Create a link # link "$dir/big1", "$dir/big2" or die "link: $!"; # # Calculate size by Aristotle method: # my ( $num, $size1 ); find( { preprocess => sub { my @l = no_upwards( @_ ); $num += @l; $size1 += sum map -s, @l; return @_; }, wanted => sub {}, }, $dir ); # # Calculate size by not rolling your own: # my ($size2) = `du -b $dir` =~ /\d+/g; printf "System thinks size equals %d kbytes, Aristotle thinks %d kbyte +s\n", $size2 / 1024, $size1 / 1024; __END__ 1024+0 records in 1024+0 records out System thinks size equals 8130 kbytes, Aristotle thinks 16192 kbytes
And the existance of hardlinks isn't the only thing your solution gets wrong.

In reply to Re^2: Get the Total Size & Total Files in a Directory by Anonymous Monk
in thread Get the Total Size & Total Files in a Directory by gopalr

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.