Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.85k stars 524 forks source link

Ternary interpreted as experimental push #22117

Open philiprbrenan opened 4 weeks ago

philiprbrenan commented 4 weeks ago

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, [];
richardleach commented 4 weeks ago

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

Grinnz commented 4 weeks ago

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.