|XP is just a number
OOP's setter/getter method - is there a way to avoid them?by tiny_monk (Sexton)
|on Oct 27, 2015 at 08:15 UTC
tiny_monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello, beloved Perl monks. My learning journey has taken me to a far and exciting land called Object-Oriented Programming (OOP). I find OOP interesting as it entails a different way of thinking and approach in programming. I am currently studying a book, Perl Objects, References & Modules (1993) by Randal L. Schwartz, and was introduced to the concepts of getters and setters. Personally, I do not feel comfortable with the idea of using setter and getter functions. I understood clearly the disadvantages of accessing class objects directly. If down the road I would need to design a class object, I would try to stay away, as possible, from using accessors. However, I am a bit frustrated because the book only illustrated how setters/getters could break the encapsulation of a class object. I made up my own example to illustrate how encapsulation could be broken into despite the success of changing the color attribute from 'red' to ‘green’.
I used Data::Dumper and the result is shown below:
The chapter I read, chapter 9, did not offer definitive and non-invasive solutions on updating an object’s attribute. On the other hand, It’s also possible that I have failed to understand some key points in the chapter. My question is, are setters and getters still used these days in Perl programming? Is there a non-invasive way to update an object’s attribute? 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? Thank you. :)