2 package Moose::Meta::Role;
9 use Scalar::Util 'blessed', 'reftype';
10 use Sub::Name 'subname';
11 use B 'svref_2object';
13 our $VERSION = '0.01';
15 Moose::Meta::Role->meta->add_attribute('$:package' => (
17 init_arg => ':package',
20 Moose::Meta::Role->meta->add_attribute('@:requires' => (
22 predicate => 'has_requires',
23 init_arg => ':requires',
30 my ($class, %options) = @_;
31 my $pkg = $options{':package'};
32 $ROLES{$pkg} ||= $class->meta->new_object(%options);
37 my ($self, $method_name, $method) = @_;
38 (defined $method_name && $method_name)
39 || confess "You must define a method name";
40 # use reftype here to allow for blessed subs ...
41 ('CODE' eq (reftype($method) || ''))
42 || confess "Your code block must be a CODE reference";
43 my $full_method_name = ($self->name . '::' . $method_name);
46 no warnings 'redefine';
47 *{$full_method_name} = subname $full_method_name => $method;
51 my ($self, $method_name, $method) = @_;
52 (defined $method_name && $method_name)
53 || confess "You must define a method name";
54 # use reftype here to allow for blessed subs ...
55 ('CODE' eq (reftype($method) || ''))
56 || confess "Your code block must be a CODE reference";
57 my $full_method_name = ($self->name . '::' . $method_name);
60 no warnings 'redefine';
61 *{$full_method_name} = $method;
65 my ($self, $method_name) = @_;
66 (defined $method_name && $method_name)
67 || confess "You must define a method name";
69 my $sub_name = ($self->name . '::' . $method_name);
72 return 0 if !defined(&{$sub_name});
73 my $method = \&{$sub_name};
74 return 0 if (svref_2object($method)->GV->STASH->NAME || '') ne $self->name &&
75 (svref_2object($method)->GV->NAME || '') ne '__ANON__';
80 my ($self, $method_name) = @_;
81 (defined $method_name && $method_name)
82 || confess "You must define a method name";
84 return unless $self->has_method($method_name);
87 return \&{$self->name . '::' . $method_name};
91 my ($self, $method_name) = @_;
92 (defined $method_name && $method_name)
93 || confess "You must define a method name";
95 my $removed_method = $self->get_method($method_name);
98 delete ${$self->name . '::'}{$method_name}
99 if defined $removed_method;
101 return $removed_method;
104 sub get_method_list {
107 grep { !/meta/ && $self->has_method($_) } %{$self->name . '::'};
118 Moose::Meta::Role - The Moose role metaobject
132 All complex software has bugs lurking in it, and this module is no
133 exception. If you find a bug please either email me, or add the bug
138 Stevan Little E<lt>stevan@iinteractive.comE<gt>
140 =head1 COPYRIGHT AND LICENSE
142 Copyright 2006 by Infinity Interactive, Inc.
144 L<http://www.iinteractive.com>
146 This library is free software; you can redistribute it and/or modify
147 it under the same terms as Perl itself.