Hello, Laurent_R. You are right. The author of the book did not explicitly write about the inherent dangers of using accessors. Nor did I claim that he did. I was merely speaking from my viewpoint that ensued after reading chapter 9. It was merely my own abstraction. It was more personal than universal. I apologize for not being able to communicate clearly. I am new to OOP. The moment I saw $self->{'attribute'} = shift; being used in the setter and $self->{'attribute'} in the getter functions, it gave me the notion that the encapsulation was being broken. But I may be wrong. My understanding is that the object's innards should stay inside the object. That's my understanding of what Randal wrote in his book. Then again I may be wrong. I was hoping to find clarity as to whether it is perfectly legal to access the object's data directly inside the setter/getter function. While I understand that it is not advised that an API caller use $object->{'attribute'} to access or update the object's attribute outside a class, I don't understand why it seems perfectly alright for the setter and getter methods to use $self->{'attribute'} for changing and accessing attributes internally? Why? This is what Randal did not explain in this particular chapter. Does the concept of encapsulation apply to the code found in the setter and getter functions? If it is so, isn't $self->{'attribute'} = shift; a violation of the encapsulation? Or is this a privilege that setter and getter functions have over API callers?


In reply to Re^2: OOP's setter/getter method - is there a way to avoid them? by tiny_monk
in thread OOP's setter/getter method - is there a way to avoid them? by tiny_monk

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.