"Hah! You're using accessors now!" Actually, I'm not. An accessor exposes an attribute.

What do you mean by "exposing"? So, if you have an implementation that uses the radius as the internal attribute and add a "set_radius" method that just does something like $self->{radius} = shift, you are using an Evil Accessor, but if you have the area as the internal attribute and add a set_radius that does $self->{area} = $PI * shift**2, you have an smart and innocent method? The interface is identical and the user shouldn't know the difference, therefore there's no way of telling, from the outside, whether a method is an accessor or not. Which leads me to the conclusion that all the ranting I hear about accessor methods is nonsense.

Now, what is a true problem is when accessor methods let the user put the object in an inconsistent state. But I call that a plain bug, not accessor evil. For example, let's say that the object stores both the radius and the area (to save recomputing the area, which as we know, is extremely expensive ;-). Then it would be bad to have a set_radius method that only does $self->{radius} = shift, because the area would be left with the wrong value. The set_radius method should be ammended to recompute the area as well.


In reply to Re: Perl OO and accessors by itub
in thread Perl OO and accessors by dragonchild

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.