| Filename | /home/hinrik/perl5/perlbrew/perls/perl-5.13.5/lib/site_perl/5.13.5/Data/OptList.pm |
| Statements | Executed 483 statements in 1.91ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 3.35ms | 7.15ms | Data::OptList::BEGIN@7 |
| 1 | 1 | 1 | 1.20ms | 1.60ms | Data::OptList::BEGIN@8 |
| 15 | 3 | 2 | 630µs | 903µs | Data::OptList::mkopt |
| 19 | 2 | 1 | 163µs | 272µs | Data::OptList::__is_a (recurses: max depth 1, inclusive time 126µs) |
| 12 | 2 | 1 | 151µs | 911µs | Data::OptList::mkopt_hash |
| 1 | 1 | 1 | 24µs | 29µs | Data::OptList::BEGIN@3 |
| 1 | 1 | 1 | 16µs | 25µs | Data::OptList::BEGIN@4 |
| 1 | 1 | 1 | 12µs | 22µs | Data::OptList::BEGIN@214 |
| 1 | 1 | 1 | 8µs | 8µs | Data::OptList::BEGIN@131 |
| 1 | 1 | 1 | 6µs | 6µs | Data::OptList::BEGIN@6 |
| 0 | 0 | 0 | 0s | 0s | Data::OptList::__ANON__[:143] |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | package Data::OptList; | ||||
| 3 | 2 | 25µs | 2 | 34µs | # spent 29µs (24+5) within Data::OptList::BEGIN@3 which was called:
# once (24µs+5µs) by Sub::Exporter::Util::BEGIN@6 at line 3 # spent 29µs making 1 call to Data::OptList::BEGIN@3
# spent 5µs making 1 call to strict::import |
| 4 | 2 | 24µs | 2 | 33µs | # spent 25µs (16+9) within Data::OptList::BEGIN@4 which was called:
# once (16µs+9µs) by Sub::Exporter::Util::BEGIN@6 at line 4 # spent 25µs making 1 call to Data::OptList::BEGIN@4
# spent 9µs making 1 call to warnings::import |
| 5 | |||||
| 6 | 2 | 20µs | 1 | 6µs | # spent 6µs within Data::OptList::BEGIN@6 which was called:
# once (6µs+0s) by Sub::Exporter::Util::BEGIN@6 at line 6 # spent 6µs making 1 call to Data::OptList::BEGIN@6 |
| 7 | 2 | 136µs | 1 | 7.15ms | # spent 7.15ms (3.35+3.80) within Data::OptList::BEGIN@7 which was called:
# once (3.35ms+3.80ms) by Sub::Exporter::Util::BEGIN@6 at line 7 # spent 7.15ms making 1 call to Data::OptList::BEGIN@7 |
| 8 | 3 | 227µs | 2 | 1.62ms | # spent 1.60ms (1.20+397µs) within Data::OptList::BEGIN@8 which was called:
# once (1.20ms+397µs) by Sub::Exporter::Util::BEGIN@6 at line 8 # spent 1.60ms making 1 call to Data::OptList::BEGIN@8
# spent 17µs making 1 call to UNIVERSAL::VERSION |
| 9 | |||||
| 10 | =head1 NAME | ||||
| 11 | |||||
| 12 | Data::OptList - parse and validate simple name/value option pairs | ||||
| 13 | |||||
| 14 | =head1 VERSION | ||||
| 15 | |||||
| 16 | version 0.106 | ||||
| 17 | |||||
| 18 | =cut | ||||
| 19 | |||||
| 20 | 1 | 1µs | our $VERSION = '0.106'; | ||
| 21 | |||||
| 22 | =head1 SYNOPSIS | ||||
| 23 | |||||
| 24 | use Data::OptList; | ||||
| 25 | |||||
| 26 | my $options = Data::OptList::mkopt([ | ||||
| 27 | qw(key1 key2 key3 key4), | ||||
| 28 | key5 => { ... }, | ||||
| 29 | key6 => [ ... ], | ||||
| 30 | key7 => sub { ... }, | ||||
| 31 | key8 => { ... }, | ||||
| 32 | key8 => [ ... ], | ||||
| 33 | ]); | ||||
| 34 | |||||
| 35 | ...is the same thing, more or less, as: | ||||
| 36 | |||||
| 37 | my $options = [ | ||||
| 38 | [ key1 => undef, ], | ||||
| 39 | [ key2 => undef, ], | ||||
| 40 | [ key3 => undef, ], | ||||
| 41 | [ key4 => undef, ], | ||||
| 42 | [ key5 => { ... }, ], | ||||
| 43 | [ key6 => [ ... ], ], | ||||
| 44 | [ key7 => sub { ... }, ], | ||||
| 45 | [ key8 => { ... }, ], | ||||
| 46 | [ key8 => [ ... ], ], | ||||
| 47 | ]); | ||||
| 48 | |||||
| 49 | =head1 DESCRIPTION | ||||
| 50 | |||||
| 51 | Hashes are great for storing named data, but if you want more than one entry | ||||
| 52 | for a name, you have to use a list of pairs. Even then, this is really boring | ||||
| 53 | to write: | ||||
| 54 | |||||
| 55 | $values = [ | ||||
| 56 | foo => undef, | ||||
| 57 | bar => undef, | ||||
| 58 | baz => undef, | ||||
| 59 | xyz => { ... }, | ||||
| 60 | ]; | ||||
| 61 | |||||
| 62 | Just look at all those undefs! Don't worry, we can get rid of those: | ||||
| 63 | |||||
| 64 | $values = [ | ||||
| 65 | map { $_ => undef } qw(foo bar baz), | ||||
| 66 | xyz => { ... }, | ||||
| 67 | ]; | ||||
| 68 | |||||
| 69 | Aaaauuugh! We've saved a little typing, but now it requires thought to read, | ||||
| 70 | and thinking is even worse than typing... and it's got a bug! It looked right, | ||||
| 71 | didn't it? Well, the C<< xyz => { ... } >> gets consumed by the map, and we | ||||
| 72 | don't get the data we wanted. | ||||
| 73 | |||||
| 74 | With Data::OptList, you can do this instead: | ||||
| 75 | |||||
| 76 | $values = Data::OptList::mkopt([ | ||||
| 77 | qw(foo bar baz), | ||||
| 78 | xyz => { ... }, | ||||
| 79 | ]); | ||||
| 80 | |||||
| 81 | This works by assuming that any defined scalar is a name and any reference | ||||
| 82 | following a name is its value. | ||||
| 83 | |||||
| 84 | =head1 FUNCTIONS | ||||
| 85 | |||||
| 86 | =head2 mkopt | ||||
| 87 | |||||
| 88 | my $opt_list = Data::OptList::mkopt( | ||||
| 89 | $input, | ||||
| 90 | $moniker, | ||||
| 91 | $require_unique, | ||||
| 92 | $must_be, | ||||
| 93 | ); | ||||
| 94 | |||||
| 95 | This produces an array of arrays; the inner arrays are name/value pairs. | ||||
| 96 | Values will be either "undef" or a reference. | ||||
| 97 | |||||
| 98 | Valid values for C<$input>: | ||||
| 99 | |||||
| 100 | undef -> [] | ||||
| 101 | hashref -> [ [ key1 => value1 ] ... ] # non-ref values become undef | ||||
| 102 | arrayref -> every value followed by a ref becomes a pair: [ value => ref ] | ||||
| 103 | every value followed by undef becomes a pair: [ value => undef ] | ||||
| 104 | otherwise, it becomes [ value => undef ] like so: | ||||
| 105 | [ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ] | ||||
| 106 | |||||
| 107 | C<$moniker> is a name describing the data, which will be used in error | ||||
| 108 | messages. | ||||
| 109 | |||||
| 110 | If C<$require_unique> is true, an error will be thrown if any name is given | ||||
| 111 | more than once. | ||||
| 112 | |||||
| 113 | C<$must_be> is either a scalar or array of scalars; it defines what kind(s) of | ||||
| 114 | refs may be values. If an invalid value is found, an exception is thrown. If | ||||
| 115 | no value is passed for this argument, any reference is valid. If C<$must_be> | ||||
| 116 | specifies that values must be CODE, HASH, ARRAY, or SCALAR, then Params::Util | ||||
| 117 | is used to check whether the given value can provide that interface. | ||||
| 118 | Otherwise, it checks that the given value is an object of the kind. | ||||
| 119 | |||||
| 120 | In other words: | ||||
| 121 | |||||
| 122 | [ qw(SCALAR HASH Object::Known) ] | ||||
| 123 | |||||
| 124 | Means: | ||||
| 125 | |||||
| 126 | _SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known') | ||||
| 127 | |||||
| 128 | =cut | ||||
| 129 | |||||
| 130 | 1 | 1µs | my %test_for; | ||
| 131 | # spent 8µs within Data::OptList::BEGIN@131 which was called:
# once (8µs+0s) by Sub::Exporter::Util::BEGIN@6 at line 138 | ||||
| 132 | 1 | 8µs | %test_for = ( | ||
| 133 | CODE => \&Params::Util::_CODELIKE, ## no critic | ||||
| 134 | HASH => \&Params::Util::_HASHLIKE, ## no critic | ||||
| 135 | ARRAY => \&Params::Util::_ARRAYLIKE, ## no critic | ||||
| 136 | SCALAR => \&Params::Util::_SCALAR0, ## no critic | ||||
| 137 | ); | ||||
| 138 | 1 | 353µs | 1 | 8µs | } # spent 8µs making 1 call to Data::OptList::BEGIN@131 |
| 139 | |||||
| 140 | sub __is_a { | ||||
| 141 | 19 | 24µs | my ($got, $expected) = @_; | ||
| 142 | |||||
| 143 | 31 | 146µs | 19 | 215µs | return List::Util::first { __is_a($got, $_) } @$expected if ref $expected; # spent 215µs making 7 calls to List::Util::first, avg 31µs/call
# spent 126µs making 12 calls to Data::OptList::__is_a, avg 10µs/call, recursion: max depth 1, sum of overlapping time 126µs |
| 144 | |||||
| 145 | return defined ( | ||||
| 146 | 12 | 106µs | 12 | 21µs | exists($test_for{$expected}) # spent 8µs making 4 calls to Params::Util::_HASHLIKE, avg 2µs/call
# spent 6µs making 4 calls to Params::Util::_ARRAYLIKE, avg 2µs/call
# spent 5µs making 3 calls to Params::Util::_CODELIKE, avg 2µs/call
# spent 1µs making 1 call to Params::Util::_SCALAR0 |
| 147 | ? $test_for{$expected}->($got) | ||||
| 148 | : Params::Util::_INSTANCE($got, $expected) ## no critic | ||||
| 149 | ); | ||||
| 150 | } | ||||
| 151 | |||||
| 152 | # spent 903µs (630+272) within Data::OptList::mkopt which was called 15 times, avg 60µs/call:
# 8 times (487µs+272µs) by Data::OptList::mkopt_hash at line 203, avg 95µs/call
# 5 times (99µs+0s) by Sub::Exporter::__ANON__[/home/hinrik/perl5/perlbrew/perls/perl-5.13.5/lib/site_perl/5.13.5/Sub/Exporter.pm:756] at line 735 of Sub/Exporter.pm, avg 20µs/call
# 2 times (44µs+0s) by Sub::Exporter::_expand_group at line 505 of Sub/Exporter.pm, avg 22µs/call | ||||
| 153 | 15 | 29µs | my ($opt_list, $moniker, $require_unique, $must_be) = @_; | ||
| 154 | |||||
| 155 | 15 | 14µs | return [] unless $opt_list; | ||
| 156 | |||||
| 157 | $opt_list = [ | ||||
| 158 | 15 | 25µs | map { $_ => (ref $opt_list->{$_} ? $opt_list->{$_} : ()) } keys %$opt_list | ||
| 159 | ] if ref $opt_list eq 'HASH'; | ||||
| 160 | |||||
| 161 | 15 | 14µs | my @return; | ||
| 162 | 15 | 14µs | my %seen; | ||
| 163 | |||||
| 164 | 15 | 108µs | for (my $i = 0; $i < @$opt_list; $i++) { ## no critic | ||
| 165 | 35 | 43µs | my $name = $opt_list->[$i]; | ||
| 166 | 35 | 28µs | my $value; | ||
| 167 | |||||
| 168 | 35 | 49µs | if ($require_unique) { | ||
| 169 | Carp::croak "multiple definitions provided for $name" if $seen{$name}++; | ||||
| 170 | } | ||||
| 171 | |||||
| 172 | 35 | 74µs | if ($i == $#$opt_list) { $value = undef; } | ||
| 173 | elsif (not defined $opt_list->[$i+1]) { $value = undef; $i++ } | ||||
| 174 | elsif (ref $opt_list->[$i+1]) { $value = $opt_list->[++$i] } | ||||
| 175 | 20 | 22µs | else { $value = undef; } | ||
| 176 | |||||
| 177 | 35 | 51µs | if ($must_be and defined $value) { | ||
| 178 | 7 | 26µs | 7 | 272µs | unless (__is_a($value, $must_be)) { # spent 272µs making 7 calls to Data::OptList::__is_a, avg 39µs/call |
| 179 | my $ref = ref $value; | ||||
| 180 | Carp::croak "$ref-ref values are not valid in $moniker opt list"; | ||||
| 181 | } | ||||
| 182 | } | ||||
| 183 | |||||
| 184 | 35 | 63µs | push @return, [ $name => $value ]; | ||
| 185 | 15 | 16µs | } | ||
| 186 | |||||
| 187 | 15 | 74µs | return \@return; | ||
| 188 | } | ||||
| 189 | |||||
| 190 | =head2 mkopt_hash | ||||
| 191 | |||||
| 192 | my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be); | ||||
| 193 | |||||
| 194 | Given valid C<L</mkopt>> input, this routine returns a reference to a hash. It | ||||
| 195 | will throw an exception if any name has more than one value. | ||||
| 196 | |||||
| 197 | =cut | ||||
| 198 | |||||
| 199 | # spent 911µs (151+760) within Data::OptList::mkopt_hash which was called 12 times, avg 76µs/call:
# 8 times (106µs+491µs) by Sub::Exporter::_rewrite_build_config at line 672 of Sub/Exporter.pm, avg 75µs/call
# 4 times (45µs+269µs) by Sub::Exporter::_rewrite_build_config at line 685 of Sub/Exporter.pm, avg 78µs/call | ||||
| 200 | 12 | 20µs | my ($opt_list, $moniker, $must_be) = @_; | ||
| 201 | 12 | 24µs | return {} unless $opt_list; | ||
| 202 | |||||
| 203 | 8 | 29µs | 8 | 760µs | $opt_list = mkopt($opt_list, $moniker, 1, $must_be); # spent 760µs making 8 calls to Data::OptList::mkopt, avg 95µs/call |
| 204 | 8 | 42µs | my %hash = map { $_->[0] => $_->[1] } @$opt_list; | ||
| 205 | 8 | 36µs | return \%hash; | ||
| 206 | } | ||||
| 207 | |||||
| 208 | =head1 EXPORTS | ||||
| 209 | |||||
| 210 | Both C<mkopt> and C<mkopt_hash> may be exported on request. | ||||
| 211 | |||||
| 212 | =cut | ||||
| 213 | |||||
| 214 | # spent 22µs (12+10) within Data::OptList::BEGIN@214 which was called:
# once (12µs+10µs) by Sub::Exporter::Util::BEGIN@6 at line 218 | ||||
| 215 | 1 | 10µs | 1 | 10µs | *import = Sub::Install::exporter { # spent 10µs making 1 call to Sub::Install::exporter |
| 216 | exports => [qw(mkopt mkopt_hash)], | ||||
| 217 | }; | ||||
| 218 | 1 | 19µs | 1 | 22µs | } # spent 22µs making 1 call to Data::OptList::BEGIN@214 |
| 219 | |||||
| 220 | =head1 AUTHOR | ||||
| 221 | |||||
| 222 | Ricardo SIGNES, C<< <rjbs@cpan.org> >> | ||||
| 223 | |||||
| 224 | =head1 BUGS | ||||
| 225 | |||||
| 226 | Please report any bugs or feature requests at L<http://rt.cpan.org>. I will be | ||||
| 227 | notified, and then you'll automatically be notified of progress on your bug as | ||||
| 228 | I make changes. | ||||
| 229 | |||||
| 230 | =head1 COPYRIGHT | ||||
| 231 | |||||
| 232 | Copyright 2006-2007, Ricardo SIGNES. This program is free software; you can | ||||
| 233 | redistribute it and/or modify it under the same terms as Perl itself. | ||||
| 234 | |||||
| 235 | =cut | ||||
| 236 | |||||
| 237 | 1 | 4µs | 1; |