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

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

Thread Previous | Thread Next
From:
Christian Millour
Date:
June 30, 2015 18:47
Subject:
Re: extraneous autovivification (foreach (@$x), keys(%$x), ...)
Message ID:
5592E421.2010707@abtela.com
Le 30/06/2015 20:19, Leon Timmermans a écrit :
> On Tue, Jun 30, 2015 at 6:55 PM, Eric Brine <ikegami@adaelis.com
> <mailto: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.

is it the same accident of implementation that autovivifies intermediary 
levels when deep rooting in a data structure, even in pure rvalue 
context ? e.g.

$ perl -MData::Dump -E'dd $x; $y = $x->{a}[1]{b}; dd $x'
undef
{ a => [undef, {}] }
$ perl -MData::Dump -E'dd $x; $z = $x->{a}{b}{c}; dd $x'
undef
{ a => { b => {} } }
$

(arguably, the assignments above could set $y and $z to undef without 
autovivifying anything...)


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