develooper Front page | perl.perl5.porters | Postings from June 2015

Re: extraneous autovivification (foreach (@$x), keys(%$x), ...)

Thread Previous
From:
Paul "LeoNerd" Evans
Date:
June 30, 2015 18:29
Subject:
Re: extraneous autovivification (foreach (@$x), keys(%$x), ...)
Message ID:
20150630192844.58e3250c@shy.leonerd.org.uk
On Tue, 30 Jun 2015 20:19:33 +0200
Leon Timmermans <fawaka@gmail.com> wrote:

> On Tue, Jun 30, 2015 at 6:55 PM, Eric Brine <ikegami@adaelis.com>
> wrote:
> 
> > The difference between foreach and push is that foreach imposes an
> > lvalue context on its list and push does not.
> >
> 
> > foreach needs to do this so you can do << $_ = uc($_) for @names;
> > >>. << sub foo { } foo(@$x) >> will similarly evaluate @$x as an
> > >>lvalue context,
> > autovivifying $x.
> >
> > I realize this doesn't answer your question.
> >
> 
> foreach needs force lvalue context to the elements, inheriting the
> lvalueness to the underlying arrayref is an accident of
> implementation IMO. Not sure how extensive the backwards
> compatibility consequences would be of changing this though.
> 
> Leon

At least half the time I use foreach, I find that lvalueness to be
actively annoying; meaning I could leak mutations of the loop
topicaliser into the array storage.

I wonder if there'd be scope for something like

  foreach my $item :readonly ( @array ) {
  }

Which then explicitly says /not/ to create an lvalue. Given as lvalues
for things like keys(%$ref) aren't going to work anyway, that would
possibly help here.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk
http://www.leonerd.org.uk/  |  https://metacpan.org/author/PEVANS

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About