The blogger agrees that you should make your object's data available to the outside world in a way lets you change the underlying implemtation when you need to.

In Java, it seems like the "best practice" is to copy and paste a bunch of identical get/set methods.

The blogger seems to be against copying and pasting when native language features can give you the same benefit. With Python (apparently) you can use the built-in features to make public attributes that can be transparently upgraded from simple attributes to get/set methods, without changing the API:

contact.email = "x@x.com"

Behind the scenes this can be implemented by a simple attribute variable or a get/set method. The user of the object doesn't care.

This feature isn't built in to Perl 5. If you break encapsulation and access the underlying hash keys directly:

$contact->{'email'} = "x@x.com";

Then you can't protect the access without a tied hash. On the other hand, if you wrap the accessor in a method:

$contact->email("x@x.com");

Then you have to copy and paste multiple identical get/set methods.

The better solution is to let a module generate the methods for you. For instance, Attribute::Property (which looks excellent, BTW - thanks for pointing it out, wolv!), automatically makes methods that are assignable like hash elements:

# module code sub email : Property; # ... # user code $contact->email = "x@x.com";

When you need to upgrade 'email' to do some validation, you can change the accessor into a full sub:

# module code sub email { # do some validation or processing here } # ... # user code - API stays the same. $contact->email = "x@x.com";

Michael


In reply to Re^2: Modules for autogenerating accessor/mutator methods by magog
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.