Re: private/protected methods

[email protected] (Stevan Little)
Newsgroups perl.moose
Message-ID <[email protected]>
Jonathan,

Sorry it took so long to get back to you on this.

No, I do not have any intentions of directly supporting this in Moose  
right now. I am of the persuasion that this should be enforced with  
social and naming conventions, and not code. Part of the reason I  
feel this way is that it is really almost impossible to implement an  
efficient and correct version of private/protected methods in Perl.  
The simplistic:

sub foo { # private
     die if caller() ne __PACKAGE__;
     ....
}

sub bar { # protected
     die unless caller()->isa(__PACKAGE__) || __PACKAGE__->isa(caller 
());
     ...
}

Are not correct, because they do not handle the following case:

package Foo;

sub foo { # public
     return "Foo::foo"
}

package Bar;

extends Foo;

sub foo { # private
     die if caller() ne __PACKAGE__;
     return "Bar::foo";
}

package main;

print Bar->new->foo; # this will die, but it should print Foo::foo

In order to make the above work, you would need to do lots of  
AUTOLOAD trickery, and that is both inefficient and very error prone.

Now, Yuval Kogman has been working on private methods (and class/ 
instance methods, etc) in MO (an experimental offshoot of Moose,  
which will likely become the basis for Moose 1.0 and beyond). But  
this is still a long way off, and so I have not even given much  
thought to what the sugar layer would look like.

- Stevan

On Nov 29, 2006, at 5:02 PM, Jonathan Swartz wrote:

> Hi Stevan -
>
> Do you have any intentions of supporting enforced private methods  
> (not just attributes) in classes, i.e. a method that is defined  
> with a normal sub {} but can only be called from the defining class?
>
> If you do have intentions of supporting this someday, do you know  
> what syntax you will use?
>
> Thanks
> Jon
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.