| Filename | /home/doy/perl5/perlbrew/perls/perl-5.10.1/lib/site_perl/5.10.1/MooseX/Params/Validate.pm |
| Statements | Executed 30 statements in 5.53ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 4.52ms | 21.8ms | MooseX::Params::Validate::BEGIN@7 |
| 1 | 1 | 1 | 1.59ms | 6.42ms | MooseX::Params::Validate::BEGIN@11 |
| 1 | 1 | 1 | 80µs | 99µs | MooseX::Params::Validate::BEGIN@3 |
| 1 | 1 | 1 | 63µs | 2.32ms | MooseX::Params::Validate::BEGIN@12 |
| 1 | 1 | 1 | 44µs | 2.35ms | MooseX::Params::Validate::BEGIN@10 |
| 1 | 1 | 1 | 42µs | 203µs | MooseX::Params::Validate::BEGIN@8 |
| 1 | 1 | 1 | 40µs | 201µs | MooseX::Params::Validate::BEGIN@6 |
| 1 | 1 | 1 | 39µs | 107µs | MooseX::Params::Validate::BEGIN@4 |
| 0 | 0 | 0 | 0s | 0s | MooseX::Params::Validate::__ANON__[:207] |
| 0 | 0 | 0 | 0s | 0s | MooseX::Params::Validate::_cache_key |
| 0 | 0 | 0 | 0s | 0s | MooseX::Params::Validate::_caller_name |
| 0 | 0 | 0 | 0s | 0s | MooseX::Params::Validate::_convert_to_param_validate_spec |
| 0 | 0 | 0 | 0s | 0s | MooseX::Params::Validate::_is_tc |
| 0 | 0 | 0 | 0s | 0s | MooseX::Params::Validate::pos_validated_list |
| 0 | 0 | 0 | 0s | 0s | MooseX::Params::Validate::validated_hash |
| 0 | 0 | 0 | 0s | 0s | MooseX::Params::Validate::validated_list |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package MooseX::Params::Validate; | ||||
| 2 | |||||
| 3 | 3 | 103µs | 2 | 119µs | # spent 99µs (80+19) within MooseX::Params::Validate::BEGIN@3 which was called:
# once (80µs+19µs) by Markdent::Role::HTMLStream::BEGIN@15 at line 3 # spent 99µs making 1 call to MooseX::Params::Validate::BEGIN@3
# spent 19µs making 1 call to strict::import |
| 4 | 3 | 106µs | 2 | 175µs | # spent 107µs (39+68) within MooseX::Params::Validate::BEGIN@4 which was called:
# once (39µs+68µs) by Markdent::Role::HTMLStream::BEGIN@15 at line 4 # spent 107µs making 1 call to MooseX::Params::Validate::BEGIN@4
# spent 68µs making 1 call to warnings::import |
| 5 | |||||
| 6 | 3 | 105µs | 2 | 362µs | # spent 201µs (40+161) within MooseX::Params::Validate::BEGIN@6 which was called:
# once (40µs+161µs) by Markdent::Role::HTMLStream::BEGIN@15 at line 6 # spent 201µs making 1 call to MooseX::Params::Validate::BEGIN@6
# spent 162µs making 1 call to Exporter::import |
| 7 | 3 | 589µs | 2 | 22.1ms | # spent 21.8ms (4.52+17.3) within MooseX::Params::Validate::BEGIN@7 which was called:
# once (4.52ms+17.3ms) by Markdent::Role::HTMLStream::BEGIN@15 at line 7 # spent 21.8ms making 1 call to MooseX::Params::Validate::BEGIN@7
# spent 247µs making 1 call to Exporter::import |
| 8 | 3 | 131µs | 2 | 364µs | # spent 203µs (42+161) within MooseX::Params::Validate::BEGIN@8 which was called:
# once (42µs+161µs) by Markdent::Role::HTMLStream::BEGIN@15 at line 8 # spent 203µs making 1 call to MooseX::Params::Validate::BEGIN@8
# spent 161µs making 1 call to Exporter::import |
| 9 | |||||
| 10 | 3 | 143µs | 2 | 4.65ms | # spent 2.35ms (44µs+2.30) within MooseX::Params::Validate::BEGIN@10 which was called:
# once (44µs+2.30ms) by Markdent::Role::HTMLStream::BEGIN@15 at line 10 # spent 2.35ms making 1 call to MooseX::Params::Validate::BEGIN@10
# spent 2.30ms making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:456] |
| 11 | 3 | 662µs | 1 | 6.42ms | # spent 6.42ms (1.59+4.83) within MooseX::Params::Validate::BEGIN@11 which was called:
# once (1.59ms+4.83ms) by Markdent::Role::HTMLStream::BEGIN@15 at line 11 # spent 6.42ms making 1 call to MooseX::Params::Validate::BEGIN@11 |
| 12 | 1 | 48µs | 1 | 2.25ms | # spent 2.32ms (63µs+2.25) within MooseX::Params::Validate::BEGIN@12 which was called:
# once (63µs+2.25ms) by Markdent::Role::HTMLStream::BEGIN@15 at line 20 # spent 2.25ms making 1 call to Sub::Exporter::__ANON__[Sub/Exporter.pm:756] |
| 13 | exports => [ | ||||
| 14 | qw( validated_hash validated_list pos_validated_list validate validatep ) | ||||
| 15 | ], | ||||
| 16 | groups => { | ||||
| 17 | default => [qw( validated_hash validated_list pos_validated_list )], | ||||
| 18 | deprecated => [qw( validate validatep )], | ||||
| 19 | }, | ||||
| 20 | 2 | 3.61ms | 1 | 2.32ms | }; # spent 2.32ms making 1 call to MooseX::Params::Validate::BEGIN@12 |
| 21 | |||||
| 22 | 1 | 5µs | our $VERSION = '0.14'; | ||
| 23 | 1 | 3µs | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 24 | |||||
| 25 | 1 | 2µs | my %CACHED_SPECS; | ||
| 26 | |||||
| 27 | sub validated_hash { | ||||
| 28 | my ( $args, %spec ) = @_; | ||||
| 29 | |||||
| 30 | my $cache_key = _cache_key( \%spec ); | ||||
| 31 | |||||
| 32 | if ( exists $CACHED_SPECS{$cache_key} ) { | ||||
| 33 | ( ref $CACHED_SPECS{$cache_key} eq 'HASH' ) | ||||
| 34 | || confess | ||||
| 35 | "I was expecting a HASH-ref in the cached $cache_key parameter" | ||||
| 36 | . " spec, you are doing something funky, stop it!"; | ||||
| 37 | %spec = %{ $CACHED_SPECS{$cache_key} }; | ||||
| 38 | } | ||||
| 39 | else { | ||||
| 40 | my $should_cache = delete $spec{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1; | ||||
| 41 | |||||
| 42 | $spec{$_} = _convert_to_param_validate_spec( $spec{$_} ) | ||||
| 43 | foreach keys %spec; | ||||
| 44 | |||||
| 45 | $CACHED_SPECS{$cache_key} = \%spec | ||||
| 46 | if $should_cache; | ||||
| 47 | } | ||||
| 48 | |||||
| 49 | my $instance; | ||||
| 50 | $instance = shift @$args if blessed $args->[0]; | ||||
| 51 | |||||
| 52 | my %args = @$args; | ||||
| 53 | |||||
| 54 | $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) | ||||
| 55 | for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec; | ||||
| 56 | |||||
| 57 | %args = Params::Validate::validate_with( | ||||
| 58 | params => \%args, | ||||
| 59 | spec => \%spec, | ||||
| 60 | called => _caller_name(), | ||||
| 61 | ); | ||||
| 62 | |||||
| 63 | return ( ( defined $instance ? $instance : () ), %args ); | ||||
| 64 | } | ||||
| 65 | |||||
| 66 | 1 | 6µs | *validate = \&validated_hash; | ||
| 67 | |||||
| 68 | sub validated_list { | ||||
| 69 | my ( $args, @spec ) = @_; | ||||
| 70 | |||||
| 71 | my %spec = @spec; | ||||
| 72 | |||||
| 73 | my $cache_key = _cache_key( \%spec ); | ||||
| 74 | |||||
| 75 | my @ordered_spec; | ||||
| 76 | if ( exists $CACHED_SPECS{$cache_key} ) { | ||||
| 77 | ( ref $CACHED_SPECS{$cache_key} eq 'ARRAY' ) | ||||
| 78 | || confess | ||||
| 79 | "I was expecting a ARRAY-ref in the cached $cache_key parameter" | ||||
| 80 | . " spec, you are doing something funky, stop it!"; | ||||
| 81 | %spec = %{ $CACHED_SPECS{$cache_key}->[0] }; | ||||
| 82 | @ordered_spec = @{ $CACHED_SPECS{$cache_key}->[1] }; | ||||
| 83 | } | ||||
| 84 | else { | ||||
| 85 | my $should_cache = delete $spec{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1; | ||||
| 86 | |||||
| 87 | @ordered_spec = grep { exists $spec{$_} } @spec; | ||||
| 88 | |||||
| 89 | $spec{$_} = _convert_to_param_validate_spec( $spec{$_} ) | ||||
| 90 | foreach keys %spec; | ||||
| 91 | |||||
| 92 | $CACHED_SPECS{$cache_key} = [ \%spec, \@ordered_spec ] | ||||
| 93 | if $should_cache; | ||||
| 94 | } | ||||
| 95 | |||||
| 96 | my $instance; | ||||
| 97 | $instance = shift @$args if blessed $args->[0]; | ||||
| 98 | |||||
| 99 | my %args = @$args; | ||||
| 100 | |||||
| 101 | $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) | ||||
| 102 | for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec; | ||||
| 103 | |||||
| 104 | %args = Params::Validate::validate_with( | ||||
| 105 | params => \%args, | ||||
| 106 | spec => \%spec, | ||||
| 107 | called => _caller_name(), | ||||
| 108 | ); | ||||
| 109 | |||||
| 110 | return ( | ||||
| 111 | ( defined $instance ? $instance : () ), | ||||
| 112 | @args{@ordered_spec} | ||||
| 113 | ); | ||||
| 114 | } | ||||
| 115 | |||||
| 116 | 1 | 3µs | *validatep = \&validated_list; | ||
| 117 | |||||
| 118 | sub pos_validated_list { | ||||
| 119 | my $args = shift; | ||||
| 120 | |||||
| 121 | my @spec; | ||||
| 122 | push @spec, shift while ref $_[0]; | ||||
| 123 | |||||
| 124 | my %extra = @_; | ||||
| 125 | |||||
| 126 | my $cache_key = _cache_key( \%extra ); | ||||
| 127 | |||||
| 128 | my @pv_spec; | ||||
| 129 | if ( exists $CACHED_SPECS{$cache_key} ) { | ||||
| 130 | ( ref $CACHED_SPECS{$cache_key} eq 'ARRAY' ) | ||||
| 131 | || confess | ||||
| 132 | "I was expecting an ARRAY-ref in the cached $cache_key parameter" | ||||
| 133 | . " spec, you are doing something funky, stop it!"; | ||||
| 134 | @pv_spec = @{ $CACHED_SPECS{$cache_key} }; | ||||
| 135 | } | ||||
| 136 | else { | ||||
| 137 | my $should_cache = exists $extra{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1; | ||||
| 138 | |||||
| 139 | # prepare the parameters ... | ||||
| 140 | @pv_spec = map { _convert_to_param_validate_spec($_) } @spec; | ||||
| 141 | |||||
| 142 | $CACHED_SPECS{$cache_key} = \@pv_spec | ||||
| 143 | if $should_cache; | ||||
| 144 | } | ||||
| 145 | |||||
| 146 | my @args = @{$args}; | ||||
| 147 | |||||
| 148 | $args[$_] = $pv_spec[$_]{constraint}->coerce( $args[$_] ) | ||||
| 149 | for grep { $pv_spec[$_] && $pv_spec[$_]{coerce} } 0 .. $#args; | ||||
| 150 | |||||
| 151 | @args = Params::Validate::validate_with( | ||||
| 152 | params => \@args, | ||||
| 153 | spec => \@pv_spec, | ||||
| 154 | called => _caller_name(), | ||||
| 155 | ); | ||||
| 156 | |||||
| 157 | return @args; | ||||
| 158 | } | ||||
| 159 | |||||
| 160 | sub _cache_key { | ||||
| 161 | my $spec = shift; | ||||
| 162 | |||||
| 163 | if ( exists $spec->{MX_PARAMS_VALIDATE_CACHE_KEY} ) { | ||||
| 164 | return delete $spec->{MX_PARAMS_VALIDATE_CACHE_KEY}; | ||||
| 165 | } | ||||
| 166 | else { | ||||
| 167 | return refaddr( caller_cv(2) ); | ||||
| 168 | } | ||||
| 169 | } | ||||
| 170 | |||||
| 171 | sub _convert_to_param_validate_spec { | ||||
| 172 | my ($spec) = @_; | ||||
| 173 | my %pv_spec; | ||||
| 174 | |||||
| 175 | $pv_spec{optional} = $spec->{optional} | ||||
| 176 | if exists $spec->{optional}; | ||||
| 177 | |||||
| 178 | $pv_spec{default} = $spec->{default} | ||||
| 179 | if exists $spec->{default}; | ||||
| 180 | |||||
| 181 | $pv_spec{coerce} = $spec->{coerce} | ||||
| 182 | if exists $spec->{coerce}; | ||||
| 183 | |||||
| 184 | my $constraint; | ||||
| 185 | if ( defined $spec->{isa} ) { | ||||
| 186 | $constraint | ||||
| 187 | = _is_tc( $spec->{isa} ) | ||||
| 188 | || Moose::Util::TypeConstraints::find_or_parse_type_constraint( | ||||
| 189 | $spec->{isa} ) | ||||
| 190 | || class_type( $spec->{isa} ); | ||||
| 191 | } | ||||
| 192 | elsif ( defined $spec->{does} ) { | ||||
| 193 | $constraint | ||||
| 194 | = _is_tc( $spec->{isa} ) | ||||
| 195 | || find_type_constraint( $spec->{does} ) | ||||
| 196 | || role_type( $spec->{does} ); | ||||
| 197 | } | ||||
| 198 | |||||
| 199 | $pv_spec{callbacks} = $spec->{callbacks} | ||||
| 200 | if exists $spec->{callbacks}; | ||||
| 201 | |||||
| 202 | if ($constraint) { | ||||
| 203 | $pv_spec{constraint} = $constraint; | ||||
| 204 | |||||
| 205 | $pv_spec{callbacks} | ||||
| 206 | { 'checking type constraint for ' . $constraint->name } | ||||
| 207 | = sub { $constraint->check( $_[0] ) }; | ||||
| 208 | } | ||||
| 209 | |||||
| 210 | delete $pv_spec{coerce} | ||||
| 211 | unless $pv_spec{constraint} && $pv_spec{constraint}->has_coercion; | ||||
| 212 | |||||
| 213 | return \%pv_spec; | ||||
| 214 | } | ||||
| 215 | |||||
| 216 | sub _is_tc { | ||||
| 217 | my $maybe_tc = shift; | ||||
| 218 | |||||
| 219 | return $maybe_tc | ||||
| 220 | if defined $maybe_tc | ||||
| 221 | && blessed $maybe_tc | ||||
| 222 | && $maybe_tc->isa('Moose::Meta::TypeConstraint'); | ||||
| 223 | } | ||||
| 224 | |||||
| 225 | sub _caller_name { | ||||
| 226 | my $depth = shift || 0; | ||||
| 227 | |||||
| 228 | return ( caller( 2 + $depth ) )[3]; | ||||
| 229 | } | ||||
| 230 | |||||
| 231 | 1 | 20µs | 1; | ||
| 232 | |||||
| 233 | __END__ |