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-follo...@perl.org>
wrote:

> # 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
>
>
>

Reply via email to