From a quick glance over your code I didn't find your problem, but two things I noticed:

First, you use code like this:

my @variable; for (... ) { ... undef @variable; }

It is much saner to declare @variable inside the for-lop instead, so it gets automatically cleaned when the loop body is exited:

for (...) { my @variable; ... # no manual cleanup necessary. }

So you keep the scope of your variables small, you can't forget to clean up, and if you exit the current iteration with next, last or continue it is still cleaned up.

Update: I just noticed that you access these variables from subroutines too. Then you also have to declare this subroutine inside the loop - which is a good idea anyway, because then you don't get confusing "action at a distance". So the start of your loop might look like this:

foreach $filepath (get_file_list($dir)) { my (%company_hist, @company_list, %reference_hist, @reference_list +) ; my $twig = XML::Twig -> new( twig_roots => { company => sub { push @company_list, $_[1]->{'att'}->{'co +de'} }, ... } );

The second thing I noticed was some commented out debugging code:

#my($k, $v); #while ( ($k,$v) = each %reference_hist ) { #print "$k => $v\n"; #}

For debugging Data::Dumper (a core module) is very helpful, you can just write

use Data::Dumper; ... print Dumper \%reference_hist;

to get good debugging output for just about any data structure.

I hope this is useful to you. And great to the see self-styled beginner using strict and warnings!

Perl 6 - links to (nearly) everything that is Perl 6.

In reply to Re: Returning the right results, but at the wrong time from subroutines by moritz
in thread Returning the right results, but at the wrong time from subroutines by leighgable

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.