Dear Monks,

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:

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 } } } }
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.

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


In reply to Iterator Problem with recursion by PetaMem

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.