We don't bite newbies here... much | |
PerlMonks |
Re: Arrow Operator Question (Autovivification motivated)by LanX (Saint) |
on Mar 26, 2023 at 13:12 UTC ( [id://11151232]=note: print w/replies, xml ) | Need Help?? |
Others already pointed you to the "how", namely Perl's autovivification But it seems nobody explained "why". It's DWIM. If you try the same thing in JS you get an error. in the browser's console (F12)
Assigning to a deeply nested path is even more a PITA in those languages
You'll need a loop to build each level step by step. But Perl does DWIM by creating accessed levels on the fly.
Many paint this as bug, because it's different to other languages. I disagree, it's a case where you can't make an omelet without breaking an egg somewhere. And I profit from this far more often than I stumble over it. It should be be better explained and motivated though.
metaThere is no autovivification to disable this on demand I personally think a syntactic solution with a no-autovivification operator might have been better. Alas there are not many characters left in the alphabet, and I'm not sure such a syntax could work: $a->{b}:{c}{d} # should not vivify beneath b FWIW there is also Data::Diver (and other similar modules) on CPAN, which could also be core.
updateBTW: Contrary to the title, you don't need the arrow operator to see this effect.
updateI'd love to see other or even better motivations for "Autovivification" discussed. Cheers Rolf
In Section
Seekers of Perl Wisdom
|
|