in reply to Returning the right results, but at the wrong time from subroutines
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!
|
|---|
| 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 |