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 16:10
Subject:
Re: (\@a) = \($x,$y) in non-void context
Message ID:
20161019161027.GB79292@plasmasturm.org
* Dave Mitchell <davem@iabyn.com> [2016-10-19 13:12]:
> > 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.
>
> No, you have a typo: my($r2,$r2) rather than $r1. With that changed,
> it prints 'rhs' when changing ($l1,$l2) to (undef,undef).

Ah. My bad.

> I can see 4 ways this could be handled.
>
> 1. (...,undef,...) = (...) autovififies a new undefined scalar value
> then assigns the RHS value to it, returning the new scalar;
>
> 2. it skips the assignment for that slot, and returns the RHS element;
>
> 3. it skips the assignment for that slot, and returns &PL_sv_undef
> (which will croak if later modified);
>
> 4. it skips the assignment for that slot, and returns a fresh undef
> scalar.
>
> (2) is the current behaviour. I'd like to change it to (3).

Do you have a specific reason other than that it seems that it “should”
be that way? I am extremely leery of this particular option as it would
cause code to crash that has run fine so far. I don’t know how much code
would be affected, but it feels potentially significant. Do we get any
benefits out of the breakage other than conceptual purity?

> (1) and (4) seem inefficient, having to create a new mortal scalar.

Sure. I thought that’s what’s going on already but since it isn’t, no
need to go in an inefficient direction.

> I think (2) is definitely semantically incorrect: the essence of list
> assignment is that it copies values: you shouldn't end up with aliases
> to RHS elements.

I’m not convinced. Perl has lots of little exceptions to such axiomatic
rules when it serves the conciseness of code. The very fact that you can
assign to `undef` is already a violation of your principle. I wouldn’t
dismiss the fact that it returns the RHS element as a mere accident with
no deliberation behind it.

The inconsistency you originally asked about regarding the return value
of list assignment in list context seems much more clearly like a bug,
since it’s contradictory both itself and the documentation.

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