Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Converting Hashes to Objects

by Corion (Patriarch)
on May 17, 2020 at 17:01 UTC ( [id://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 (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 :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found