|Just another Perl shrine|
Re: OOP's setter/getter method - is there a way to avoid them?by jeffa (Bishop)
|on Oct 27, 2015 at 16:27 UTC||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.
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)