in reply to May Thy Closures Be Blessed

I've always been big on the "shotgun quote" not because of the idea that enforcement in not neccessary, but that writing accessors and mutators ad nauseum is silly

... i.e, by convention a blessed hash with internal data should only be accessed externally by class methods, and if the functions don't exist, it's at-your-own-risk. Internal hash-access, thereby being unacceptable/against-convention/etc ...

Bottom line -- everything is private if you agree to stay out of the living room, and that's a great design point. Once you start saying "I have a shotgun, but you can come in if", you start walking down the road of non-OO-OO, aka java-esque accessor/mutator hell.

I suspect there are a lot of yet undiscovered uses for this technique that will only be revealed with time and community acceptance. If nothing else, it will shutup the Java people claiming that Perl OO has poor encapsulation.

As much as I hate Java, I won't defend a failing in Perl when I see it. Perl does have horrible encapsulation, primiarly because of what you had to write to achieve it. Perhaps if stuff like this was in the core module distribution, a la "use ObjectVoodoo" or somesuch, we could argue the language did it well, but as it stands, the language does it poorly, and it becomes the role of the coder to fix the language ... hence (at least) Java speaks it more fluently. Usually java coders get it all wrong though -- and by no means is java a shining example of OO.

Replies are listed 'Best First'.
Re: Re: May Thy Closures Be Blessed
by diotalevi (Canon) on Apr 26, 2004 at 17:43 UTC

    I am fully in support of this type of encapsulation. Programmers ought not to be fiddling with an object's member variables without being invited first. Programmers doing that deserve the heck they get from it. I do it sometimes because the implementation on CPAN wasn't precient enough to suit my needs so a mutation is needed. I also do it because it is fun.

    I don't do it in places that matter. (well maybe except LWP::Simple::Cookies, LWP::Simple::Post, and B::Deobfuscate but those are exceptions and well enough defined exception areas.

Re: May Thy Closures Be Blessed
by Abigail-II (Bishop) on Apr 27, 2004 at 08:13 UTC
    Perl does not enforce private and public parts of its modules as you may have been used to in other languages like C++, Ada, or Modula-17. Perl doesn't have an infatuation with enforced privacy. It would prefer that you stayed out of its living room because you weren't invited, not because it has a shotgun.
    I like the shotgun quote. I fully support it. That's why I hate Perl's OO system. Do you remember where the shotgun quote is? It's not in the manual about objects - it's in the manual about modules. Modules all have their own namespaces - the living rooms. With objects however, this is different. A superclass might not invite the subclass in its living room, but it's as much the living room of the subclass as it is the superclass'.