perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

experiment refaliasing: global cursor variable holds alias to last item of looped-over list

Open happy-barney opened this issue 2 weeks ago • 0 comments

Problem

Perl supports multiple ways how to declare cursor for for-over version of for statement. One of them is to use existing variable, eg:

my $foo;
for $foo (@list) { ... }

In such case, $foo (doesn't matter whether it is my, or our) preserves its original value after loop.

But not when refaliasing is used, then variable is loosing its previous value and remains to be an alias to last value processed by the loop.

Steps to Reproduce

use experimental qw (refaliasing);

my @foo = qw (Foo Bar);
our $cursor = 42;

for \ $cursor (\ $foo[0], \ $foo[1]) {
}

# Outputs: Bar
say q ($cursor: ), $cursor;

$cursor = q (Zoo);

# Outputs: Foo Zoo
say q (@foo: ), join q ( ) => @foo;

Expected behavior

Reference is broken after loop finishes and original value is restored.

Perl configuration Behaviour exists in blead, is also covered by test in my PR #23873, test for-over-scope.t.

happy-barney avatar Nov 22 '25 16:11 happy-barney