Perl6 Object Oriented Cookbook (v0.2.1)  
Section 10: Limiting Access to Classes, Attributes, and Methods  
 
Recipe 10.4: Explicitly Violating the Privacy of an Attribute or Method
Last Updated: Sep 8, 2003
Status: Draft
      Previous Page   Next Page

How important is this problem to you?
  (Login to Vote)
4.50 Rating, 2 Votes  

How acceptable is the proposed solution?
  (Login to Vote)
2.00 Rating, 1 Vote  

Problem:  

A class has a private attribute or method. Foolhardily, you want to create code that violates that privacy and accesses it anyway.

Solution:  

Refer to the exact location of the attribute or method:

class MyClass {
	method foo is private { ... }
}

my $obj = MyClass.new;
$obj.foo;               # WRONG, is private
$obj.Myclass.foo;       # ok, it's explicitly named

Discussion:

Privacy is not absolute in perl6; if you are intent, there are ways to get around it. Most succinctly; even if an attribute or method has been declared is private, it may be explicitly accessed, from anywhere, by specifying its full location. This is not recommended, but it is supported.

Issue: Icky. Is it better to have a supported privacy hole, or the inevitable "unauthorized" privacy holes?

Issue: Alternate approach: $obj.SPY.foo or similar?


Log In to Comment


Login / Edit User Info -- Copyright © 2002 Cognitivity -- Previous Page   Next Page