1 package Moose::Cookbook::Meta::Recipe6;
3 # ABSTRACT: A method metaclass for marking methods public or private
12 package My::Meta::Method;
15 use Moose::Util::TypeConstraints;
17 extends 'Moose::Meta::Method';
21 isa => enum( [ qw( public private ) ] ),
30 my $self = $class->SUPER::wrap(%options);
32 $self->{_policy} = $options{policy};
34 $self->_add_policy_wrapper;
39 sub _add_policy_wrapper {
42 return if $self->is_public;
44 my $name = $self->name;
45 my $package = $self->package_name;
46 my $real_body = $self->body;
49 die "The $package\::$name method is private"
50 unless ( scalar caller() ) eq $package;
55 $self->{body} = $body;
58 sub is_public { $_[0]->_policy eq 'public' }
59 sub is_private { $_[0]->_policy eq 'private' }
65 has 'password' => ( is => 'rw' );
67 __PACKAGE__->meta()->add_method(
69 My::Meta::Method->new(
70 name => '_reset_password',
71 package_name => __PACKAGE__,
72 body => sub { $_[0]->password('reset') },
79 This example shows a custom method metaclass that models public versus
80 private methods. If a method is defined as private, it adds a wrapper
81 around the method which dies unless it is called from the class where
84 The way the method is added to the class is rather ugly. If we wanted
85 to make this a real feature, we'd probably want to add some sort of
86 sugar to allow us to declare private methods, but that is beyond the
87 scope of this recipe. See the Extending recipes for more on this
90 The core of our custom class is the C<policy> attribute, and
91 C<_add_policy_wrapper> method.
93 You'll note that we have to explicitly set the C<policy> attribute in
96 $self->{_policy} = $options{policy};
98 That is necessary because Moose metaclasses do not use the meta API to
99 create objects. Most Moose classes have a custom "inlined" constructor
102 In this particular case, our parent class's constructor is the C<wrap>
103 method. We call that to build our object, but it does not include
104 subclass-specific attributes.
106 The C<_add_policy_wrapper> method is where the real work is done. If
107 the method is private, we construct a wrapper around the real
108 subroutine which checks that the caller matches the package in which
109 the subroutine was created.
111 If they don't match, it dies. If they do match, the real method is
112 called. We use C<goto> so that the wrapper does not show up in the
115 Finally, we replace the value of C<< $self->{body} >>. This is another
116 case where we have to do something a bit gross because Moose does not
117 use Moose for its own implementation.
119 When we pass this method object to the metaclass's C<add_method>
120 method, it will take the method body and make it available in the
123 Finally, when we retrieve these methods via the introspection API, we
124 can call the C<is_public> and C<is_private> methods on them to get
125 more information about the method.
129 A custom method metaclass lets us add both behavior and
130 meta-information to methods. Unfortunately, because the Perl
131 interpreter does not provide easy hooks into method declaration, the
132 API we have for adding these methods is not very pretty.
134 That can be improved with custom Moose-like sugar, or even by using a
135 tool like L<Devel::Declare> to create full-blown new keywords in Perl.
143 my $user = MyApp::User->new( password => 'foo!' );
145 like( exception { $user->_reset_password },
146 qr/The MyApp::User::_reset_password method is private/,
147 '_reset_password method dies if called outside MyApp::User class');
152 sub run_reset { $_[0]->_reset_password }
157 is( $user->password, 'reset', 'password has been reset' );