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

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

Thread Previous | Thread Next
From:
David Nicol
Date:
October 19, 2016 08:52
Subject:
Re: (\@a) = \($x,$y) in non-void context
Message ID:
CAFwScO92WheH7U74pzHhEJ7BeXSunyULPF+L4ikgVw2w3+B8vA@mail.gmail.com
On Wed, Oct 19, 2016 at 3:13 AM, Dave Mitchell <davem@iabyn.com> wrote:
>
> 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 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".


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.

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