Filename | /opt/perl-5.18.1/lib/site_perl/5.18.1/darwin-thread-multi-2level/Moose/Meta/Method/Delegation.pm |
Statements | Executed 14 statements in 861µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 12µs | 12µs | BEGIN@3 | Moose::Meta::Method::Delegation::
1 | 1 | 1 | 11µs | 140µs | BEGIN@16 | Moose::Meta::Method::Delegation::
1 | 1 | 1 | 10µs | 47µs | BEGIN@13 | Moose::Meta::Method::Delegation::
1 | 1 | 1 | 9µs | 14µs | BEGIN@11 | Moose::Meta::Method::Delegation::
1 | 1 | 1 | 9µs | 42µs | BEGIN@14 | Moose::Meta::Method::Delegation::
1 | 1 | 1 | 9µs | 24µs | BEGIN@10 | Moose::Meta::Method::Delegation::
0 | 0 | 0 | 0s | 0s | __ANON__[:111] | Moose::Meta::Method::Delegation::
0 | 0 | 0 | 0s | 0s | _get_delegate_accessor | Moose::Meta::Method::Delegation::
0 | 0 | 0 | 0s | 0s | _initialize_body | Moose::Meta::Method::Delegation::
0 | 0 | 0 | 0s | 0s | _new | Moose::Meta::Method::Delegation::
0 | 0 | 0 | 0s | 0s | associated_attribute | Moose::Meta::Method::Delegation::
0 | 0 | 0 | 0s | 0s | curried_arguments | Moose::Meta::Method::Delegation::
0 | 0 | 0 | 0s | 0s | delegate_to_method | Moose::Meta::Method::Delegation::
0 | 0 | 0 | 0s | 0s | new | Moose::Meta::Method::Delegation::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | |||||
2 | package Moose::Meta::Method::Delegation; | ||||
3 | # spent 12µs within Moose::Meta::Method::Delegation::BEGIN@3 which was called:
# once (12µs+0s) by Moose::Meta::Attribute::BEGIN@22 at line 5 | ||||
4 | 1 | 7µs | $Moose::Meta::Method::Delegation::AUTHORITY = 'cpan:STEVAN'; | ||
5 | 1 | 40µs | 1 | 12µs | } # spent 12µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@3 |
6 | { | ||||
7 | 2 | 1µs | $Moose::Meta::Method::Delegation::VERSION = '2.1005'; | ||
8 | } | ||||
9 | |||||
10 | 2 | 30µs | 2 | 40µs | # spent 24µs (9+16) within Moose::Meta::Method::Delegation::BEGIN@10 which was called:
# once (9µs+16µs) by Moose::Meta::Attribute::BEGIN@22 at line 10 # spent 24µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@10
# spent 16µs making 1 call to strict::import |
11 | 2 | 42µs | 2 | 18µs | # spent 14µs (9+5) within Moose::Meta::Method::Delegation::BEGIN@11 which was called:
# once (9µs+5µs) by Moose::Meta::Attribute::BEGIN@22 at line 11 # spent 14µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@11
# spent 5µs making 1 call to warnings::import |
12 | |||||
13 | 2 | 36µs | 2 | 84µs | # spent 47µs (10+37) within Moose::Meta::Method::Delegation::BEGIN@13 which was called:
# once (10µs+37µs) by Moose::Meta::Attribute::BEGIN@22 at line 13 # spent 47µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@13
# spent 37µs making 1 call to Exporter::import |
14 | 2 | 37µs | 2 | 75µs | # spent 42µs (9+33) within Moose::Meta::Method::Delegation::BEGIN@14 which was called:
# once (9µs+33µs) by Moose::Meta::Attribute::BEGIN@22 at line 14 # spent 42µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@14
# spent 33µs making 1 call to Exporter::import |
15 | |||||
16 | 1 | 400ns | # spent 140µs (11+129) within Moose::Meta::Method::Delegation::BEGIN@16 which was called:
# once (11µs+129µs) by Moose::Meta::Attribute::BEGIN@22 at line 17 | ||
17 | 1 | 662µs | 2 | 269µs | 'Class::MOP::Method::Generated'; # spent 140µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@16
# spent 129µs making 1 call to base::import |
18 | |||||
19 | |||||
20 | sub new { | ||||
21 | my $class = shift; | ||||
22 | my %options = @_; | ||||
23 | |||||
24 | ( exists $options{attribute} ) | ||||
25 | || confess "You must supply an attribute to construct with"; | ||||
26 | |||||
27 | ( blessed( $options{attribute} ) | ||||
28 | && $options{attribute}->isa('Moose::Meta::Attribute') ) | ||||
29 | || confess | ||||
30 | "You must supply an attribute which is a 'Moose::Meta::Attribute' instance"; | ||||
31 | |||||
32 | ( $options{package_name} && $options{name} ) | ||||
33 | || confess | ||||
34 | "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"; | ||||
35 | |||||
36 | ( $options{delegate_to_method} && ( !ref $options{delegate_to_method} ) | ||||
37 | || ( 'CODE' eq ref $options{delegate_to_method} ) ) | ||||
38 | || confess | ||||
39 | 'You must supply a delegate_to_method which is a method name or a CODE reference'; | ||||
40 | |||||
41 | exists $options{curried_arguments} | ||||
42 | || ( $options{curried_arguments} = [] ); | ||||
43 | |||||
44 | ( $options{curried_arguments} && | ||||
45 | ( 'ARRAY' eq ref $options{curried_arguments} ) ) | ||||
46 | || confess 'You must supply a curried_arguments which is an ARRAY reference'; | ||||
47 | |||||
48 | my $self = $class->_new( \%options ); | ||||
49 | |||||
50 | weaken( $self->{'attribute'} ); | ||||
51 | |||||
52 | $self->_initialize_body; | ||||
53 | |||||
54 | return $self; | ||||
55 | } | ||||
56 | |||||
57 | sub _new { | ||||
58 | my $class = shift; | ||||
59 | my $options = @_ == 1 ? $_[0] : {@_}; | ||||
60 | |||||
61 | return bless $options, $class; | ||||
62 | } | ||||
63 | |||||
64 | sub curried_arguments { (shift)->{'curried_arguments'} } | ||||
65 | |||||
66 | sub associated_attribute { (shift)->{'attribute'} } | ||||
67 | |||||
68 | sub delegate_to_method { (shift)->{'delegate_to_method'} } | ||||
69 | |||||
70 | sub _initialize_body { | ||||
71 | my $self = shift; | ||||
72 | |||||
73 | my $method_to_call = $self->delegate_to_method; | ||||
74 | return $self->{body} = $method_to_call | ||||
75 | if ref $method_to_call; | ||||
76 | |||||
77 | my $accessor = $self->_get_delegate_accessor; | ||||
78 | |||||
79 | my $handle_name = $self->name; | ||||
80 | |||||
81 | # NOTE: we used to do a goto here, but the goto didn't handle | ||||
82 | # failure correctly (it just returned nothing), so I took that | ||||
83 | # out. However, the more I thought about it, the less I liked it | ||||
84 | # doing the goto, and I preferred the act of delegation being | ||||
85 | # actually represented in the stack trace. - SL | ||||
86 | # not inlining this, since it won't really speed things up at | ||||
87 | # all... the only thing that would end up different would be | ||||
88 | # interpolating in $method_to_call, and a bunch of things in the | ||||
89 | # error handling that mostly never gets called - doy | ||||
90 | $self->{body} = sub { | ||||
91 | my $instance = shift; | ||||
92 | my $proxy = $instance->$accessor(); | ||||
93 | |||||
94 | my $error | ||||
95 | = !defined $proxy ? ' is not defined' | ||||
96 | : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')} | ||||
97 | : undef; | ||||
98 | |||||
99 | if ($error) { | ||||
100 | $self->throw_error( | ||||
101 | "Cannot delegate $handle_name to $method_to_call because " | ||||
102 | . "the value of " | ||||
103 | . $self->associated_attribute->name | ||||
104 | . $error, | ||||
105 | method_name => $method_to_call, | ||||
106 | object => $instance | ||||
107 | ); | ||||
108 | } | ||||
109 | unshift @_, @{ $self->curried_arguments }; | ||||
110 | $proxy->$method_to_call(@_); | ||||
111 | }; | ||||
112 | } | ||||
113 | |||||
114 | sub _get_delegate_accessor { | ||||
115 | my $self = shift; | ||||
116 | my $attr = $self->associated_attribute; | ||||
117 | |||||
118 | # NOTE: | ||||
119 | # always use a named method when | ||||
120 | # possible, if you use the method | ||||
121 | # ref and there are modifiers on | ||||
122 | # the accessors then it will not | ||||
123 | # pick up the modifiers too. Only | ||||
124 | # the named method will assure that | ||||
125 | # we also have any modifiers run. | ||||
126 | # - SL | ||||
127 | my $accessor = $attr->has_read_method | ||||
128 | ? $attr->get_read_method | ||||
129 | : $attr->get_read_method_ref; | ||||
130 | |||||
131 | $accessor = $accessor->body if Scalar::Util::blessed $accessor; | ||||
132 | |||||
133 | return $accessor; | ||||
134 | } | ||||
135 | |||||
136 | 1 | 4µs | 1; | ||
137 | |||||
138 | # ABSTRACT: A Moose Method metaclass for delegation methods | ||||
139 | |||||
140 | __END__ |