blob: f82c26a57caea70b2c2ff57562d73f187f808700 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
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();
croak "The $caller\::$name method is protected"
unless ( ( scalar caller() ) eq $caller
|| $new_caller->isa($caller) );
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<private>
=item B<protected>
=back
=head2 METHODS
=over 4
=item B<private>
A private method is visible only in the class.
=item B<protected>
A protected method is visible in the class and any subclasses.
=back
=head1 AUTHOR
franck cuny E<lt>franck.cuny {at} rtgi.frE<gt>
=head1 SEE ALSO
Idea stolen from L<Moose::Cookbook::Meta::Recipe6>.
=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<perlartistic>.
|