| Filename | /Users/ap13/perl5/lib/perl5/darwin-2level/Moose/Meta/Method/Delegation.pm |
| Statements | Executed 12 statements in 534µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 13µs | 26µs | Moose::Meta::Method::Delegation::BEGIN@4 |
| 1 | 1 | 1 | 11µs | 16µs | Moose::Meta::Method::Delegation::BEGIN@5 |
| 1 | 1 | 1 | 9µs | 144µs | Moose::Meta::Method::Delegation::BEGIN@12 |
| 1 | 1 | 1 | 8µs | 36µs | Moose::Meta::Method::Delegation::BEGIN@7 |
| 1 | 1 | 1 | 7µs | 37µs | Moose::Meta::Method::Delegation::BEGIN@9 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Method::Delegation::__ANON__[:111] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Method::Delegation::_get_delegate_accessor |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Method::Delegation::_initialize_body |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Method::Delegation::_new |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Method::Delegation::associated_attribute |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Method::Delegation::curried_arguments |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Method::Delegation::delegate_to_method |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Method::Delegation::new |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Moose::Meta::Method::Delegation; | ||||
| 2 | 1 | 600ns | our $VERSION = '2.1403'; | ||
| 3 | |||||
| 4 | 2 | 22µs | 2 | 39µs | # spent 26µs (13+13) within Moose::Meta::Method::Delegation::BEGIN@4 which was called:
# once (13µs+13µs) by Moose::Meta::Attribute::BEGIN@14 at line 4 # spent 26µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@4
# spent 13µs making 1 call to strict::import |
| 5 | 2 | 24µs | 2 | 20µs | # spent 16µs (11+4) within Moose::Meta::Method::Delegation::BEGIN@5 which was called:
# once (11µs+4µs) by Moose::Meta::Attribute::BEGIN@14 at line 5 # spent 16µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@5
# spent 4µs making 1 call to warnings::import |
| 6 | |||||
| 7 | 2 | 26µs | 2 | 65µs | # spent 36µs (8+29) within Moose::Meta::Method::Delegation::BEGIN@7 which was called:
# once (8µs+29µs) by Moose::Meta::Attribute::BEGIN@14 at line 7 # spent 36µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@7
# spent 29µs making 1 call to Exporter::import |
| 8 | |||||
| 9 | 1 | 400ns | # spent 37µs (7+30) within Moose::Meta::Method::Delegation::BEGIN@9 which was called:
# once (7µs+30µs) by Moose::Meta::Attribute::BEGIN@14 at line 10 | ||
| 10 | 1 | 27µs | 2 | 66µs | 'Class::MOP::Method::Generated'; # spent 37µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@9
# spent 30µs making 1 call to parent::import |
| 11 | |||||
| 12 | 2 | 431µs | 2 | 278µs | # spent 144µs (9+135) within Moose::Meta::Method::Delegation::BEGIN@12 which was called:
# once (9µs+135µs) by Moose::Meta::Attribute::BEGIN@14 at line 12 # spent 144µs making 1 call to Moose::Meta::Method::Delegation::BEGIN@12
# spent 135µs making 1 call to Sub::Exporter::__ANON__[Sub/Exporter.pm:337] |
| 13 | |||||
| 14 | sub new { | ||||
| 15 | my $class = shift; | ||||
| 16 | my %options = @_; | ||||
| 17 | |||||
| 18 | ( exists $options{attribute} ) | ||||
| 19 | || throw_exception( MustSupplyAnAttributeToConstructWith => params => \%options, | ||||
| 20 | class => $class | ||||
| 21 | ); | ||||
| 22 | |||||
| 23 | ( blessed( $options{attribute} ) | ||||
| 24 | && $options{attribute}->isa('Moose::Meta::Attribute') ) | ||||
| 25 | || throw_exception( MustSupplyAMooseMetaAttributeInstance => params => \%options, | ||||
| 26 | class => $class | ||||
| 27 | ); | ||||
| 28 | |||||
| 29 | ( $options{package_name} && $options{name} ) | ||||
| 30 | || throw_exception( MustSupplyPackageNameAndName => params => \%options, | ||||
| 31 | class => $class | ||||
| 32 | ); | ||||
| 33 | |||||
| 34 | ( $options{delegate_to_method} && ( !ref $options{delegate_to_method} ) | ||||
| 35 | || ( 'CODE' eq ref $options{delegate_to_method} ) ) | ||||
| 36 | || throw_exception( MustSupplyADelegateToMethod => params => \%options, | ||||
| 37 | class => $class | ||||
| 38 | ); | ||||
| 39 | |||||
| 40 | exists $options{curried_arguments} | ||||
| 41 | || ( $options{curried_arguments} = [] ); | ||||
| 42 | |||||
| 43 | ( $options{curried_arguments} && | ||||
| 44 | ( 'ARRAY' eq ref $options{curried_arguments} ) ) | ||||
| 45 | || throw_exception( MustSupplyArrayRefAsCurriedArguments => params => \%options, | ||||
| 46 | class_name => $class | ||||
| 47 | ); | ||||
| 48 | |||||
| 49 | my $self = $class->_new( \%options ); | ||||
| 50 | |||||
| 51 | weaken( $self->{'attribute'} ); | ||||
| 52 | |||||
| 53 | $self->_initialize_body; | ||||
| 54 | |||||
| 55 | return $self; | ||||
| 56 | } | ||||
| 57 | |||||
| 58 | sub _new { | ||||
| 59 | my $class = shift; | ||||
| 60 | my $options = @_ == 1 ? $_[0] : {@_}; | ||||
| 61 | |||||
| 62 | return bless $options, $class; | ||||
| 63 | } | ||||
| 64 | |||||
| 65 | sub curried_arguments { (shift)->{'curried_arguments'} } | ||||
| 66 | |||||
| 67 | sub associated_attribute { (shift)->{'attribute'} } | ||||
| 68 | |||||
| 69 | sub delegate_to_method { (shift)->{'delegate_to_method'} } | ||||
| 70 | |||||
| 71 | sub _initialize_body { | ||||
| 72 | my $self = shift; | ||||
| 73 | |||||
| 74 | my $method_to_call = $self->delegate_to_method; | ||||
| 75 | return $self->{body} = $method_to_call | ||||
| 76 | if ref $method_to_call; | ||||
| 77 | |||||
| 78 | my $accessor = $self->_get_delegate_accessor; | ||||
| 79 | |||||
| 80 | my $handle_name = $self->name; | ||||
| 81 | |||||
| 82 | # NOTE: we used to do a goto here, but the goto didn't handle | ||||
| 83 | # failure correctly (it just returned nothing), so I took that | ||||
| 84 | # out. However, the more I thought about it, the less I liked it | ||||
| 85 | # doing the goto, and I preferred the act of delegation being | ||||
| 86 | # actually represented in the stack trace. - SL | ||||
| 87 | # not inlining this, since it won't really speed things up at | ||||
| 88 | # all... the only thing that would end up different would be | ||||
| 89 | # interpolating in $method_to_call, and a bunch of things in the | ||||
| 90 | # error handling that mostly never gets called - doy | ||||
| 91 | $self->{body} = sub { | ||||
| 92 | my $instance = shift; | ||||
| 93 | my $proxy = $instance->$accessor(); | ||||
| 94 | |||||
| 95 | if( !defined $proxy ) { | ||||
| 96 | throw_exception( AttributeValueIsNotDefined => method => $self, | ||||
| 97 | instance => $instance, | ||||
| 98 | attribute => $self->associated_attribute, | ||||
| 99 | ); | ||||
| 100 | } | ||||
| 101 | elsif( ref($proxy) && !blessed($proxy) ) { | ||||
| 102 | throw_exception( AttributeValueIsNotAnObject => method => $self, | ||||
| 103 | instance => $instance, | ||||
| 104 | attribute => $self->associated_attribute, | ||||
| 105 | given_value => $proxy | ||||
| 106 | ); | ||||
| 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 | 3µs | 1; | ||
| 137 | |||||
| 138 | # ABSTRACT: A Moose Method metaclass for delegation methods | ||||
| 139 | |||||
| 140 | __END__ |