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        


In reply to Re: Object accessors with unknown data (hash) by tye
in thread Object accessors with unknown data by Amblikai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.