Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Prevent direct acces to object's attributes

by vitoco (Friar)
on Sep 07, 2009 at 18:19 UTC ( #794002=note: print w/ replies, xml ) Need Help??


in reply to Re: Prevent direct acces to object's attributes
in thread Prevent direct acces to object's attributes

Why do you want to prevent access to object values using $obj->{...}? Maybe you want to educate your project members to not do that?

Reading directly from attributes is not a real problem, but writing is, because there are some interdependencies between attributes: some of them are preprocessed values based on other ones (like a cache), and changing them would produce unexpected behavior on some methods.

Of course, that is mentioned in the POD, but then I thought I missed something from the docs.

I'm new in writing OO code... Now, Moose and InsideOut objects are in my queue for further reading, just after Damian Conway's book!

Thanks to everyone...


Comment on Re^2: Prevent direct acces to object's attributes
Download Code
Re^3: Prevent direct acces to object's attributes
by FunkyMonk (Canon) on Sep 07, 2009 at 22:12 UTC
    You could have a look at the lock functions in Hash::Util. It's a core module since 5.8, so there's probably nothing to install.

    But, I'm sure I've read somewhere that their use is discouraged in objects. Can any Wise Old Monks shed any light on that?

    Just Use Moose. You'll love it.

      You cannot (re)bless a hash that is locked. Which can hinder certain OO programming techniques. Furthermore, locking a hash doesn't prevent accessing the attributes. 'lock_keys' prevents keys to be added 'lock_value' makes values readonly, and 'lock_hash' makes the entire hash readonly. But keys being readonly still makes them readable. And if you have a reference to the hash (which traditional Perl objects give you), nothing stops you from calling any of the unlock function on them.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-10-22 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls