Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-03-28 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found