Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Making up your mind on an API lets you change the implementation later on without too much refactoring madness. If you only access and modify attributes through methods (choose any of setField/getField or set_field/get_field or field or whatever you consider best) you can decide at a later moment to change the implementation from hash to inside-out object without the need to check all the code using a class.

Recently I have been using a module of my own which works well enough for my needs and has let me learn a bit about dynamic function creation. If a class Hero needs scalar fields name, nick and strength and array fields friend and woe, then I declare it like this:

package Hero; use BTW::Class ( scalar => [qw( name nick strength )], array => [qw( friend woe )] ); ...
BTW::Class takes care of creating an "intermediate" package Hero::BTWClass, populating it with all accessor and mutator methods. Hero::BTWClass is appended to Hero's @ISA. There is a unique hash of hashes containing the structure of all objects, indexed by the numeric object reference. This keeps the actual objects "clean" as in inside-out objects. This way:
  • The object itself can be a reference of any kind;
  • Hero can override accessors and mutators and access the automatically generated ones via SUPER::addFriend, SUPER::setName and so on, if there is need for additional actions or checks while accessing or setting fields.

Cheers

Antonio Bellezza

The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket

In reply to Re: Which Perl 5 Object System to Use? by abell
in thread Which Perl 5 Object System to Use? by John M. Dlugosz

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2020-05-25 12:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If programming languages were movie genres, Perl would be:















      Results (145 votes). Check out past polls.

      Notices?