← Index
NYTProf Performance Profile   « block view • line view • sub view »
For reply.pl
  Run on Thu Oct 21 22:40:13 2010
Reported on Thu Oct 21 22:44:38 2010

Filename/home/hinrik/perl5/perlbrew/perls/perl-5.13.5/lib/site_perl/5.13.5/Data/OptList.pm
StatementsExecuted 199 statements in 1.28ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1112.33ms3.03msData::OptList::::BEGIN@7Data::OptList::BEGIN@7
1111.21ms1.59msData::OptList::::BEGIN@8Data::OptList::BEGIN@8
832229µs390µsData::OptList::::mkoptData::OptList::mkopt
102199µs160µsData::OptList::::__is_aData::OptList::__is_a (recurses: max depth 1, inclusive time 78µs)
62173µs408µsData::OptList::::mkopt_hashData::OptList::mkopt_hash
11128µs33µsData::OptList::::BEGIN@3Data::OptList::BEGIN@3
11117µs27µsData::OptList::::BEGIN@214Data::OptList::BEGIN@214
11111µs22µsData::OptList::::BEGIN@4Data::OptList::BEGIN@4
1118µs8µsData::OptList::::BEGIN@131Data::OptList::BEGIN@131
1115µs5µsData::OptList::::BEGIN@6Data::OptList::BEGIN@6
0000s0sData::OptList::::__ANON__[:143]Data::OptList::__ANON__[:143]
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1
2package Data::OptList;
3226µs238µs
# spent 33µs (28+5) within Data::OptList::BEGIN@3 which was called: # once (28µs+5µs) by Sub::Exporter::BEGIN@7 at line 3
use strict;
# spent 33µs making 1 call to Data::OptList::BEGIN@3 # spent 5µs making 1 call to strict::import
4224µs234µs
# spent 22µs (11+12) within Data::OptList::BEGIN@4 which was called: # once (11µs+12µs) by Sub::Exporter::BEGIN@7 at line 4
use warnings;
# spent 22µs making 1 call to Data::OptList::BEGIN@4 # spent 12µs making 1 call to warnings::import
5
6220µs15µs
# spent 5µs within Data::OptList::BEGIN@6 which was called: # once (5µs+0s) by Sub::Exporter::BEGIN@7 at line 6
use List::Util ();
# spent 5µs making 1 call to Data::OptList::BEGIN@6
72142µs13.03ms
# spent 3.03ms (2.33+695µs) within Data::OptList::BEGIN@7 which was called: # once (2.33ms+695µs) by Sub::Exporter::BEGIN@7 at line 7
use Params::Util ();
# spent 3.03ms making 1 call to Data::OptList::BEGIN@7
83217µs21.61ms
# spent 1.59ms (1.21+380µs) within Data::OptList::BEGIN@8 which was called: # once (1.21ms+380µs) by Sub::Exporter::BEGIN@7 at line 8
use Sub::Install 0.921 ();
# spent 1.59ms making 1 call to Data::OptList::BEGIN@8 # spent 13µs making 1 call to UNIVERSAL::VERSION
9
10=head1 NAME
11
12Data::OptList - parse and validate simple name/value option pairs
13
14=head1 VERSION
15
16version 0.106
17
18=cut
19
2011µsour $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
51Hashes are great for storing named data, but if you want more than one entry
52for a name, you have to use a list of pairs. Even then, this is really boring
53to write:
54
55 $values = [
56 foo => undef,
57 bar => undef,
58 baz => undef,
59 xyz => { ... },
60 ];
61
62Just 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
69Aaaauuugh! We've saved a little typing, but now it requires thought to read,
70and thinking is even worse than typing... and it's got a bug! It looked right,
71didn't it? Well, the C<< xyz => { ... } >> gets consumed by the map, and we
72don't get the data we wanted.
73
74With Data::OptList, you can do this instead:
75
76 $values = Data::OptList::mkopt([
77 qw(foo bar baz),
78 xyz => { ... },
79 ]);
80
81This works by assuming that any defined scalar is a name and any reference
82following 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
95This produces an array of arrays; the inner arrays are name/value pairs.
96Values will be either "undef" or a reference.
97
98Valid 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
107C<$moniker> is a name describing the data, which will be used in error
108messages.
109
110If C<$require_unique> is true, an error will be thrown if any name is given
111more than once.
112
113C<$must_be> is either a scalar or array of scalars; it defines what kind(s) of
114refs may be values. If an invalid value is found, an exception is thrown. If
115no value is passed for this argument, any reference is valid. If C<$must_be>
116specifies that values must be CODE, HASH, ARRAY, or SCALAR, then Params::Util
117is used to check whether the given value can provide that interface.
118Otherwise, it checks that the given value is an object of the kind.
119
120In other words:
121
122 [ qw(SCALAR HASH Object::Known) ]
123
124Means:
125
126 _SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known')
127
128=cut
129
13011µsmy %test_for;
131
# spent 8µs within Data::OptList::BEGIN@131 which was called: # once (8µs+0s) by Sub::Exporter::BEGIN@7 at line 138
BEGIN {
13219µ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 );
1381325µs18µs}
# spent 8µs making 1 call to Data::OptList::BEGIN@131
139
140
# spent 160µs (99+62) within Data::OptList::__is_a which was called 10 times, avg 16µs/call: # 6 times (67µs+-67µs) by List::Util::first at line 143, avg 0s/call # 4 times (32µs+128µs) by Data::OptList::mkopt at line 178, avg 40µs/call
sub __is_a {
14126112µs my ($got, $expected) = @_;
142
143651µs10128µs return List::Util::first { __is_a($got, $_) } @$expected if ref $expected;
# spent 128µs making 4 calls to List::Util::first, avg 32µs/call # spent 78µs making 6 calls to Data::OptList::__is_a, avg 13µs/call, recursion: max depth 1, sum of overlapping time 78µs
144
145 return defined (
146611µs exists($test_for{$expected})
# spent 4µs making 2 calls to Params::Util::_HASHLIKE, avg 2µs/call # spent 4µs making 2 calls to Params::Util::_CODELIKE, avg 2µs/call # spent 3µs making 2 calls to Params::Util::_ARRAYLIKE, avg 2µs/call
147 ? $test_for{$expected}->($got)
148 : Params::Util::_INSTANCE($got, $expected) ## no critic
149 );
150}
151
152
# spent 390µs (229+161) within Data::OptList::mkopt which was called 8 times, avg 49µs/call: # 5 times (174µs+161µs) by Data::OptList::mkopt_hash at line 203, avg 67µs/call # 2 times (34µ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 17µs/call # once (21µs+0s) by Sub::Exporter::_expand_group at line 505 of Sub/Exporter.pm
sub mkopt {
153124239µs my ($opt_list, $moniker, $require_unique, $must_be) = @_;
154
155 return [] unless $opt_list;
156
157 $opt_list = [
158 map { $_ => (ref $opt_list->{$_} ? $opt_list->{$_} : ()) } keys %$opt_list
159 ] if ref $opt_list eq 'HASH';
160
161 my @return;
162 my %seen;
163
164 for (my $i = 0; $i < @$opt_list; $i++) { ## no critic
165 my $name = $opt_list->[$i];
166 my $value;
167
168 if ($require_unique) {
169 Carp::croak "multiple definitions provided for $name" if $seen{$name}++;
170 }
171
172 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 else { $value = undef; }
176
177 if ($must_be and defined $value) {
1784160µs unless (__is_a($value, $must_be)) {
# spent 160µs making 4 calls to Data::OptList::__is_a, avg 40µs/call
179 my $ref = ref $value;
180 Carp::croak "$ref-ref values are not valid in $moniker opt list";
181 }
182 }
183
184 push @return, [ $name => $value ];
185 }
186
187 return \@return;
188}
189
190=head2 mkopt_hash
191
192 my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be);
193
194Given valid C<L</mkopt>> input, this routine returns a reference to a hash. It
195will throw an exception if any name has more than one value.
196
197=cut
198
199
# spent 408µs (73+335) within Data::OptList::mkopt_hash which was called 6 times, avg 68µs/call: # 4 times (49µs+186µs) by Sub::Exporter::_rewrite_build_config at line 672 of Sub/Exporter.pm, avg 59µs/call # 2 times (25µs+149µs) by Sub::Exporter::_rewrite_build_config at line 685 of Sub/Exporter.pm, avg 87µs/call
sub mkopt_hash {
2002773µs my ($opt_list, $moniker, $must_be) = @_;
201 return {} unless $opt_list;
202
2035335µs $opt_list = mkopt($opt_list, $moniker, 1, $must_be);
# spent 335µs making 5 calls to Data::OptList::mkopt, avg 67µs/call
204 my %hash = map { $_->[0] => $_->[1] } @$opt_list;
205 return \%hash;
206}
207
208=head1 EXPORTS
209
210Both C<mkopt> and C<mkopt_hash> may be exported on request.
211
212=cut
213
214
# spent 27µs (17+10) within Data::OptList::BEGIN@214 which was called: # once (17µs+10µs) by Sub::Exporter::BEGIN@7 at line 218
BEGIN {
215115µs110µs *import = Sub::Install::exporter {
# spent 10µs making 1 call to Sub::Install::exporter
216 exports => [qw(mkopt mkopt_hash)],
217 };
218119µs127µs}
# spent 27µs making 1 call to Data::OptList::BEGIN@214
219
220=head1 AUTHOR
221
222Ricardo SIGNES, C<< <rjbs@cpan.org> >>
223
224=head1 BUGS
225
226Please report any bugs or feature requests at L<http://rt.cpan.org>. I will be
227notified, and then you'll automatically be notified of progress on your bug as
228I make changes.
229
230=head1 COPYRIGHT
231
232Copyright 2006-2007, Ricardo SIGNES. This program is free software; you can
233redistribute it and/or modify it under the same terms as Perl itself.
234
235=cut
236
23713µs1;