Skip to content

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

@happy-barney

Description

@happy-barney

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions