develooper Front page | perl.perl5.porters | Postings from January 2017

Re: [perl #4514] two bugs: substitution on a list

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
January 3, 2017 14:50
Subject:
Re: [perl #4514] two bugs: substitution on a list
Message ID:
20170103145014.GV3096@iabyn.com
On Sat, Sep 07, 2013 at 10:25:24PM -0700, Father Chrysostomos via RT wrote:
> On Sat Sep 07 02:21:21 2013, davem wrote:
> > On Fri, Sep 06, 2013 at 07:24:44PM -0700, James E Keenan via RT wrote:
> > > > On Wed Oct 25 08:41:02 2000, hv@crypt.compulink.co.uk wrote:
> > > > > (After a report in clp.moderated from Peter Scott <peter@psdt.com>.)
> > > > > 
> > > > > perl -Dr -we '(($y) = "x") =~ s/a/b/'
> > > > > 
> > > > > shows that a) perl is trying to perform the substitution on the
> string
> > > > > '1' which is not what the caller intended, and b) '1' is not marked
> > > > > readonly in this context, so no error is reported. I think both of
> > > > > these are bugs: I think the former should be fixed by giving an
> error
> > > > > if the LHS of a binding operator is supplied as a list, and that the
> > > > > latter should be fixed (if still relevant) by marking the count of
> > > > > list elements as readonly.
> > 
> > I don't think either of these are bugs.
> > 
> > In the first instance, its a list assignment evaluated in scalar context,
> > which is well-defined as returning the number of elements assigned.
> > The result may be slightly unexpected to the uninitiated, but its no
> > different than say
> > 
> >     $count = (($y) = "x");
> > 
> > which sets count to 1.
> > 
> > In the second instance, (($y) = "x") in scalar context is an expression
> > that returns a well-defined numeric value. I see no rationale for it
> > to be read-only, any more than any other expression should return a
> > read-only value.
> 
> Hmm, but should aassign be allowed as a scalar lvalue?
> 
> We also allow:
> 
> read STDIN, @a=qw(foo bar), 10
> 
> Should we?
> 
> (I wrote earlier that I agreed.  Now I’m not sure.  Reopening....)

I'm now more inclined to think that it should be a compile-time error.

c.f.

    my $p = 10;
    my $q = 20;
    my $x = (($p + $q) =~ s/3/4/);

which gives

    Can't modify addition (+) in substitution (s///) at /home/davem/tmp/p line 7, near "s/3/4/)"

(while changing it to s/3/4/r is legal and sets $x to 40).


-- 
I don't want to achieve immortality through my work...
I want to achieve it through not dying.
    -- Woody Allen

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