Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #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.


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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [choroba]: and I'm trying hard not to get involved in troll feeding.

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2017-04-26 12:26 GMT
    Find Nodes?
      Voting Booth?
      I'm a fool:

      Results (476 votes). Check out past polls.