My perl is a bit rusty so I'm posting this to make sure I'm doing things the "right way".

I've got a one-to-many list of values that I read in using the following code (the %dtgs hash is defined globally)

open(my $dtg_file, "<", $infile) or die "Unable to open $infile: $!\n" +; while(<$dtg_file>) { chomp; my ($dtg,@files) = split /:/; $dtgs{$dtg} = \@files; } close $dtg_file;

I do some processing and when a match is found to one of the files, I want to remove it to speed up further processing (there are about 60k files and they are being compared to over 100 million files, looking for matches).

sub remove_from_dtgs { my ($dtg,$file) = @_; my @files = grep {$_ ne $file} @{$dtgs{$dtg}}; if(@files == 0) { delete $dtgs{$dtg}; } else { $dtgs{$dtg} = \@files; } }

I want to make sure that I'm not creating a memory leak by replacing $dtgs{$dtg} with the new array. If memory serves (no pun intended), perl will detect there are no longer any references to the old array and will free up the memory. But this script is going to run for a long time (see 100 million files above) and I want to avoid any issues.

Other optimization suggestions are also welcome. Thanks in advance!

Edited with corrections from kennethk


In reply to Avoiding Memory Leaks by wink

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.