[perl #129344] [BUG] "my" variable in a recursive subroutine leaks to callers scope

2016-09-28 Thread jn...@jnthn.net via RT
On Sat Sep 24 01:02:24 2016, lloyd.fo...@gmail.com wrote:
> Reproduced. Looks like you shouldn't nest subs inside recursive functions
> atm :S.

I think it's only multis that suffer the problem.

/jnthn


Re: [perl #129344] [BUG] "my" variable in a recursive subroutine leaks to callers scope

2016-09-24 Thread Lloyd Fournier
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 
wrote:

> # New Ticket Created by  Steve Schulze
> # Please include the string:  [perl #129344]
> # in the subject line of all future correspondence about this issue.
> # 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:
>
> 
>
>  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 ]);
>
> 
>
> 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
>
>
>


[perl #129344] [BUG] "my" variable in a recursive subroutine leaks to callers scope

2016-09-23 Thread via RT
# New Ticket Created by  Steve Schulze 
# Please include the string:  [perl #129344]
# in the subject line of all future correspondence about this issue. 
# 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:



 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 ]);



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