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

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

Thread Previous | Thread Next
From:
demerphq
Date:
July 4, 2015 16:09
Subject:
Re: extraneous autovivification (foreach (@$x), keys(%$x), ...)
Message ID:
CANgJU+U5XOohd01EER3Z398N+M42ie2LbXULhUaDd1hooq4XzA@mail.gmail.com
On 1 July 2015 at 00:02, Ricardo Signes <perl.p5p@rjbs.manxome.org> wrote:
> * Aristotle Pagaltzis <pagaltzis@gmx.de> [2015-06-30T17:16:14]
>> Maybe we cannot change that now. And if we cannot change it, we cannot
>> change it. But I wish we *knew* that, for having tried it – instead of
>> just forever wondering.
>>
>> And maybe we would instead find that we can.
>
> First, could we control this lexically so that we had a proper core version of
> "no autovivification" that didn't mean we have to pester Vincent every time we
> change the internals?
>
> Second, if so, we could at least look for how much CPAN would break.
>
> At first blush, I'm not sure I like to imagine that anyone is relying on this
> behavior, but of course it is possible that there are reasonable reasons to do
> so.  Somewhere.

I bet there is code that depends on it. I imagine such code would look
something like this:


foreach my $thing (@{$hashref->{$key}}) {
   whatever($thing);
}
my $ary= $hashref->{$key};
push @$ary, @more_things;


Also, note that we have syntax, of sorts, to say "dont autovivify" in
this kind of context:

foreach my $thing ( @{ $hashref->{$key} || [] } ) {
}

I bet this construct is pretty common, and could be optimised somehow
to not create a new anon array every time.

Yves

-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

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