Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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...

Replies are listed 'Best First'.
Re^3: Prevent direct acces to object's attributes
by FunkyMonk (Chancellor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://794002]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2018-06-19 13:13 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.