Hacker News new | ask | show | jobs
by SmirkingRevenge 2513 days ago
Autovivication seemed like a great feature to me, back in the day, when I was getting started with Perl. Working with similar data structures in languages like Python always seemed like a chore.

I had heard a few perl monks criticize it a bit, as an "end run around use strict", but I was kind of dismissive. It turns out, that's a really apt way to describe it. It basically tosses the niceties of use strict out the window, so long as its a hash. And sooo many things are hashes/hashrefs in Perl.

The first time I wasted a day tracking down a bug that turned out to be a well hidden typo in a hash key assignment, changed my mind. Well... maybe the third time.

2 comments

FWIW, you can 'lock down' a hash so that reads or writes of non-existing keys will cause a fatal error. Not only does this stop autovivification on 'writes' to a hash, but it will also help catch typos in your code just reading from the hash: e.g. if you accidentally wrote

  $foo = $bar{Autovivication}
instead of

  $foo = $bar{Autovivification}
in your program, it will abort instead of returning an undefined value :)

See Hash::Util for details.

I mention I am a linux sysadmin but I should have been clearer: I always use perl for <100 line scripts. Back when I started and in my peer group we made clear difference between programming and scripting. I never learned programming, I don't need to as that's not my job. I write small glue scripts and that's where perl shines and I see nothing else having any chance to replace it from what exists now.