Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Converting Hashes to Objects

by Corion (Pope)
on May 17, 2020 at 17:01 UTC ( #11116864=note: print w/replies, xml ) Need Help??


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 (Bishop) 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 (Bishop) 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 :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2020-09-26 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (141 votes). Check out past polls.

    Notices?