Skip to content

Lexical subroutines can't capture refalias in the same block #24012

@leonerd

Description

@leonerd

While investigating issues around refalias, declared_refs, and the upcoming PPC0034 (refalias in signatures), I encountered the following cornercase:

use v5.36;
use experimental qw( refaliasing declared_refs );

my \@arr = [1, 2, 3];
my sub inner { say "Inside inner, array is <@arr>"; }
say "Outside, array is <@arr>";
inner();

It produces output, showing that while the refalias affected the @arr variable in the outer scope, it did not affect the one captured by the inner sub:

Outside, array is <1 2 3>
Inside inner, array is <>

A slight modification of the program to put the lexical sub in its own nested block makes it behave as expected:

use v5.36;
use experimental qw( refaliasing declared_refs );

my \@arr = [1, 2, 3];
{
  my sub inner { say "Inside inner, array is <@arr>"; }
  say "Outside, array is <@arr>";
  inner();
}
Outside, array is <1 2 3>
Inside inner, array is <1 2 3>

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions