Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"my" variable in a recursive subroutine leaks to callers scope #5690

Open
p6rt opened this issue Sep 23, 2016 · 4 comments
Open

"my" variable in a recursive subroutine leaks to callers scope #5690

p6rt opened this issue Sep 23, 2016 · 4 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Sep 23, 2016

Migrated from rt.perl.org#129344 (status was 'open')

Searchable as RT129344$

@p6rt
Copy link
Author

p6rt commented Sep 23, 2016

From @thundergnat

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

@p6rt
Copy link
Author

p6rt commented Sep 24, 2016

From @LLFourn

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​:

# 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-archive.perl.org/perl6/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 \(@&#8203;items, $level = 0\) \{

     my $count = 1;  \# leaks on recursion?

     multi sub process\-item \($item,  $level\) \{ '    ' x $level ~

$count++ ~ ') ' ~ $item }

     multi sub process\-item \(@&#8203;array, $level\) \{ process\-list\(@&#8203;array,

$level + 1) }

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

 my @&#8203;a = 'a', 'b';
 put process\-list\(\[ 9, @&#8203;a, 7, 6, @&#8203;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

@p6rt
Copy link
Author

p6rt commented Sep 24, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Sep 28, 2016

From @jnthn

On Sat Sep 24 01​:02​:24 2016, lloyd.fourn@​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

@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant