Filename | /usr/share/perl5/Data/OptList.pm |
Statements | Executed 17 statements in 2.12ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 2.25ms | 2.96ms | BEGIN@10 | Data::OptList::
1 | 1 | 1 | 2.02ms | 2.22ms | BEGIN@11 | Data::OptList::
1 | 1 | 1 | 12µs | 19µs | BEGIN@100 | Data::OptList::
1 | 1 | 1 | 11µs | 23µs | BEGIN@1 | Class::Load::
1 | 1 | 1 | 7µs | 11µs | BEGIN@2 | Class::Load::
1 | 1 | 1 | 6µs | 6µs | BEGIN@15 | Data::OptList::
1 | 1 | 1 | 4µs | 4µs | BEGIN@9 | Data::OptList::
0 | 0 | 0 | 0s | 0s | __ANON__[:27] | Data::OptList::
0 | 0 | 0 | 0s | 0s | __ANON__[:54] | Data::OptList::
0 | 0 | 0 | 0s | 0s | __is_a | Data::OptList::
0 | 0 | 0 | 0s | 0s | mkopt | Data::OptList::
0 | 0 | 0 | 0s | 0s | mkopt_hash | Data::OptList::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | 2 | 22µs | 2 | 34µs | # spent 23µs (11+12) within Class::Load::BEGIN@1 which was called:
# once (11µs+12µs) by Class::Load::BEGIN@10 at line 1 # spent 23µs making 1 call to Class::Load::BEGIN@1
# spent 12µs making 1 call to strict::import |
2 | 2 | 45µs | 2 | 15µs | # spent 11µs (7+4) within Class::Load::BEGIN@2 which was called:
# once (7µs+4µs) by Class::Load::BEGIN@10 at line 2 # spent 11µs making 1 call to Class::Load::BEGIN@2
# spent 4µs making 1 call to warnings::import |
3 | package Data::OptList; | ||||
4 | { | ||||
5 | 2 | 900ns | $Data::OptList::VERSION = '0.109'; | ||
6 | } | ||||
7 | # ABSTRACT: parse and validate simple name/value option pairs | ||||
8 | |||||
9 | 2 | 20µs | 1 | 4µs | # spent 4µs within Data::OptList::BEGIN@9 which was called:
# once (4µs+0s) by Class::Load::BEGIN@10 at line 9 # spent 4µs making 1 call to Data::OptList::BEGIN@9 |
10 | 2 | 826µs | 1 | 2.96ms | # spent 2.96ms (2.25+714µs) within Data::OptList::BEGIN@10 which was called:
# once (2.25ms+714µs) by Class::Load::BEGIN@10 at line 10 # spent 2.96ms making 1 call to Data::OptList::BEGIN@10 |
11 | 3 | 782µs | 2 | 2.23ms | # spent 2.22ms (2.02+205µs) within Data::OptList::BEGIN@11 which was called:
# once (2.02ms+205µs) by Class::Load::BEGIN@10 at line 11 # spent 2.22ms making 1 call to Data::OptList::BEGIN@11
# spent 9µs making 1 call to UNIVERSAL::VERSION |
12 | |||||
13 | |||||
14 | 1 | 200ns | my %test_for; | ||
15 | # spent 6µs within Data::OptList::BEGIN@15 which was called:
# once (6µs+0s) by Class::Load::BEGIN@10 at line 22 | ||||
16 | 1 | 6µs | %test_for = ( | ||
17 | CODE => \&Params::Util::_CODELIKE, ## no critic | ||||
18 | HASH => \&Params::Util::_HASHLIKE, ## no critic | ||||
19 | ARRAY => \&Params::Util::_ARRAYLIKE, ## no critic | ||||
20 | SCALAR => \&Params::Util::_SCALAR0, ## no critic | ||||
21 | ); | ||||
22 | 1 | 390µs | 1 | 6µs | } # spent 6µs making 1 call to Data::OptList::BEGIN@15 |
23 | |||||
24 | sub __is_a { | ||||
25 | my ($got, $expected) = @_; | ||||
26 | |||||
27 | return List::Util::first { __is_a($got, $_) } @$expected if ref $expected; | ||||
28 | |||||
29 | return defined ( | ||||
30 | exists($test_for{$expected}) | ||||
31 | ? $test_for{$expected}->($got) | ||||
32 | : Params::Util::_INSTANCE($got, $expected) ## no critic | ||||
33 | ); | ||||
34 | } | ||||
35 | |||||
36 | sub mkopt { | ||||
37 | my ($opt_list) = shift; | ||||
38 | |||||
39 | my ($moniker, $require_unique, $must_be); # the old positional args | ||||
40 | my $name_test; | ||||
41 | |||||
42 | if (@_ == 1 and Params::Util::_HASHLIKE($_[0])) { | ||||
43 | my $arg = $_[0]; | ||||
44 | ($moniker, $require_unique, $must_be, $name_test) | ||||
45 | = @$arg{ qw(moniker require_unique must_be name_test) }; | ||||
46 | } else { | ||||
47 | ($moniker, $require_unique, $must_be) = @_; | ||||
48 | } | ||||
49 | |||||
50 | $moniker = 'unnamed' unless defined $moniker; | ||||
51 | |||||
52 | return [] unless $opt_list; | ||||
53 | |||||
54 | $name_test ||= sub { ! ref $_[0] }; | ||||
55 | |||||
56 | $opt_list = [ | ||||
57 | map { $_ => (ref $opt_list->{$_} ? $opt_list->{$_} : ()) } keys %$opt_list | ||||
58 | ] if ref $opt_list eq 'HASH'; | ||||
59 | |||||
60 | my @return; | ||||
61 | my %seen; | ||||
62 | |||||
63 | for (my $i = 0; $i < @$opt_list; $i++) { ## no critic | ||||
64 | my $name = $opt_list->[$i]; | ||||
65 | my $value; | ||||
66 | |||||
67 | if ($require_unique) { | ||||
68 | Carp::croak "multiple definitions provided for $name" if $seen{$name}++; | ||||
69 | } | ||||
70 | |||||
71 | if ($i == $#$opt_list) { $value = undef; } | ||||
72 | elsif (not defined $opt_list->[$i+1]) { $value = undef; $i++ } | ||||
73 | elsif ($name_test->($opt_list->[$i+1])) { $value = undef; } | ||||
74 | else { $value = $opt_list->[++$i] } | ||||
75 | |||||
76 | if ($must_be and defined $value) { | ||||
77 | unless (__is_a($value, $must_be)) { | ||||
78 | my $ref = ref $value; | ||||
79 | Carp::croak "$ref-ref values are not valid in $moniker opt list"; | ||||
80 | } | ||||
81 | } | ||||
82 | |||||
83 | push @return, [ $name => $value ]; | ||||
84 | } | ||||
85 | |||||
86 | return \@return; | ||||
87 | } | ||||
88 | |||||
89 | |||||
90 | sub mkopt_hash { | ||||
91 | my ($opt_list, $moniker, $must_be) = @_; | ||||
92 | return {} unless $opt_list; | ||||
93 | |||||
94 | $opt_list = mkopt($opt_list, $moniker, 1, $must_be); | ||||
95 | my %hash = map { $_->[0] => $_->[1] } @$opt_list; | ||||
96 | return \%hash; | ||||
97 | } | ||||
98 | |||||
99 | |||||
100 | # spent 19µs (12+7) within Data::OptList::BEGIN@100 which was called:
# once (12µs+7µs) by Class::Load::BEGIN@10 at line 104 | ||||
101 | 1 | 5µs | 1 | 7µs | *import = Sub::Install::exporter { # spent 7µs making 1 call to Sub::Install::exporter |
102 | exports => [qw(mkopt mkopt_hash)], | ||||
103 | }; | ||||
104 | 1 | 22µs | 1 | 19µs | } # spent 19µs making 1 call to Data::OptList::BEGIN@100 |
105 | |||||
106 | 1 | 2µs | 1; | ||
107 | |||||
108 | __END__ |