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

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

Thread Previous | Thread Next
Aristotle Pagaltzis
October 19, 2016 09:59
Re: (\@a) = \($x,$y) in non-void context
Message ID:
* Dave Mitchell <> [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 <> [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.


* Dave Mitchell <> [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}).

Aristotle Pagaltzis // <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About