perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

Ternary interpreted as experimental push

Open philiprbrenan opened this issue 1 year ago • 2 comments

In the line marked OK the choice of which array to use is controlled by a constant and all works as expected. But replacing the constant by a variable produces an unexpected message: Experimental push on scalar is now forbidden.

use v5.34;
use Data::Dump qw(dump);
use Test::More;

my @s1; my  @s2;
push ((1 ?  @s1 : @s2), 'AAAA');  # Ok - using constant
is_deeply  \@s1, ["AAAA"];
is_deeply  \@s2, [];

my @t1; my  @t2; my $t = 1;
push (($t ? @t1 : @t2), 'AAAA'); # Experimental push on scalar is now forbidden - using variable
is_deeply  \@t1, ["AAAA"];
is_deeply  \@t2, [];

philiprbrenan avatar Apr 02 '24 21:04 philiprbrenan

This looks like the inconsistency described in https://github.com/Perl/perl5/issues/17883 (preceded with more historical detail in https://github.com/Perl/perl5/issues/14436).

The current state of affairs is that some (de)referencing is needed to accomplish this:

push @{$t ? \@t1 : \@t2}, 'AAAA';

I'm unclear on whether resolving the inconsistency could result in the above also being prohibited, c.f. https://github.com/Perl/perl5/issues/14436#issuecomment-646996371

richardleach avatar Apr 02 '24 21:04 richardleach

By the way the reason the first version works is because a constant expression is replaced at parse time so it only sees the @s1 operand.

Grinnz avatar Apr 02 '24 22:04 Grinnz