|more useful options|
Re^2: characterstics of private in perlby adrianh (Chancellor)
|on Mar 09, 2003 at 12:03 UTC||Need Help??|
The short answer is no. The long answer is to read TheDamians book, and trawl the archives for the appropriate terms, but basically it comes down to, "you can make it tremendously difficult, but can never absolutely prevent it". (In fact I might argue that the same is true in C++)
I would say the short answer is yes, and the long answer is the same. Closures and inside-out objects are just as "private" as C++ private vars.
But im betting that the answer doesn't matter, as the question is probably not relevent.
The questions is very relevant. Good encapsulation is necessary to produce solid libaries without hidden dependencies.
Peeps, please dont reply with a bunch of examples of where private data is so useful or required in other languages. If you have a perl example fine. :-)
Consider a generic module LazyObject for lazy loading. A naive implementation might include a method like this:
Consider something that inherits from LazyObject:
Oops. Clash of private object attributes. If you are the author of both modules, it is an easy fix. If you are not - if LazyObject is something from CPAN intended to be generic - it's harder.
Now, what are the options:
The ability to have private attributes make the problem disappear. Both module authors can have whatever private variables they consider necessary without having to worry about each other.
The issue is encapsulation not data-hiding.
The "shotgun" analogy is a false one in many cases. The problem here is not deliberate invasion, it's accidental trespass. As the author of LazyObject I don't care if an author deliberately goes in and messes with _loaded. The problem is with somebody accidentally overriding it because they don't know it's been used. The fact that I can break other peoples code by adding another private attribute to a new version of LazyObject is terrible!
This kind of mess makes writing bullet proof modules for reuse much harder than it needs to be. Personally, I don't think "reading the source" is a solution. I shouldn't have to read the source of another module to get my one to work. That is what encapsulation and abstraction is all about :-)
Now, there are many ways around this (closures and inside-out objects for example). However, they all involve extra work. So I am really looking forward to perl6 and decent encapsulation.