Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

"My question is, are setters and getters still used these days in Perl programming?"

• Yes they very much are. This is why packages like Moose easily make them available for you.

"Is there a non-invasive way to update an object’s attribute?"

• From inside the object? Sure, as long as those methods are neither the getter nor the setter? :) You see ... something has to get the attribute on behalf of the client, and something has to set the attribute on behalf of the client.

'Is OOP considered a universal subject that it could be discussed without any reference to a particular programming language? Are there books that you could recommend on the subject?"

• Yes, very much so, although you'll see many examples in C++ and Java. My favorite book on the subject is "Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma, Helm, Johnson and Vlissides.

"However, I am a bit frustrated because the book only illustrated how setters/getters could break the encapsulation of a class object."

• Just remember that getters and setters are use to enforce encapsulation, not break it. In Perl, an object is most often implemented as a hash, and it is blessed to a package so that the interpreter can find its methods. If you don't care about those methods, then just use a hash. A hash is a key/value store of attributes, which can in turn be treated as a light weight object. No getters/setters needed ... and since you don't need to worry about internal representation of bytes 9 times out of 10 in Perl, you don't even need to worry about such encapsulation, although you may still benefit from its organization. Encapsulation is essentially used for 2 reasons: to hide away internal representations of bytes (double vs long etc.) and to keep your code organized. Along the lines of organization are working with large teams where you don't want some team relying on the internals of your code if you will making changes to it -- if they can reliably use an unchanging interface, this can prevent a breakdown of communication among large teams.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: OOP's setter/getter method - is there a way to avoid them? by jeffa
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-25 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found