Skip Menu |
Report information
Id: 129344
Status: open
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: thundergnat [at] comcast.net
Cc:
AdminCc:

Severity: (no value)
Tag: Bug
Platform: (no value)
Patch Status: (no value)
VM: (no value)



From: thundergnat <thundergnat [...] comcast.net>
Subject: [BUG] "my" variable in a recursive subroutine leaks to callers scope
To: rakudobug [...] perl.org
Date: Fri, 23 Sep 2016 18:06:01 -0400
Download (untitled) / with headers
text/plain 894b
I have a subroutine that may recurse, that contains a "my" counter variable. Each recursion gets its own copy of the variable, but it seems that when the "inner" recursion returns, the "outer" version of the variable gets the contents from the "inner" routine. See cut down snippet: <perl6> sub process-list (@items, $level = 0) { my $count = 1; # leaks on recursion? multi sub process-item ($item, $level) { ' ' x $level ~ $count++ ~ ') ' ~ $item } multi sub process-item (@array, $level) { process-list(@array, $level + 1) } join "\n", map { process-item($_, $level) }, @items; } my @a = 'a', 'b'; put process-list([ 9, @a, 7, 6, @a, 15, 11 ]); </perl6> Output: Got 1) 9 1) a 2) b 3) 7 4) 6 1) a 2) b 3) 15 4) 11 Expected: 1) 9 1) a 2) b 2) 7 3) 6 1) a 2) b 4) 15 5) 11
Subject: Re: [perl #129344] [BUG] "my" variable in a recursive subroutine leaks to callers scope
From: Lloyd Fournier <lloyd.fourn [...] gmail.com>
Date: Sat, 24 Sep 2016 08:01:40 +0000
To: perl6-compiler [...] perl.org, bugs-bitbucket [...] rt.perl.org
Download (untitled) / with headers
text/plain 1.8k
Reproduced. Looks like you shouldn't nest subs inside recursive functions atm :S.
More golfed with just $level being used to demonstrate:

sub process-list (@items, $level = 0) {
    multi sub process-item ($item) {
        ('=' x $level) ~ $item;
    }

    multi sub process-item (@array) {
        process-list(@array, $level + 1)
    }

    join "\n", map &process-item, @items;
}

my @a = 'a', 'b';
put process-list([ 9, @a, 7, 6, @a, 15, 11 ]);
-----
9
=a
=b
=7
=6
==a
==b
==15
==11

^ It is never incremented/re-assigned but $level is still +1 after the call has finished.

On Sat, Sep 24, 2016 at 8:07 AM Steve Schulze <perl6-bugs-followup@perl.org> wrote:
Show quoted text
# New Ticket Created by  Steve Schulze
# Please include the string:  [perl #129344]
# in the subject line of all future correspondence about this issue.
# <URL: https://rt.perl.org/Ticket/Display.html?id=129344 >


I have a subroutine that may recurse, that contains a "my" counter
variable. Each recursion gets its own copy of the variable, but it seems
that when the "inner" recursion returns, the "outer" version of the
variable gets the contents from the "inner" routine.

See cut down snippet:

<perl6>

     sub process-list (@items, $level = 0) {

         my $count = 1;  # leaks on recursion?

         multi sub process-item ($item,  $level) { '    ' x $level ~
$count++ ~ ') ' ~ $item }

         multi sub process-item (@array, $level) { process-list(@array,
$level + 1) }

         join "\n", map { process-item($_, $level) }, @items;
     }

     my @a = 'a', 'b';
     put process-list([ 9, @a, 7, 6, @a, 15, 11 ]);

</perl6>

Output:

Got

1) 9
     1) a
     2) b
3) 7
4) 6
     1) a
     2) b
3) 15
4) 11

Expected:

1) 9
     1) a
     2) b
2) 7
3) 6
     1) a
     2) b
4) 15
5) 11


RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 202b
On Sat Sep 24 01:02:24 2016, lloyd.fourn@gmail.com wrote: Show quoted text
> Reproduced. Looks like you shouldn't nest subs inside recursive functions > atm :S.
I think it's only multis that suffer the problem. /jnthn


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org