Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) 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. :-)

Okay :-)

Consider a generic module LazyObject for lazy loading. A naive implementation might include a method like this:

sub load { my $self = shift; return unless $self->{_loaded}; # load lazy object $self->{_loaded}=1; };

Consider something that inherits from LazyObject:

package Weapon; use base qw(LazyObject); # add ammunition to weapon sub load { my ($self, $ammo); $self->{_loaded} = $ammo; }; sub can_fire { $self->{_loaded} && $self->{_working}; };

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:

  • As the author of Weapon I can go read the source for LazyObject, figure out that there is a naming clash and change the names of the Weapon attributes. A pain the ass, and I open myself to possible future problems if LazyObject changes the names of it's private methods/attributes to match mine.
  • As the author of LazyObject I can document my private methods/attributes so users of the module can avoid them. This somewhat defeats the object of using private methods/attributes - and there is still the problem of adding new private methods/attributes in a later version breaking the code of people who inherit from LazyObject.

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.


In reply to Re^2: characterstics of private in perl by adrianh
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (13)
    As of 2014-08-20 15:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (118 votes), past polls