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.

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.

Second, although this is probably a minor point, where you say foreach $key (sort keys %category), the sort 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.

#!/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"); }

In reply to Re: Poorly written script by Thelonius
in thread Poorly written script by Baffled

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.