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
123
124
125
126
127
128
129
130
131
132
133
134
135
|
package MooseX::Privacy;
our $VERSION = '0.01';
use Moose::Exporter;
Moose::Exporter->setup_import_methods(
with_meta => [qw( private_method protected_method )], );
sub private_method {
my ( $meta, $name, $body ) = @_;
$meta->add_private_method($name, $body);
}
sub protected_method {
my ( $meta, $name, $body ) = @_;
$meta->add_protected_method($name, $body);
}
sub init_meta {
my ( $me, %options ) = @_;
my $for = $options{for_class};
Moose->init_meta(%options);
Moose::Util::MetaRole::apply_metaroles(
for_class => $for,
metaclass_roles => [ 'MooseX::Privacy::Meta::Class', ],
);
}
1;
__END__
=head1 NAME
MooseX::Privacy - Provides the syntax to restrict/control visibility of your methods
=head1 SYNOPSIS
use MooseX::Privacy;
has config => (
is => 'rw',
isa => 'Some::Config',
traits => [qw/Private/],
);
has username => (
is => 'rw',
isa => 'Str',
traits => [qw/Protected/],
);
private_method foo => sub {
return 23;
};
protected_method bar => sub {
return 42;
};
=head1 DESCRIPTION
MooseX::Privacy brings the concept of private and protected methods to your class.
=head1 METHODS
=head2 Private
When you declare a method as B<private>, this method can be called only within the class.
package Foo;
use Moose;
use MooseX::Privacy;
private_method foo => sub { return 23 };
sub mul_by_foo { my $self = shift; $self->foo * $_[0] }
1;
my $foo = Foo->new;
$foo->foo; # die
$foo->mul_by_foo; # ok
=head2 Protected
When you declare a method as B<protected>, this method can be called only
within the class AND any of it's subclasses.
package Foo;
use Moose;
use MooseX::Privacy;
protected_method foo => sub { return 23 };
package Bar;
use Moose;
extends Foo;
sub bar { my $self = shift; $self->foo }
1;
my $foo = Foo->new;
$foo->foo; # die
my $bar = Bar->new;
$bar->bar; # ok
=head2 Attributes
=head3 Private
When the B<Private> traits is applied to an attribute, this attribute can only be read or set within the class.
=head3 Protected
When the B<Protected> traits is applied to an attribute, this attribute can only be read or set within the class AND any of his subclasses.
=head1 AUTHOR
franck cuny E<lt>franck@lumberjaph.netE<gt>
=head1 SEE ALSO
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|