When I was making some tests of Class::HPLOO, where multiple documents are filtered in the same process, I found a very strange bug, that only happens if you use my and recursivity in a strange way.

Soo, I tried to isolate the bug and make sample of it:

{ package RecMy ; use strict ; sub rec { my $n = shift ; return if !$n ; rec( --$n ) ; my $local = "[$n] set" if ($n/3) !~ /\./ ; $local .= ' append' ; print "<$n> $local\n" ; } for(1..5) { print "--------------------- $_\n" ; rec(4); } }
The BUGGED output is:
--------------------- 1 <0> [0] set append <1> append <2> append <3> [3] set append --------------------- 2 <0> [0] set append <1> append append <2> append append <3> [3] set append --------------------- 3 <0> [0] set append <1> append append append <2> append append append <3> [3] set append --------------------- 4 <0> [0] set append <1> append append append append <2> append append append append <3> [3] set append --------------------- 5 <0> [0] set append <1> append append append append append <2> append append append append append <3> [3] set append
But the right output should be:
--------------------- 1 <0> [0] set append <1> append <2> append <3> [3] set append --------------------- 2 <0> [0] set append <1> append <2> append <3> [3] set append --------------------- 3 <0> [0] set append <1> append <2> append <3> [3] set append --------------------- 4 <0> [0] set append <1> append <2> append <3> [3] set append --------------------- 5 <0> [0] set append <1> append <2> append <3> [3] set append
And to produce the right output I have just changed one line of the code:
## changed from (bugged): my $local = "[$n] set" if ($n/3) !~ /\./ ; ## to (fine): my $local ; $local = "[$n] set" if ($n/3) !~ /\./ ;

Soo, why this happens? It seems that my() creates the same scalar 2 times? Or the scalar is not destroied after end the scope of the sub? Or this is a optimization bug? I think that this is a reference count bug, but I haven't looked the Perl CORE yet to really say that.

Note that the bug only happens if the interpolation exists, that is made by:

... if ($n/3) !~ /\./ ;
I saw the bug first on Perl-5.6.1, than I saw it on Perl-5.8.2 and Perl-5.8.3 too. Soo, wasn't patched yet!

I will wait some advices from the monks before send it to perlbug...

Graciliano M. P.
"Creativity is the expression of the liberty".


In reply to This is really a Perl BUG with my?! Need some advice... by gmpassos

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.