Filename | /Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Data/Perl/Role/Collection/Hash.pm |
Statements | Executed 342105 statements in 509ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
75073 | 1 | 1 | 234ms | 5.56s | get | Data::Perl::Role::Collection::Hash::
38798 | 1 | 1 | 211ms | 227ms | kv | Data::Perl::Role::Collection::Hash::
65 | 1 | 1 | 733µs | 7.47ms | set | Data::Perl::Role::Collection::Hash::
29 | 3 | 3 | 157µs | 196µs | values | Data::Perl::Role::Collection::Hash::
1 | 1 | 1 | 31µs | 102µs | BEGIN@5 | Data::Perl::Role::Collection::Hash::
1 | 1 | 1 | 6µs | 110µs | BEGIN@7 | Data::Perl::Role::Collection::Hash::
1 | 1 | 1 | 6µs | 18µs | BEGIN@77 | Data::Perl::Role::Collection::Hash::
1 | 1 | 1 | 6µs | 27µs | BEGIN@8 | Data::Perl::Role::Collection::Hash::
1 | 1 | 1 | 6µs | 15µs | BEGIN@9 | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | _array_class | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | accessor | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | all | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | clear | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | count | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | defined | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | delete | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | exists | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | is_empty | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | keys | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | new | Data::Perl::Role::Collection::Hash::
0 | 0 | 0 | 0s | 0s | shallow_clone | Data::Perl::Role::Collection::Hash::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Data::Perl::Role::Collection::Hash; | ||||
2 | 1 | 0s | $Data::Perl::Role::Collection::Hash::VERSION = '0.002011'; | ||
3 | # ABSTRACT: Wrapping class for Perl's built in hash structure. | ||||
4 | |||||
5 | 3 | 42µs | 3 | 173µs | # spent 102µs (31+71) within Data::Perl::Role::Collection::Hash::BEGIN@5 which was called:
# once (31µs+71µs) by Role::Tiny::_load_module at line 5 # spent 102µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@5
# spent 53µs making 1 call to strictures::import
# spent 18µs making 1 call to strictures::VERSION |
6 | |||||
7 | 2 | 23µs | 2 | 214µs | # spent 110µs (6+104) within Data::Perl::Role::Collection::Hash::BEGIN@7 which was called:
# once (6µs+104µs) by Role::Tiny::_load_module at line 7 # spent 110µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@7
# spent 104µs making 1 call to Role::Tiny::import |
8 | 2 | 17µs | 2 | 48µs | # spent 27µs (6+21) within Data::Perl::Role::Collection::Hash::BEGIN@8 which was called:
# once (6µs+21µs) by Role::Tiny::_load_module at line 8 # spent 27µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@8
# spent 21µs making 1 call to Exporter::import |
9 | 2 | 399µs | 2 | 24µs | # spent 15µs (6+9) within Data::Perl::Role::Collection::Hash::BEGIN@9 which was called:
# once (6µs+9µs) by Role::Tiny::_load_module at line 9 # spent 15µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@9
# spent 9µs making 1 call to Module::Runtime::import |
10 | |||||
11 | sub new { my $cl = shift; bless({ @_ }, $cl) } | ||||
12 | |||||
13 | sub _array_class { 'Data::Perl::Collection::Array' } | ||||
14 | |||||
15 | # spent 5.56s (234ms+5.33) within Data::Perl::Role::Collection::Hash::get which was called 75073 times, avg 74µs/call:
# 75073 times (234ms+5.33s) by Data::Perl::Collection::Hash::MooseLike::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Data/Perl/Collection/Hash/MooseLike.pm:22] at line 14 of Data/Perl/Collection/Hash/MooseLike.pm, avg 74µs/call | ||||
16 | 75073 | 9.31ms | my $self = shift; | ||
17 | |||||
18 | 75073 | 139ms | if (@_ > 1) { | ||
19 | my @res = @{$self}{@_}; | ||||
20 | |||||
21 | blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res; | ||||
22 | } | ||||
23 | else { | ||||
24 | 75073 | 108ms | 47236 | 5.33s | $self->{$_[0]}; # spent 5.33s making 47236 calls to Mojo::URL::__ANON__[Mojo/URL.pm:3], avg 113µs/call |
25 | } | ||||
26 | } | ||||
27 | |||||
28 | # spent 7.47ms (733µs+6.74) within Data::Perl::Role::Collection::Hash::set which was called 65 times, avg 115µs/call:
# 65 times (733µs+6.74ms) by Data::Perl::Collection::Hash::MooseLike::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Data/Perl/Collection/Hash/MooseLike.pm:22] at line 14 of Data/Perl/Collection/Hash/MooseLike.pm, avg 115µs/call | ||||
29 | 65 | 10µs | my $self = shift; | ||
30 | 65 | 198µs | my @keys_idx = grep { ! ($_ % 2) } 0..$#_; | ||
31 | 65 | 40µs | my @values_idx = grep { $_ % 2 } 0..$#_; | ||
32 | |||||
33 | 65 | 155µs | 39 | 3.38ms | @{$self}{@_[@keys_idx]} = @_[@values_idx]; # spent 3.38ms making 39 calls to Mojo::URL::__ANON__[Mojo/URL.pm:3], avg 87µs/call |
34 | |||||
35 | 65 | 89µs | 39 | 3.32ms | my @res = @{$self}{@_[@keys_idx]}; # spent 3.32ms making 39 calls to Mojo::URL::__ANON__[Mojo/URL.pm:3], avg 85µs/call |
36 | |||||
37 | 65 | 264µs | 65 | 32µs | blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res; # spent 32µs making 65 calls to Scalar::Util::blessed, avg 492ns/call |
38 | } | ||||
39 | |||||
40 | sub delete { | ||||
41 | my $self = shift; | ||||
42 | my @res = CORE::delete @{$self}{@_}; | ||||
43 | |||||
44 | blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res; | ||||
45 | } | ||||
46 | |||||
47 | sub keys { | ||||
48 | my ($self) = @_; | ||||
49 | |||||
50 | my @res = keys %{$self}; | ||||
51 | |||||
52 | blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res; | ||||
53 | } | ||||
54 | |||||
55 | sub exists { CORE::exists $_[0]->{$_[1]} } | ||||
56 | |||||
57 | sub defined { CORE::defined $_[0]->{$_[1]} } | ||||
58 | |||||
59 | # spent 196µs (157+39) within Data::Perl::Role::Collection::Hash::values which was called 29 times, avg 7µs/call:
# 16 times (95µs+6µs) by JSON::Schema::Modern::_canonical_resources at line 17 of (eval 340)[Sub/Quote.pm:3], avg 6µs/call
# 12 times (54µs+32µs) by JSON::Schema::Modern::Document::_canonical_resources at line 17 of (eval 295)[Sub/Quote.pm:3], avg 7µs/call
# once (8µs+1µs) by JSON::Schema::Modern::_get_vocabulary_values at line 17 of (eval 358)[Sub/Quote.pm:3] | ||||
60 | 29 | 11µs | my ($self) = @_; | ||
61 | |||||
62 | 29 | 54µs | my @res = CORE::values %{$_[0]}; | ||
63 | |||||
64 | 29 | 150µs | 29 | 39µs | blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res; # spent 39µs making 29 calls to Scalar::Util::blessed, avg 1µs/call |
65 | } | ||||
66 | |||||
67 | # spent 227ms (211+16.2) within Data::Perl::Role::Collection::Hash::kv which was called 38798 times, avg 6µs/call:
# 38798 times (211ms+16.2ms) by JSON::Schema::Modern::Document::resource_pairs at line 17 of (eval 296)[Sub/Quote.pm:3], avg 6µs/call | ||||
68 | 38798 | 13.2ms | my ($self) = @_; | ||
69 | |||||
70 | 38798 | 103ms | my @res = CORE::map { [ $_, $self->{$_} ] } CORE::keys %{$self}; | ||
71 | |||||
72 | 38798 | 135ms | 38798 | 16.2ms | blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res; # spent 16.2ms making 38798 calls to Scalar::Util::blessed, avg 417ns/call |
73 | } | ||||
74 | |||||
75 | |||||
76 | { | ||||
77 | 3 | 215µs | 2 | 30µs | # spent 18µs (6+12) within Data::Perl::Role::Collection::Hash::BEGIN@77 which was called:
# once (6µs+12µs) by Role::Tiny::_load_module at line 77 # spent 18µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@77
# spent 12µs making 1 call to warnings::unimport |
78 | |||||
79 | sub all { | ||||
80 | my ($self) = @_; | ||||
81 | |||||
82 | my @res = CORE::map { $_, $self->{$_} } CORE::keys %{$self}; | ||||
83 | |||||
84 | @res; | ||||
85 | } | ||||
86 | |||||
87 | 1 | 1µs | *elements = *all; | ||
88 | } | ||||
89 | |||||
90 | sub clear { %{$_[0]} = () } | ||||
91 | |||||
92 | sub count { CORE::scalar CORE::keys %{$_[0]} } | ||||
93 | |||||
94 | sub is_empty { CORE::scalar CORE::keys %{$_[0]} ? 0 : 1 } | ||||
95 | |||||
96 | sub accessor { | ||||
97 | if (@_ == 2) { | ||||
98 | $_[0]->{$_[1]}; | ||||
99 | } | ||||
100 | elsif (@_ > 2) { | ||||
101 | $_[0]->{$_[1]} = $_[2]; | ||||
102 | } | ||||
103 | } | ||||
104 | |||||
105 | sub shallow_clone { blessed($_[0]) ? bless({%{$_[0]}}, ref $_[0]) : {%{$_[0]}} } | ||||
106 | |||||
107 | 1 | 3µs | 1; | ||
108 | |||||
109 | =pod | ||||
110 | |||||
111 | =encoding UTF-8 | ||||
112 | |||||
113 | =head1 NAME | ||||
114 | |||||
115 | Data::Perl::Role::Collection::Hash - Wrapping class for Perl's built in hash structure. | ||||
116 | |||||
117 | =head1 VERSION | ||||
118 | |||||
119 | version 0.002011 | ||||
120 | |||||
121 | =head1 SYNOPSIS | ||||
122 | |||||
123 | use Data::Perl qw/hash/; | ||||
124 | |||||
125 | my $hash = hash(a => 1, b => 2); | ||||
126 | |||||
127 | $hash->values; # (1, 2) | ||||
128 | |||||
129 | $hash->set('foo', 'bar'); # (a => 1, b => 2, foo => 'bar') | ||||
130 | |||||
131 | =head1 DESCRIPTION | ||||
132 | |||||
133 | This class provides a wrapper and methods for interacting with a hash. | ||||
134 | All methods that return a list do so via a Data::Perl::Collection::Array | ||||
135 | object. | ||||
136 | |||||
137 | =head1 PROVIDED METHODS | ||||
138 | |||||
139 | =over 4 | ||||
140 | |||||
141 | =item B<new($key, $value, ...)> | ||||
142 | |||||
143 | Given an optional list of keys/values, constructs a new Data::Perl::Collection::Hash | ||||
144 | object initalized with keys/values and returns it. | ||||
145 | |||||
146 | =item B<get($key, $key2, $key3...)> | ||||
147 | |||||
148 | Returns a list of values in the hash for the given keys. | ||||
149 | |||||
150 | This method requires at least one argument. | ||||
151 | |||||
152 | =item B<set($key =E<gt> $value, $key2 =E<gt> $value2...)> | ||||
153 | |||||
154 | Sets the elements in the hash to the given values. It returns the new values | ||||
155 | set for each key, in the same order as the keys passed to the method. | ||||
156 | |||||
157 | This method requires at least two arguments, and expects an even number of | ||||
158 | arguments. | ||||
159 | |||||
160 | =item B<delete($key, $key2, $key3...)> | ||||
161 | |||||
162 | Removes the elements with the given keys. | ||||
163 | |||||
164 | Returns a list of values in the hash for the deleted keys. | ||||
165 | |||||
166 | =item B<keys> | ||||
167 | |||||
168 | Returns the list of keys in the hash. | ||||
169 | |||||
170 | This method does not accept any arguments. | ||||
171 | |||||
172 | =item B<exists($key)> | ||||
173 | |||||
174 | Returns true if the given key is present in the hash. | ||||
175 | |||||
176 | This method requires a single argument. | ||||
177 | |||||
178 | =item B<defined($key)> | ||||
179 | |||||
180 | Returns true if the value of a given key is defined. | ||||
181 | |||||
182 | This method requires a single argument. | ||||
183 | |||||
184 | =item B<values> | ||||
185 | |||||
186 | Returns the list of values in the hash. | ||||
187 | |||||
188 | This method does not accept any arguments. | ||||
189 | |||||
190 | =item B<kv> | ||||
191 | |||||
192 | Returns the key/value pairs in the hash as an array of array references. | ||||
193 | |||||
194 | for my $pair ( $object->option_pairs ) { | ||||
195 | print "$pair->[0] = $pair->[1]\n"; | ||||
196 | } | ||||
197 | |||||
198 | This method does not accept any arguments. | ||||
199 | |||||
200 | =item B<elements/all> | ||||
201 | |||||
202 | Returns the key/value pairs in the hash as a flattened list.. | ||||
203 | |||||
204 | This method does not accept any arguments. | ||||
205 | |||||
206 | =item B<clear> | ||||
207 | |||||
208 | Resets the hash to an empty value, like C<%hash = ()>. | ||||
209 | |||||
210 | This method does not accept any arguments. | ||||
211 | |||||
212 | =item B<count> | ||||
213 | |||||
214 | Returns the number of elements in the hash. Also useful for not empty: | ||||
215 | C<< has_options => 'count' >>. | ||||
216 | |||||
217 | This method does not accept any arguments. | ||||
218 | |||||
219 | =item B<is_empty> | ||||
220 | |||||
221 | If the hash is populated, returns false. Otherwise, returns true. | ||||
222 | |||||
223 | This method does not accept any arguments. | ||||
224 | |||||
225 | =item B<accessor($key)> | ||||
226 | |||||
227 | =item B<accessor($key, $value)> | ||||
228 | |||||
229 | If passed one argument, returns the value of the specified key. If passed two | ||||
230 | arguments, sets the value of the specified key. | ||||
231 | |||||
232 | When called as a setter, this method returns the value that was set. | ||||
233 | |||||
234 | =item B<shallow_clone> | ||||
235 | |||||
236 | This method returns a shallow clone of the hash reference. The return value | ||||
237 | is a reference to a new hash with the same keys and values. It is I<shallow> | ||||
238 | because any values that were references in the original will be the I<same> | ||||
239 | references in the clone. | ||||
240 | |||||
241 | =item B<_array_class> | ||||
242 | |||||
243 | The name of the class which returned lists are instances of; i.e. | ||||
244 | C<< Data::Perl::Collection::Array >>. | ||||
245 | |||||
246 | Subclasses of this class can override this method. | ||||
247 | |||||
248 | =back | ||||
249 | |||||
250 | Note that C<each> is deliberately omitted, due to its stateful interaction | ||||
251 | with the hash iterator. C<keys> or C<kv> are much safer. | ||||
252 | |||||
253 | =head1 SEE ALSO | ||||
254 | |||||
255 | =over 4 | ||||
256 | |||||
257 | =item * L<Data::Perl> | ||||
258 | |||||
259 | =item * L<MooX::HandlesVia> | ||||
260 | |||||
261 | =back | ||||
262 | |||||
263 | =head1 AUTHOR | ||||
264 | |||||
265 | Matthew Phillips <mattp@cpan.org> | ||||
266 | |||||
267 | =head1 COPYRIGHT AND LICENSE | ||||
268 | |||||
269 | This software is copyright (c) 2020 by Matthew Phillips <mattp@cpan.org>. | ||||
270 | |||||
271 | This is free software; you can redistribute it and/or modify it under | ||||
272 | the same terms as the Perl 5 programming language system itself. | ||||
273 | |||||
274 | =cut | ||||
275 | |||||
276 | __END__ |