Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: OOP's setter/getter method - is there a way to avoid them?

by jeffa (Bishop)
on Oct 27, 2015 at 16:27 UTC ( #1146133=note: print w/replies, xml ) Need Help??


in reply to OOP's setter/getter method - is there a way to avoid them?

"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)

Replies are listed 'Best First'.
Re^2: OOP's setter/getter method - is there a way to avoid them?
by tiny_monk (Sexton) on Oct 28, 2015 at 05:59 UTC

    Jeffa, thank you for sharing your insight on the subject. I appreciate it. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1146133]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2019-05-27 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (155 votes). Check out past polls.

    Notices?