note
Thelonius
It hard to see how it could use quite so much memory, but there are a couple of obvious ways to reduce what it does use.
<p>
First, you are building an array to hold all the names of the text files in each directory, when all you need is the count. So you should just count in a loop. I notice that the readdir documentation in perlfunc only shows an example of readdir in list context, but for this purpose, a loop is better. If there is some huge directory with 100,000 files, that could use a lot of memory, but that seems unlikely.
<p>
Second, although this is probably a minor point, where you say <code>foreach $key (sort keys %category)</code>, the <code>sort</code> is causing an array of all the keys to be built. Since this is unnecessary for this use, you can just leave the sort out.
<code>
#!/usr/bin/perl
use Carp;
use strict;
use vars qw(%config %category %form %super);
require "/var/www/vhosts/mysite.com/cgi-bin/categories.cgi";
$config{'basepath'} = '/var/www/vhosts/mysite.com/cgi-bin/';
$config{'bluedir'} = 'register';
sub count_text_files {
my ($dirname) = @_;
my $result = 0;
if (!opendir DIR, $dirname) {
carp "Cannot open $dirname: $!\n";
return 0;
}
while (defined(my $file = readdir DIR)) {
if (-T "$dirname/$file") {
++$result;
}
}
close DIR;
return $result;
}
my $key;
my $numusers = 1;
$numusers += count_text_files("$config{'basepath'}$config{'bluedir'}");
my $totalfiles = 100;
foreach $key (keys %category) {
$totalfiles += count_text_files("$config{'basepath'}$key");
}
</code>
667311
667311