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 09:59
Subject:
Re: (\@a) = \($x,$y) in non-void context
Message ID:
20161019095855.GA77494@plasmasturm.org
* Dave Mitchell <davem@iabyn.com> [2016-10-19 10:24]:
> There's one further issue I've noticed: undef on the LHS:
>
>     sub inc { $_++ for @_ }
>
>     inc($a, undef             ); # "Modification of a read-only value attempted"
>     inc(($a, undef) = ($x, $y)); # increments $a and $y

I think that’s correct.

* David Nicol <davidnicol@gmail.com> [2016-10-19 11:00]:
> Undef appears to be special in that it gets replaced with an alias to
> the RHS element instead of throwing a compile-time error.
>
> $ perl -le  '$x=$y=1; ($a,undef) = ($x,$y) ; print "a:$a x:$x y:$y"'
> a:1 x:1 y:1
> $ perl -le  '$x=$y=1; ($a,2) = ($x,$y) ; print "a:$a x:$x y:$y"'
> Can't modify constant item in list assignment at -e line 1, near ") ;"
> Execution of -e aborted due to compilation errors.

Correct that it’s special, but wrong guess as to why.

    sub x { join ":", \(@_) }
    my $lhs = x my($l1,$l2);
    my $rhs = x my($r2,$r2);
    my $ret = x(($l1,$l2)=($r1,$r2));
    say "lhs" if $lhs eq $ret;
    say "rhs" if $rhs eq $ret'

This says “lhs”. It does not say “rhs” if you change the LHS of the
assignment to (undef,undef) – which would be what it should say if such
an assignment to undef were returning an alias to the RHS element.

Therefore,

* Dave Mitchell <davem@iabyn.com> [2016-10-19 10:24]:
> I think that second call to inc should pass ($a, undef) rather than
> ($a, $y) to inc, and should also die with "Modification of a read-only
> value attempted".

Apparently a literal `undef` on the LHS is special-cased to autovivify
an anonymous scalar.

Which is a scalar that gets assigned to, which is what list assignment
should return – so the current behaviour is correct.

Oddly, that seems to mean (@a,undef)=1 must return ($a[0],do{my$anon}).

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