Commit | Line | Data |
74862722 |
1 | package Moose::Meta::Method::Overridden; |
8ee73eeb |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8ee73eeb |
6 | use base 'Moose::Meta::Method'; |
7 | |
18c2ec0e |
8 | sub new { |
9 | my ( $class, %args ) = @_; |
10 | |
11 | # the package can be overridden by roles |
12 | # it is really more like body's compilation stash |
13 | # this is where we need to override the definition of super() so that the |
14 | # body of the code can call the right overridden version |
991933fb |
15 | my $super_package = $args{package} || $args{class}->name; |
18c2ec0e |
16 | |
17 | my $name = $args{name}; |
18 | |
19 | my $super = $args{class}->find_next_method_by_name($name); |
20 | |
21 | (defined $super) |
cee532a1 |
22 | || $class->throw_error("You cannot override '$name' because it has no super method", data => $name); |
18c2ec0e |
23 | |
24 | my $super_body = $super->body; |
25 | |
3f9e4b0a |
26 | my $method = $args{method}; |
18c2ec0e |
27 | |
28 | my $body = sub { |
991933fb |
29 | local $Moose::SUPER_PACKAGE = $super_package; |
3f9e4b0a |
30 | local @Moose::SUPER_ARGS = @_; |
31 | local $Moose::SUPER_BODY = $super_body; |
32 | return $method->(@_); |
18c2ec0e |
33 | }; |
34 | |
3f9e4b0a |
35 | # FIXME do we need this make sure this works for next::method? |
991933fb |
36 | # subname "${super_package}::${name}", $method; |
3f9e4b0a |
37 | |
18c2ec0e |
38 | # FIXME store additional attrs |
1b2aea39 |
39 | $class->wrap( |
40 | $body, |
41 | package_name => $args{class}->name, |
42 | name => $name |
43 | ); |
18c2ec0e |
44 | } |
45 | |
8ee73eeb |
46 | 1; |
47 | |
ad46f524 |
48 | # ABSTRACT: A Moose Method metaclass for overridden methods |
49 | |
8ee73eeb |
50 | __END__ |
51 | |
52 | =pod |
53 | |
39b3bc94 |
54 | =head1 DESCRIPTION |
55 | |
cad6578d |
56 | This class implements method overriding logic for the L<Moose> |
57 | C<override> keyword. |
18c2ec0e |
58 | |
cad6578d |
59 | The overriding subroutine's parent will be invoked explicitly using |
60 | the C<super> keyword from the parent class's method definition. |
18c2ec0e |
61 | |
62 | =head1 METHODS |
63 | |
64 | =over 4 |
65 | |
cad6578d |
66 | =item B<< Moose::Meta::Method::Overridden->new(%options) >> |
67 | |
68 | This constructs a new object. It accepts the following options: |
69 | |
70 | =over 8 |
71 | |
72 | =item * class |
73 | |
74 | The metaclass object for the class in which the override is being |
75 | declared. This option is required. |
76 | |
77 | =item * name |
78 | |
79 | The name of the method which we are overriding. This method must exist |
80 | in one of the class's superclasses. This option is required. |
81 | |
82 | =item * method |
83 | |
84 | The subroutine reference which implements the overriding. This option |
85 | is required. |
86 | |
87 | =back |
39b3bc94 |
88 | |
18c2ec0e |
89 | =back |
39b3bc94 |
90 | |
91 | =head1 BUGS |
92 | |
d4048ef3 |
93 | See L<Moose/BUGS> for details on reporting bugs. |
39b3bc94 |
94 | |
18c2ec0e |
95 | =cut |