Skip Menu |

This queue is for tickets about the MooseX-Declare CPAN distribution.

Report information
The Basics
Id: 49385
Status: open
Priority: 0/
Queue: MooseX-Declare

People
Owner: Nobody in particular
Requestors: dhorne [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Add protection to methods
This is simply a feature request that you may wish to consider. In the past I have used Attribute::Protected to denote whether methods are puiblic, private or protected. Now that we have Moose, and more specifically. MooseX::Declare, it would be nice if we could optionally specify the access to the methods: public method add(Num $a, Num $b) { # methods are public be default, so w don't need to specify this # if we don't want... } protected method validate_range(Int $a) { } private method check_state(Str $a) { } Thanks
On Wed Sep 02 20:42:11 2009, DHORNE wrote: Show quoted text
> This is simply a feature request that you may wish to consider. In the > past I have used Attribute::Protected to denote whether methods are > puiblic, private or protected. Now that we have Moose, and more > specifically. MooseX::Declare, it would be nice if we could optionally > specify the access to the methods: > > public method add(Num $a, Num $b) { > # methods are public be default, so w don't need to specify this > # if we don't want... > } > > protected method validate_range(Int $a) { > > } > > private method check_state(Str $a) { > > } > > Thanks
I'm never quite sure i like this approach. If the method is truly private then you can do: my $check_state = method(Str $a) { }; ... $self->$check_state("Foo"); I'm not sure protected makes sense in the perl world: "A Perl module would prefer that you stay out of its living room because you weren't invited, not because it has a shotgun." (from Programming Perl)
On Thu Sep 03 10:10:36 2009, ASH wrote: Show quoted text
> On Wed Sep 02 20:42:11 2009, DHORNE wrote:
> > This is simply a feature request that you may wish to consider. In the > > past I have used Attribute::Protected to denote whether methods are > > puiblic, private or protected. Now that we have Moose, and more > > specifically. MooseX::Declare, it would be nice if we could optionally > > specify the access to the methods: > > > > public method add(Num $a, Num $b) { > > # methods are public be default, so w don't need to specify this > > # if we don't want... > > } > > > > protected method validate_range(Int $a) { > > > > } > > > > private method check_state(Str $a) { > > > > } > > > > Thanks
> > I'm never quite sure i like this approach. If the method is truly > private then you can do: > > my $check_state = method(Str $a) { }; > > ... > > $self->$check_state("Foo"); > > I'm not sure protected makes sense in the perl world: "A Perl module > would prefer that you stay out of its living room because you weren't > invited, not because it has a shotgun." (from Programming Perl)
I appreciate this point of view, and understand that many Perlers will find my suggestion is outside the received culture. However, one of the attractions of MooseX::Declare is that it allows Perl to operate within an OO idiom that I prefer. My suggestion of an additional declarative syntax to my mind makes the code easier to read and also provides an explicit documentation of how the method should be accessed, as opposed to the implicit alternative offered. The Perl idiom is to prefix "private" methods with an underscore. I've personally worked on code where people came in to the living room uninvited, but instead of having a shotgun, I'd just prefer a lock on the door. Where else might this be useful? Consider a web application where the action is specified in a URL, and the action maps to a method in the controller. However, we don't want app users to enter a private method in the URL to get access to code that they shouldn't. CGI::Application's approach makes you define all the public methods in a setup method. This to me seems clumsy and inelegant. I much prefer the rails approach of being able to make private or protected methods actually private or protected. This would be much easier with an elegant declarative syntax.