Beefy Boxes and Bandwidth Generously Provided by pair Networks
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 ( #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.


(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?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2022-01-19 02:43 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (55 votes). Check out past polls.