PetaMem has asked for the wisdom of the Perl Monks concerning the following question:
today I spent my day bughunting a little subroutine and although it seems unlikely to me, I am about to state, that this is a Perl bug:
the problem of this routine is, that it seems to repeat the iteration within the for(@out) loop when returning from the recursive call to itself.sub analyze { my $htree = shift; my $rul = shift; my $hist = (split /\n/, $htree->data)[0]; my $str = (split /\|/, $hist)[0]; my ($k, $v); $hist = "|$hist"; while(($k,$v) = each %$rul) { my @out = @{&proccode($str,$v)}; for my $h (@out) { if($hist !~ /\|$h\|/) { my $new = $htree->append("$h$hist\n$k"); &analyze($new,$rul); # PROBLEM: the @out loop is iterated again # even a 'next' does not force it to increment/end } } } }
Which of course results in an endless loop for some cases. Even if I try to force it with "next" after the recursive call, it seems to be ignored.
Even if @out contains only one element, this happens.
So what is happening here? If I comment out the recursion call, the for(@out) loop behaves as it should.
sidenote: I encountered already a bug in Perl 5.8.0 when doing goto jumps within a for-loop (variables became undefined) - this was gone in 5.8.6 - which is what I'm using now.
Update: The 5.8.0 bug is ilustrated here.
Update2: Thanks to Fletch, Splinky and tlm, and because I had nothing to loose, I tried a modified version without the each iterator. Although I still do not see why it should influence the for(@out) loop, NOW IT WORKS! Instead of copying the hash, I extracted the keys into their own list.
... my @rules = keys %$rul; for my $k (@rules) { my @out = @{&proccode($str,$$rul{$k})}; ...
Bye
PetaMem All Perl: MT, NLP, NLU
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Iterator Problem with recursion
by Fletch (Bishop) on Apr 26, 2005 at 17:09 UTC | |
by splinky (Hermit) on Apr 26, 2005 at 17:15 UTC | |
by PetaMem (Priest) on Apr 26, 2005 at 17:26 UTC | |
by tlm (Prior) on Apr 26, 2005 at 17:49 UTC | |
by PetaMem (Priest) on Apr 26, 2005 at 18:09 UTC | |
Re: Iterator Problem with recursion
by diotalevi (Canon) on Apr 27, 2005 at 04:47 UTC | |
Re: Iterator Problem with recursion
by eibwen (Friar) on Apr 26, 2005 at 17:13 UTC | |
by PetaMem (Priest) on Apr 26, 2005 at 17:49 UTC | |
Re: Iterator Problem with recursion
by moot (Chaplain) on Apr 26, 2005 at 17:08 UTC | |
by PetaMem (Priest) on Apr 26, 2005 at 17:43 UTC |