I found/modified this chunk of code when I wanted to find the total size of files matching a pattern in a directory, i.e. the total size of *.pm in ~/. I first used it on win32 in a perlTk program so I know it works well.
package sizes;
require 5.001;
use strict;
use vars qw(@ISA $VERSION);
$VERSION = "1.0";
@ISA = qw(Exporter);
sub new {
my $self = {};
my $pkg = shift;
$self->{dir} = shift;
$self->{pat} = shift;
$self->{dh} = undef;
bless $self;
opendir $self->{dh}, $self->{dir}
|| warn "cannot open dir: [".$self->{dir}."] $!\n";
return $self;
}
sub check {
my $self = shift;
$self->{size} = 0;
while (my $file = readdir($self->{dh})) {
next unless $file =~ /$self->{pat}/;
my $fqpn = $self->{dir}.'/'.$file;
print STDERR "size $fqpn: ".$self->{size}."\n";
$self->{size} += -s $fqpn;
print STDERR "size $fqpn: ".$self->{size}."\n";
}
print STDERR "rewinding\n";
rewinddir($self->{dh});
return $self->{size};
}
sub DESTROY {
my $self = shift;
closedir $self->{dh};
}
1;
use sizes;
$checker = sizes->new($dir,$pat);
$size=$checker->check();
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.