package MooseX::MethodPrivate; use Moose; use Moose::Exporter; use Carp qw/croak/; our $VERSION = '0.1.0'; Moose::Exporter->setup_import_methods( with_caller => [qw( private protected )], ); sub private { my $caller = shift; my $name = shift; my $real_body = shift; my $body = sub { croak "The $caller\::$name method is private" unless ( scalar caller() ) eq $caller; goto &{$real_body}; }; $caller->meta->add_method( $name, $body ); } sub protected { my $caller = shift; my $name = shift; my $real_body = shift; my $body = sub { my $new_caller = caller(); my @isa = $new_caller->meta->superclasses; my @check = grep {/$caller/} @isa; croak "The $caller\::$name method is protected" unless ( ( scalar caller() ) eq $caller || @check ); goto &{$real_body}; }; $caller->meta->add_method( $name, $body ); } 1; __END__ =head1 NAME MooseX::MethodPrivate - Declare methods private or protected =head1 SYNOPSIS package Foo; use MooseX::MethodPrivate; private 'foo' => sub { ... } protected 'bar' => sub { ... } ... my $foo = Foo->new; $foo->foo; # die, can't call foo because it's a private method $foo->bar; # die, can't call bar because it's a protected method package Bar; use MooseX::MethodPrivate; extends qw/Foo/; sub baz { my $self = shift; $self->foo; # die, can't call foo because it's a private method $self->bar; # ok, can call this method because we extends Foo and # it's a protected method } =head1 DESCRIPTION MooseX::MethodPrivate add two new keyword for methods declaration: =over 2 =item B =item B =back =head2 METHODS =over 4 =item B A private method is visible only in the class. =item B A protected method is visible in the class and any subclasses. =back =head1 AUTHOR franck cuny Efranck.cuny {at} rtgi.frE =head1 SEE ALSO =head1 LICENSE Copyright (c) 2009, RTGI All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L.