Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Autovivification Oddity

by oiskuu (Hermit)
on Feb 11, 2018 at 15:07 UTC ( [id://1208959]=note: print w/replies, xml ) Need Help??


in reply to Autovivification Oddity

Not an answer to your question, but I'd like to point out a thing about dereferencing.

You can control (avoid) autovivification by taking a slice instead of plain dereference. One-element slices may raise a warning, though, so I guess this might count as a hack.

$ perl -wMstrict -MData::Dump -e' my $x = $#{$$a{list}}; dd $a' { list => [] } $ perl -wMstrict -MData::Dump -e' my $x = $#{@$a{list}}; dd $a' Can't use an undefined value as an ARRAY reference at -e line 1.

And to abuse it a little further: a slice in scalar context (imposed by dereference) yields its last value, so for instance

$ perl -MData::Dump -e' my %h = map {$_ => {v => "x$_"}} qw(a b c); dd + @h{qw(a b c)}->{v};' "xc"

Update. Forgot to mention: with recent enough perls that support postderef, one can change a straight deref into a slice simply by making an -> into ->@. Thus you may prevent autovivification at any level, e.g. $x->@{a}->@[5].

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1208959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found