Accessor generation is very handy when you've got a 'big' object model sketched out - the nouns of your application - and you'd like to flesh out that model quickly in Perl, so you can get on to how those objects interact. It's also good coding practice to reduce redundant code, and accessor/mutator generation helps quite a bit there.

I've used Class::Accessor several times, and it's pretty easy-going. It's very good for cases where you have many classes to implement, and they're all fairly similar (in the since that they are all composites of each other or simple scalar datatypes). It's easy to have validating mutators. Class::Accessor is also nice about not getting in your way; if you want, you can use it to generate only some of your methods, and hand-code the "interesting" ones yourself.

Class::Accessor can also be used on-the-fly (at runtime) to add new methods, though most people won't need such a fancy trick. Just call $obj->mk_accessor() and you can add bells and whistles as needed. But most object models don't need to go around redefining their schemas.

Class::MethodMaker, Class::MakeMethods, and many other approaches want to be compile-time only, which might be "safer", or at least "saner". In other words, you define your classes in like this (varies a bit):

use Class::MyFavoriteMethodGenerator qw( YOUR-CLASS-DEF-HERE );

where YOUR-CLASS-DEF-HERE is usually a hash of attribute names and options (read-only, etc).

Class::MakeMethods is much more ambitious about generating more complex methods for you, while trying to keep your class definition code as simple as possible. Try it, and see if you like it.


In reply to Re: Modules for autogenerating accessor/mutator methods by other90percent
in thread Modules for autogenerating accessor/mutator methods by srdst13

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, details, 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, summary, 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.