Is there a way of privatizing variables/member functions of a package

The only way I know of is through lexical variables. You can use them to create closures that only the enclosed blocks have access to. There are ways to get around this protection, but they're not pretty or for the faint of heart. Here's the basic example:

package Example::Module; use strict; { # this variable is not accessible outside of the block # that it's in. thus, any access to it must go through # the subroutines defined in this block. my $variable; sub accumulate { $variable += $_ for @_ } sub printvalue { print $variable, $/ } } 1;

You could use that package like so:

use Example::Module; Example::Module::accumulate(5,6,7); Example::Module::printvalue;

Using anonymous coderefs, you can get the same protection for subroutines:

package Example::Module; use strict; { my $value; # this lexical variable stores a subroutine that is only # accessible from within this block. my $add = sub { $value += $_[0] }; sub accumulate { $add->($_) for @_ } sub printvalue { print $value, $/ } } 1;

Now, a problem comes in when you want to use these techniques with objects. Converted to an object interface, these lexical variables would be persistent across all instances of the object. Trying to use them for instance data would obviously be a problem. Thus, you have to keep track of the instances yourself.

One way to do this is to make each data member a hash that is keyed on the object instance's reference ID. You have to be careful to remove these instance variables when the object is destroyed, though, because the hash is persistent throughout the program's lifetime. Note, this is an approach that Abigail-II developed, called inside-out objects. Here's a very minimal example:

package Example::Module; use strict; # note, we don't actually have to use the reference that we # bless. we just need it for its unique reference id. sub new { bless {}, shift } { my %value; my %counter; sub accumulate { my $self = shift; for (@_) { $value{ $self } += $_; $counter{ $self }++; } } sub printvalue { my $self = shift; printf "%s has value %2d from %2d iterations.\n", $self, $value{$self}, $counter{$self}; } # prevent memory leaks. DESTROY { my $self = shift; delete $value{$self}; delete $counter{$self}; } } 1;

Of course, it's neat that you can get this protection if you really need it. But most of the time, my experience shows that you don't really need it. Most of the community simply relies on politeness and common conventions, such as using _ in the beginning of private method names.


In reply to Re^5: Writing Modules/namespace polution by Mugatu
in thread Writing Modules/namespace polution by thekestrel

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!
  • 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, details, 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, summary, 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.