in reply to Returning the right results, but at the wrong time from subroutines

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.

Replies are listed 'Best First'.
Re^2: Returning the right results, but at the wrong time from subroutines
by leighgable (Acolyte) on Sep 07, 2009 at 00:50 UTC
    Wow!

    Thanks for these comments. I could sense that my "action at a distance" solution was probably not kosher, and I really like how you call an anonymous function in twig_roots and assign to the array from there. Really, thanks to both of you for commenting. I took the advice from your colleague below and updated my post with a link to a sample data file that is downloadable without registering, downloaded perltidy, ran my code through it, and reposted that, and added a snippet of data from one of the files I am parsing.

    Much thanks to both of you.