Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Which Perl 5 Object System to Use?

by abell (Chaplain)
on May 17, 2003 at 09:13 UTC ( #258846=note: print w/replies, xml ) Need Help??


in reply to Which Perl 5 Object System to Use?

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2020-05-27 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (152 votes). Check out past polls.

    Notices?