Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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



In reply to Re: Re^2: characterstics of private in perl by demerphq
in thread characterstics of private in perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found