New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can't use ternary on lhs of push to return array lvalue #14436
Comments
From @rjbsCreated by @rjbsThis works: ~$ perl -le '($ARGV[0] ? @a : @b) = (1,2,3); print "A<@a> B<@b>"' 1 Ternary as lvalue is well-established, and the source of my favorite warning: ~$ perl -le '($ARGV[0] ? $a : @b) = (1,2,3); print "A<@a> B<@b>"' 0 It used to be that you couldn't use a ternary as the first value to push for ~$ perl5.8.8 -e 'push(($x ? $y : $z), 1)' It doesn't matter whether y/z are both arrays, either. This changed in perl v5.14, with autoderef: ~$ perl -le '$y = []; $z = []; push(($x ? $y : $z), 1); print "Y<@$y> Z<@$z>"' ...but only for scalars. Kinda. ~$ perl -e 'push(($x ? @y : @z), 1)' Since the first argument is not a literal array, we use the "push EXPR, LIST" I don't think this is a bug... but! If autoderef ends up being removed the In the meantime, of course, there's this workaround: ~$ perl -le 'use experimental "postderef"; :-) Perl Info
|
From @rjbsI forgot to mention that the use of a constant in the ternary will get the array in place before anything untoward can happen, confusing the situation a bit: ~$ perl -le 'push((0 ? @a : @b), 1); print "A<@a> B<@b>"' -- |
From @demerphqYou can always do this: push @{ $cond ? $x : $y }, $foo; On 21 January 2015 at 17:28, Ricardo SIGNES via RT
-- |
The RT System itself - Status changed from 'new' to 'open' |
From @ikegamiOn Wed, Jan 21, 2015 at 11:24 AM, Ricardo SIGNES <perlbug-followup@perl.org>
You could with a little extra: $ perl -E'push @{ $x ? \@y : \@z }, 1; say "Y<@y> Z<@z>"' Would there remain a compelling reason for a ternary (and other
It's not being rejected. $ perl -E'push $x ? \@y : \@z, 1; say "Y<@y> Z<@z>"' |
From @rjbs* Eric Brine <ikegami@adaelis.com> [2015-01-21T13:40:02]
Yes, and in fact I showed that already in my ticket, although I used postfix
I meant a ternary that results in an array rather than a reference, of course. -- |
With push on reference forbidden, this leads to an error now:
|
This seems to contradict the documentation of the "Conditional Operator" which says
|
On Fri, 19 Jun 2020 at 10:55, E. Choroba ***@***.***> wrote:
With push on reference forbidden, this leads to an error now:
perl -wE 'push $x ? @A1 : @a2, @argv'
Experimental push on scalar is now forbidden at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
I would be surprised if it ever worked. Anytime I have seen something like
…--
perl -Mre=debug -e "/just|another|perl|hacker/"
|
Context only applies to valid code, and push syntactically expects an array, not an expression. This is documented as push ARRAY, LIST In more specific terms, that means push @NAME, LIST
push @BLOCK, LIST
push EXPR->@*, LIST (Also simplified forms of blocks like |
Since the |
This is deeply confusing. If an expression returning an array is not valid as first argument for push, could we then at least make this consistent and improve the error? When you compare
and
Isn't it a) a language bug that the compile-time constant expression passes the syntax check and does the expected pushing to one of the arrays, depending on condition, and At the very least, the behaviour between compile-time constant expression and runtime-evaluated expression should be consistent. |
On Sat, 20 Jun 2020 at 20:20, Thomas Orgis ***@***.***> wrote:
This is deeply confusing. If an expression returning an array is not valid
as first argument for push, could we then at least make this consistent and
improve the error? When you compare
$ perl -e 'push($a > 1 ? @x : @y, 3); print ***@***.*** | @y\n"'
Experimental push on scalar is now forbidden at -e line 1, near "3)"
Execution of -e aborted due to compilation errors.
and
$ perl -e 'push(0 > 1 ? @x : @y, 3); print ***@***.*** | @y\n"'
| 3
Isn't it
a) a language bug that the compile-time constant expression passes the
syntax check and does the expected pushing to one of the arrays, depending
on condition, and
b) at very misleading error message, as I did never intend to push on a
scalar. The expression clearly is supposed to return an array.
At the very least, the behaviour between compile-time constant expression
and runtime-evaluated expression should be consistent.
Yeah, agreed. Nice catch. I guess the expression gets folded away.
FWIW, I wouldnt be surprised if this is hard to fix.
Yves
|
Should I open a new issue about this, nevertheless, or should this one here be reopened? |
I think you should reopen with a new ticket so the title is clear.
Yves
…On Mon, 22 Jun 2020 at 10:58, Thomas Orgis ***@***.***> wrote:
Should I open a new issue about this, nevertheless, or should this one
here be reopened?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#14436 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAZ5R3X4QHRXTTPNKM4IALRX4MM5ANCNFSM4OCQ7M6A>
.
--
perl -Mre=debug -e "/just|another|perl|hacker/"
|
On 6/22/20 5:50 AM, Yves Orton wrote:
I think you should reopen with a new ticket so the title is clear.
Yves
Why not reopen this one, and edit the title?
… On Mon, 22 Jun 2020 at 10:58, Thomas Orgis ***@***.***> wrote:
> Should I open a new issue about this, nevertheless, or should this one
> here be reopened?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#14436 (comment)>, or
> unsubscribe
>
<https://github.com/notifications/unsubscribe-auth/AAAZ5R3X4QHRXTTPNKM4IALRX4MM5ANCNFSM4OCQ7M6A>
> .
>
--
perl -Mre=debug -e "/just|another|perl|hacker/"
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#14436 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAA2DH4WA4MYKWA42S4K7KDRX5ARTANCNFSM4OCQ7M6A>.
|
Well, there's #17883 now … |
Migrated from rt.perl.org#123648 (status was 'open')
Searchable as RT123648$
The text was updated successfully, but these errors were encountered: