- first note on your code is that you blatantly waste CPU cycles with double-quotes interpolation. heh, this is habit seen to many people that came to Perl from other programming language like C/C++ where a string should always be limited by double quotes, but many other languages (and Perl is one of them) offer extended functionality through these semantic sugarcubes :)
e.g.:
"." -> '.'
"*END*" -> '*END*'
"support\@mydomain.com" -> 'support@mydomain.com'
... etc ...
- check
Perl FAQ for a perlish way of getting your hostname
- i'd rather consider using
getpwuid and a caching system instead of pushing all users information into memory
- run this script on a cron basis scanning user writeable filesystems and you'll soon get headaches (considering of course that users are always b.a.d.) - they could create a heck lotta dumb files to make your script trash your system (heh, my first thought was to tell you to switch to a more efficient storage method for the
%files data structure)
- presuming a MB equals '1000000' is wrong. indeed, HDD manufacturers inherited this darn habit, but this is not true on the filesystem thay lies onto that storage space ;-)
- you waste again a lots of CPU cycles performing a lot of useless
stats: file used in your
wanted routine as
$_ has already been
stat-ed by the
File::Find module, so now you can rely on the information in
_;
- as a principle: restrict the context of each variable as much as possible to avoid problems ;-]
(i'm talking about most of the variables on this line:
my ($uid, %files, $size, @pw, %unames, $str);)
Here is a trimmed down code that implements that cache I told you above, eliminates all useless stats and switches %files to hash of arrays ;-]
use Data::Dumper;
use Cache::MemoryCache ();
my $cache = new Cache::MemoryCache;
find({wanted => \&ab_wanted, no_chdir => 1}, $opt{d});
print Dumper \%files;
sub ab_wanted {
if (-r _ && -f _){
my $size = int ((-s _) / 2**20);
if ($size > $opt{s} && $opt{a} < -A _){
my $uid = (lstat(_))[4];
my $name = $cache->get($uid);
unless (defined $name) {
$name = getpwuid($uid);
$cache->set($uid, $name);
}
push @{$files{$name}}, [ $_, $size, int -A _ ];
}
}
}