Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Re^2: characterstics of private in perl

by demerphq (Chancellor)
on Mar 09, 2003 at 15:21 UTC ( #241568=note: print w/ replies, xml ) Need Help??


in reply to Re^2: characterstics of private in perl
in thread characterstics of private in perl

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.

Well, my understanding of the above is that with sufficient trickery the data isnt really private. But I also agree that they are more or less equivelently private as compared to other things. I just dont agree that there is really a need for them in most cases.

The questions is very relevant. Good encapsulation is necessary to produce solid libaries without hidden dependencies.

The way I see it the question "How do I do private object attributes" is not the same as "How to I avoid object attribute namespace clashes in inheritance". I would say that the first is just one answer to the second, the one that in many ways is the simplest to implement and may have certain interesting properties, but not the only nor necessarily the best answer by any means.

Now, what are the options:

Well I can think of bunch of options besides closures/inside out objects. The most immediate strategy I would take would be to use a single, package specific key, then put all my attributes under that. If one day the original object was changed to have a key for its own use with the name of my package, well then I would be suprised.

To me the point is this, if an author wishes to create a class that is intended to be used as a baseclass he had better take care to organize the object in such a way that this isnt a problem. This could be something like this code

#!perl -l use strict; package Base; sub package_attrs : lvalue { my $self=shift; my $caller=shift || caller or die "For use by subclasses only\n"; $self->{$caller} } sub new { my $class=shift; my $self=bless {},$class; $self->init(@_); return $self; } sub init { my $self=shift; my %params=@_; $self->package_attrs=\%params; return $self; } package Base::Sub; our @ISA=qw(Base); sub init { my $self=shift; my ($x,$y,$z)=(shift,shift,shift); $self->package_attrs={foo=>$x,bar=>$y,baz=>$z}; $self->SUPER::init(@_); return $self; } sub foo { my $self=shift; my $attrs=$self->package_attrs; return @_ ? (($attrs->{foo}=shift),$self) : $attrs->{foo} } package Base::Sub::Woofer; our @ISA=qw(Base::Sub); sub init { my $self=shift; my ($x,$y,$z)=(shift,shift,shift); $self->package_attrs={foo=>$x,bar=>$y,baz=>$z}; $self->SUPER::init(@_); return $self; } sub bar { my $self=shift; my $attrs=$self->package_attrs; return @_ ? (($attrs->{bar}=shift),$self) : $attrs->{bar} } package main; use Data::Dumper; my $obj1=Base->new(foo=>"Delicious"); my $obj2=Base::Sub->new(1,2,3,foo=>"Delicious"); my $obj3=Base::Sub::Woofer->new(10,20,30,1,2,3,foo=>"Delicious"); $obj3->package_attrs="foo"; print Dumper($obj1,$obj2,$obj3);

We have now have more or less addressed the real problem that you wanted solved, and without introducing privacy. We can call package_attrs with a package name to twiddle their attributes if we want to, and we probably won't do so accidentally.

I agree that in some ways its too bad that there isn't a clearly defined framework that we can all be comfortable within, and its good that Perl6 will address that. I just dont agree that there is no other way than introducing purely private variables.

I agree that designing an OO framework with heavy use of inheritance in mind requires a bit of effort. But usually you only have to do it for the base class and then its not that much effort to use.


---
demerphq



Comment on Re: Re^2: characterstics of private in perl
Select or Download Code
Re^4: characterstics of private in perl
by adrianh (Chancellor) on Mar 09, 2003 at 23:59 UTC
    Well, my understanding of the above is that with sufficient trickery the data isnt really private. But I also agree that they are more or less equivelently private as compared to other things.

    I agree - what I was trying to say that the C++ "private" variables are not really private either. We can always just treat the object as a sequence of bytes and get at them that way.

    The way I see it the question "How do I do private object attributes" is not the same as "How to I avoid object attribute namespace clashes in inheritance". I would say that the first is just one answer to the second, the one that in many ways is the simplest to implement and may have certain interesting properties, but not the only nor necessarily the best answer by any means.

    I agree they are not the only answer - but they are an answer. A nice simple answer in my opinion. I was providing one example of where private object attributes would be useful in perl - which was your request ;-)

    With the exception of inside-out objects and closures the problem with other solutions, including package specific keys, is that they rely on the sub-class writer being aware of the mechanism for you to be completely safe. For me "more or less" and "probably won't" isn't good enough.

    That probably says something very sad about my personality :-)

    As you say, you can get around the problems with a bit of effort. However, the effort is enough that people get it wrong. I resent having to code stuff that the language should take care of for me. This stuff should be simple. Roll on perl6!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2014-07-26 15:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls