http://qs321.pair.com?node_id=11116864


in reply to Converting Hashes to Objects

If you're not tied to creating objects, I found locking hashes quite good to prevent typos:

use Hash::Util 'lock_keys'; my $hash = h2o { foo => "bar", x => "y" }, qw/ more keys /; lock_keys $hash; print $hash->{ foo }, "\n"; # prints "bar" $hash->{ x } = "z"; # setter $hash->{batman} = 'secret'; # dies

Replies are listed 'Best First'.
Re^2: Converting Hashes to Objects
by haukex (Archbishop) on May 17, 2020 at 17:06 UTC

    Definitely, and I actually use that a fair amount, and Hash::Util's functions were part of the inspiration for this. The advantage of this module is that it saves you two characters ;-) (they're a little slower to type on a German keyboard too...) One of the disadvantages is that method calls don't interpolate into strings.

    (Hmmm, maybe I could to add a -lock option to additionally lock the hashref's keys, so the hash can be used safely in both ways... Update: Done!)

Re^2: Converting Hashes to Objects
by haukex (Archbishop) on May 17, 2020 at 21:07 UTC
    I found locking hashes quite good to prevent typos

    Thank you very much for the inspiration, I just released v0.06 that locks the hash's keyset by default :-)