[EMAIL PROTECTED] wrote:
"  Hi Perl Hacker! I have a set of scripts in use on my system that i would
" like to start tweaking. They are all hand written and not someone elses
" work. I would like to start reducing the memory it takes to run the scripts.
" I want to start implementing undef declarations in the scripts, is there a
" method to this madness, do you actually have to declare before undef'ing
" them? A simple example of memory hogging is:
" 
" # Opening a file
" sub somesub {
" open(FILE, "user.data");
" @SOMEVAR = <FILE>
" foreach $_ (@SOMEVAR) {
" chop ($_);
" @SPLITED=split(/\|/, $_);
" ... some code here
" undef @SPLITTED;
" }
" undef @SOMEVAR;
" }
" 
" is this the correct format for reducing memory usage as this sample routing
" pulls in a large file and splits it up then analyes it and spits it to
" screen via web.? The typical size of the perl viewed via top during usage is
" around 125 to 150 megs each execution, which when 15-20 people are running
" it means you need a real mean machine. Typically my machine just hangs and
" starts to que the processes and people get overly anxios and hit reload just
" doubling and so on the executions of these scripts.

You'd do better to, well, not read the whole file into memory.

Try:

use strict;

sub somesub {
    my (@tmp, @SPLITTED);
    open FILE, "< user.data" or die "can't open: $!"; # always check!
    while (<FILE>) {                # same effect
        chop;                       # implicitly chops $_
        @tmp = split '|';           # implicitly splits $_
        format_and_output(@tmp);    # handle @tmp here...
        push @SPLITTED, @tmp;       # ...or store if you must
    }
    close FILE or die "can't close: $!";
}

If you can process everything from within that somesub() and any
sub-functions, then all your _lexically-scoped_ my vars will go out of
scope after somesub(), and any memory it _does_ use will be
garbage-collected upon returning from somesub().

And while you're learning good perl style (such as idioms and checking
returns from open() ), check out

$ perldoc -q 'entire file'

-- 
Watch my captor grow old and die.
No satisfaction. Still here.
    -- Morpheus, The Sandman
_______________________________________________
Perl-Unix-Users mailing list. To unsubscribe go to 
http://listserv.ActiveState.com/mailman/subscribe/perl-unix-users

Reply via email to