Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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

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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2021-09-21 15:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found