Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Which Perl 5 Object System to Use?

by John M. Dlugosz (Monsignor)
on May 16, 2003 at 22:32 UTC ( #258779=perlquestion: print w/ replies, xml ) Need Help??
John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

There are a few ways to make objects in Perl 5, including pure conventions, conventions with some common helper code, modules to implement classes and/or objects, all the way up to huge meta-systems.

I'm sure there are a few more now than last time I looked.

So, which one to choose? How about a quick survey of the ones out there (what do you like?) and a summary of the benifits/tradeoffs of using that particular system?

—John

Comment on Which Perl 5 Object System to Use?
Re: Which Perl 5 Object System to Use?
by broquaint (Abbot) on May 16, 2003 at 23:45 UTC
    So, which one to choose?
    This really depends on the object/class system that you want to build. A simple blessed hash will do in a lot of cases where you simply want an object plain and simple, whereas if you want a more adaptable complex system then some of the Class:: and Object:: modules will be worth checking out. Personally I rely on a simple perl data structure until I need to do anything of significant complexity, then I start to use external modules and patterns. YMMV :)
    HTH

    _________
    broquaint

Re: Which Perl 5 Object System to Use?
by Abigail-II (Bishop) on May 17, 2003 at 00:03 UTC
    I like the advantages of strictness and I dislike global variables all living in the same package for normal code. I don't think OO code should be any different, so I prefer to use Inside Out Objects.

    Abigail

Re: Which Perl 5 Object System to Use?
by samtregar (Abbot) on May 17, 2003 at 04:18 UTC
    I like Class::MethodMaker. It's easy to use and it makes quick work of the most irritating part of doing OO, generating accessors and constructors. Class::Struct is nice too, but much more limited in scope and less configurable.

    I describe both modules as well as covering several do-it-yourself options in my book. But if you're really interested in learning about all the options you should read this one by Damian Conway.

    -sam

      Would anyone with experience of both Class::MethodMaker and Class::MakeMethods care to give a comparison?

      (I've read the pod and used MakeMethods - but I'm interested in people's preferences)

Re: Which Perl 5 Object System to Use?
by abell (Chaplain) on May 17, 2003 at 09:13 UTC

    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: perlquestion [id://258779]
Approved by BazB
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (16)
As of 2014-07-28 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (204 votes), past polls