kulls has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,
I recently found Class::Accessor module in Plucene . Can anyone please tell me the difference between Class::Accessor and Class::MethodMaker ?. When I can/can't use Class :: ( Accessor/Methodmaker ) ??
Thanks,
-kulls

Replies are listed 'Best First'.
Re: Class::MethodMaker and Class::Accessor
by jbert (Priest) on Jan 02, 2007 at 10:43 UTC
    The docs for Class::Accessor contain this nugget:

    "What makes this module special compared to all the other method generating modules ("SEE ALSO")? By overriding the get() and set() methods you can alter the behavior of the accessors class-wide. Also, the accessors are implemented as closures which should cost a bit less memory than most other solutions which generate a new method for each accessor."

    With Class::MethodMaker being listed in the SEE ALSO section.

    As to which is best, or, more generally, which is currently the "best" way to do OO in modern perl5, I don't have a good answer, although it has been discussed a bit on perlmonks. Personally, I probably tend to use Class::Accessor, simply because it is the one I picked up first.

    I think this is a case where there is a bit too much TIMTOWDI, but I don't know how to best resolve it.*

    * - of course, "pick one and stick with it" or "do a survey of all possible ones and choose that" or "ask for help on perlmonks and take the answer people agree most on" are all working strategies, but the latter doesn't scale too well. But I think that the main problem (maybe I'm wrong and it isn't a problem) is for people coming from other languages. They want to do OO, but the un-aided ways are a bit clunky and choosing one of the aided ways can be confusing.

Re: Class::MethodMaker and Class::Accessor
by duckyd (Hermit) on Jan 02, 2007 at 20:28 UTC
    Class::Accessor can only create simple get/set methods. Class::MethodMaker offers a bunch of different types of methods that can be created. For example, some of the "method types" supported by Class::MethodMaker are "list", "hash", "hash_of_lists", "boolean", and "counter". These are intended to be more convenient for certain tasks than simple scalar get/set methods. For example, a "count" method has an accessor method, but also an _incr method (to increment) and an _reset method to reset the value. This avoids needing to do things like $foo->counter( $foo->counter++ ) to increment the value in a get/set.