Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow multivariable foreach to use refaliases#24094

Merged
leonerd merged 5 commits intoPerl:bleadfrom
leonerd:foreach-multivar-refalias
Jan 22, 2026
Merged

Allow multivariable foreach to use refaliases#24094
leonerd merged 5 commits intoPerl:bleadfrom
leonerd:foreach-multivar-refalias

Conversation

@leonerd
Copy link
Contributor

@leonerd leonerd commented Jan 17, 2026

Fixes #24027

As noted by #24027, while multivar foreach and refalias foreach have each been available separately for some time now, it wasn't possible to combine the two until now.

This current implementation does have limits on the number of variables in total are allowed in a multivariable foreach loop if any are refaliases (no more than 256), and which ones can be refaliases (none past the 24th) that are all to do with the way I am abusing the U32 op_targ field to store the variable count in the lower 8 bits, and a bitmask of which vars are refaliases in the upper 24 bits. I decided to do that as it means I don't have to make the OP_ITER op any larger - such as by expanding it to an UNOP_AUX to give it that extra storage space. In practice I don't imagine those limits will be a problem for any practical use-case.

If in a future version we did want to expand on those limits, I think it would be possible by moving the refalias_mask storage to a PV stored in the UNOP_AUX vector, or somesuch.

  • This set of changes requires a perldelta entry, and I'll write it today

New helper functions to avoid some copy-paste. Also because I'm
otherwise about to add a third copy.
Currently these are all scalar variables, but soon I will add the
ability for iteration variables to be refaliases, so they won't
necessarily all be scalars any more.
 * Define a new private op constant OPpITER_REFALIAS
 * Presence of this flag on OP_ITER means the iteration var is a
   refalias
 * Throw away the LVREF/REFGEN part of the optree in favour of just
   setting this flag

This change is useful on its own, and also a good stepping-stone towards
being able to implement multiple refalias variables in foreach; as per
  Perl#24027
@leonerd leonerd force-pushed the foreach-multivar-refalias branch from 70dacfa to 59b135e Compare January 17, 2026 14:12
As noted by Perl#24027, while multivar
foreach and refalias foreach have each been available separately for
some time now, it wasn't possible to combine the two until now.

This current implementation does have limits on the number of variables
in total are allowed in a multivariable foreach loop if any are
refaliases (no more than 256), and which ones can be refaliases (none
past the 24th) that are all to do with the way I am abusing the U32
`op_targ` field to store the variable count in the lower 8 bits, and a
bitmask of which vars are refaliases in the upper 24 bits. I decided to
do that as it means I don't have to make the `OP_ITER` op any larger -
such as by expanding it to an UNOP_AUX to give it that extra storage
space. In practice I don't imagine those limits will be a problem for
any practical use-case.

If in a future version we did want to expand on those limits, I think it
would be possible by moving the `refalias_mask` storage to a PV stored
in the UNOP_AUX vector, or somesuch.
@leonerd leonerd force-pushed the foreach-multivar-refalias branch from 59b135e to 0824dfe Compare January 17, 2026 15:26
@leonerd leonerd merged commit da6c78e into Perl:blead Jan 22, 2026
33 checks passed
@leonerd leonerd deleted the foreach-multivar-refalias branch January 22, 2026 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

foreach multiple vars may not be refaliases

2 participants

Comments