baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi,

well i have some problems with creating module, actually there wouldn't be any problems in the first place if the the perl could force erasing the data from the memory, while it is still working on the problem. well now i'm trying to separate functions by creating modules, but there are some problems like , i want to put a subroutine into the module, that takes some data from the main script, passes it into the sub(which is in the module), sub rearranges it and returns it into the variable in the main script.

>>MAIN SCRIPT<< use strict; use bignum; use Temp; my $f=@ARGV[0]; for (my $xx = 1;$xx<=100;$xx++){ my $x=Fact($f); print "$x"; } >>Module<< package Temp; sub Fact{ my $d = $_[0]; my $h =1; for (my $i = 1; $i<=$d; $i++){ $h *=$i; } return $h; } 1;
also how can i make that every time it finishes it with the module , it terminates that script (.pm). like in the example -- every time it finishes with the for loop in the main program it closes the .pm and then every time that opens the for loop it opens the .pm script.

thanx

Replies are listed 'Best First'.
Re: module problems
by pc88mxer (Vicar) on May 16, 2008 at 19:49 UTC
    If I understand you correctly, you want to reload a module. The basic mechanics of doing that is to delete the module's key in %INC and then require it again:
    use Temp; ... delete $INC{'Temp.pm'}; require Temp; # reloads the Temp.pm module
    See also Module::Reload and Module::Refresh in CPAN.
Re: module problems
by chromatic (Archbishop) on May 16, 2008 at 20:07 UTC
    also how can i make that every time it finishes it with the module , it terminates that script (.pm). like in the example -- every time it finishes with the for loop in the main program it closes the .pm and then every time that opens the for loop it opens the .pm script.

    Why would you want to do this? Without knowing that, I'm not sure anyone can give you a good suggestion.

      ok i want to do the same thing as:
      for(my $i...){ system ("perl foo.pl X Y"); ... }
      but this seams as pretty "hammered" way to do this . Why? because this is the only way i now how to clear my memory, all other functions do not work, if i don't do this it seams that perl looses the tag that tells him where he left some peace of data, and he can't clear the memory every time he repeats the process (so i have a hudge memory leakage). memory builds until there is no free memory left so the program crashes finaly.

      :)

        Given that leaks are often an indication that there a foible (bug?) in the code maybe a better thing would be to track down and fix the leak?

        Tools such as Devel::Size, Devel::Leak and Devel::DumpSizes will help. Note that if you want to use Devel::Leak with recent versions of Perl apply the patch at rt://22587 (thanks lima1). The discussion in Tracking down memory leaks may be of some help.

        On a different topic: Your C is showing! The Perlish way to iterate over a range is:

        for my $index (1 .. 500) { }

        which is shorter, clearer and more maintainable.


        Perl is environmentally friendly - it saves trees
Re: module problems
by Anonymous Monk on May 16, 2008 at 20:28 UTC
    The reason that you see this "persistence" is because your call to Fact is my $x=Fact($f) while the loop variable is $xx. $f is not a loop variant.