develooper Front page | perl.perl5.porters | Postings from October 2016

Re: (\@a) = \($x,$y) in non-void context

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
October 19, 2016 02:23
Subject:
Re: (\@a) = \($x,$y) in non-void context
Message ID:
20161019022251.GA74765@plasmasturm.org
* Dave Mitchell <davem@iabyn.com> [2016-10-17 10:24]:
> I've just spotted another issue with list assignment in list context.
> Does anyone think the third case below is a bug? I think it should
> output "1:u" like the second case:
>
>     sub f { print join(':', map $_ // "u", @_), "\n"; }
>
>     f(($a,$b)     = () ); # prints "u:u"
>     f(($a,$b)     = (1)); # prints "1:u"
>     f(($a,$b, %h) = (1)); # prints "1"
>
> on the principle that the return value of a list assignment is its
> LHS; i.e. that @a = ((X) = (Y)) should be equivalent to (X) = (Y); @a
> = (X), where (X) is a list of lvals, like ($a, undef, %h)

Hmm. Probably.

There appears to be only one single mention of what list assignment in
list context does across the entire documentation, in perlop.pod:

    Similarly, a list assignment in list context produces the list of
    lvalues assigned to, and a list assignment in scalar context returns
    the number of elements produced by the expression on the right hand
    side of the assignment.

This reads to me like

    ( $foo, $bar, @baz ) = 1..5

is supposed to return the same lvalues as this expression:

    ( $foo, $bar, $baz[0], $baz[1], $baz[2] )

Question is, what ought this return?

    ( @foo, $bar, @baz ) = 1..3

$foo will be undef after this assignment, so clearly it is “assigned to”
per the documentation text? And @baz will be cleared as well, however no
scalar inside it will be assigned to as such. Thus, the list of lvalues
returned must be this:

    ( $foo[0], $foo[1], $foo[2], $bar )

Right? But that seems odd to me too. It is consistent, but is it really
the intent?

I think we need to nail down what the behaviour is supposed to be before
we decide what exactly is a bug or not… and we also ought to have tests
for this that cover all the cases.

Larry: I know you’re fun elsewhere these days :-)  but given the paucity
of docs and lack of tests it would be very helpful to have your input on
this issue.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About