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

comment on

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

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 rifling through the Monastery: (3)
As of 2021-11-28 20:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found