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

Re: Object accessors with unknown data (hash)

by tye (Sage)
on Jan 26, 2015 at 14:25 UTC ( [id://1114527]=note: print w/replies, xml ) Need Help??


in reply to Object accessors with unknown data

Doesn't sound like you have anything that qualifies as a class. Sounds like you have a hash.

Accessor methods named after attributes are anti-modular. Using such for ad-hoc attributes provide zero benefit (to go along with the significant drawbacks).

Now, if you have special behavior that you want to implement, then it can make sense to write a class. But the ad-hoc key/value pairs are not the attributes of the objects. The attributes of the object are whatever pieces of data you need in order to implement the interface. In the simplest case, you build a class with a single attribute, a hash (ref).

Then the methods you build tend to provide the basic functions that Perl hashes provide:

my $val = $obj->get($key); my %copy = $obj->get(); my %parts = $obj->get(@keys); $obj->set( $key => $val ); $obj->set( %pairs );

Those methods can do extra things that just a hash wouldn't do (prevent write access, detect use of non-present keys).

More likely, you have other behaviors that lead to the need for at least one or two other attributes (which you probably shouldn't build accessors for).

- tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found