| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "Hash::Util 3perl" |
| 58 |
.TH Hash::Util 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
Hash::Util \- A selection of general\-utility hash subroutines |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 1 |
| 68 |
\& # Restricted hashes |
| 69 |
\& |
| 70 |
\& use Hash::Util qw( |
| 71 |
\& fieldhash fieldhashes |
| 72 |
\& |
| 73 |
\& all_keys |
| 74 |
\& lock_keys unlock_keys |
| 75 |
\& lock_value unlock_value |
| 76 |
\& lock_hash unlock_hash |
| 77 |
\& lock_keys_plus |
| 78 |
\& hash_locked hash_unlocked |
| 79 |
\& hashref_locked hashref_unlocked |
| 80 |
\& hidden_keys legal_keys |
| 81 |
\& |
| 82 |
\& lock_ref_keys unlock_ref_keys |
| 83 |
\& lock_ref_value unlock_ref_value |
| 84 |
\& lock_hashref unlock_hashref |
| 85 |
\& lock_ref_keys_plus |
| 86 |
\& hidden_ref_keys legal_ref_keys |
| 87 |
\& |
| 88 |
\& hash_seed hash_value hv_store |
| 89 |
\& bucket_stats bucket_info bucket_array |
| 90 |
\& lock_hash_recurse unlock_hash_recurse |
| 91 |
\& lock_hashref_recurse unlock_hashref_recurse |
| 92 |
\& |
| 93 |
\& hash_traversal_mask |
| 94 |
\& ); |
| 95 |
\& |
| 96 |
\& my %hash = (foo => 42, bar => 23); |
| 97 |
\& # Ways to restrict a hash |
| 98 |
\& lock_keys(%hash); |
| 99 |
\& lock_keys(%hash, @keyset); |
| 100 |
\& lock_keys_plus(%hash, @additional_keys); |
| 101 |
\& |
| 102 |
\& # Ways to inspect the properties of a restricted hash |
| 103 |
\& my @legal = legal_keys(%hash); |
| 104 |
\& my @hidden = hidden_keys(%hash); |
| 105 |
\& my $ref = all_keys(%hash,@keys,@hidden); |
| 106 |
\& my $is_locked = hash_locked(%hash); |
| 107 |
\& |
| 108 |
\& # Remove restrictions on the hash |
| 109 |
\& unlock_keys(%hash); |
| 110 |
\& |
| 111 |
\& # Lock individual values in a hash |
| 112 |
\& lock_value (%hash, \*(Aqfoo\*(Aq); |
| 113 |
\& unlock_value(%hash, \*(Aqfoo\*(Aq); |
| 114 |
\& |
| 115 |
\& # Ways to change the restrictions on both keys and values |
| 116 |
\& lock_hash (%hash); |
| 117 |
\& unlock_hash(%hash); |
| 118 |
\& |
| 119 |
\& my $hashes_are_randomised = hash_seed() !~ /^\e0+$/; |
| 120 |
\& |
| 121 |
\& my $int_hash_value = hash_value( \*(Aqstring\*(Aq ); |
| 122 |
\& |
| 123 |
\& my $mask= hash_traversal_mask(%hash); |
| 124 |
\& |
| 125 |
\& hash_traversal_mask(%hash,1234); |
| 126 |
.Ve |
| 127 |
.SH DESCRIPTION |
| 128 |
.IX Header "DESCRIPTION" |
| 129 |
\&\f(CW\*(C`Hash::Util\*(C'\fR and \f(CW\*(C`Hash::Util::FieldHash\*(C'\fR contain special functions |
| 130 |
for manipulating hashes that don't really warrant a keyword. |
| 131 |
.PP |
| 132 |
\&\f(CW\*(C`Hash::Util\*(C'\fR contains a set of functions that support |
| 133 |
restricted hashes. These are described in |
| 134 |
this document. \f(CW\*(C`Hash::Util::FieldHash\*(C'\fR contains an (unrelated) |
| 135 |
set of functions that support the use of hashes in |
| 136 |
\&\fIinside-out classes\fR, described in Hash::Util::FieldHash. |
| 137 |
.PP |
| 138 |
By default \f(CW\*(C`Hash::Util\*(C'\fR does not export anything. |
| 139 |
.SS "Restricted hashes" |
| 140 |
.IX Subsection "Restricted hashes" |
| 141 |
5.8.0 introduces the ability to restrict a hash to a certain set of |
| 142 |
keys. No keys outside of this set can be added. It also introduces |
| 143 |
the ability to lock an individual key so it cannot be deleted and the |
| 144 |
ability to ensure that an individual value cannot be changed. |
| 145 |
.PP |
| 146 |
This is intended to largely replace the deprecated pseudo-hashes. |
| 147 |
.IP \fBlock_keys\fR 4 |
| 148 |
.IX Item "lock_keys" |
| 149 |
.PD 0 |
| 150 |
.IP \fBunlock_keys\fR 4 |
| 151 |
.IX Item "unlock_keys" |
| 152 |
.PD |
| 153 |
.Vb 2 |
| 154 |
\& lock_keys(%hash); |
| 155 |
\& lock_keys(%hash, @keys); |
| 156 |
.Ve |
| 157 |
.Sp |
| 158 |
Restricts the given \f(CW%hash\fR's set of keys to \f(CW@keys\fR. If \f(CW@keys\fR is not |
| 159 |
given it restricts it to its current keyset. No more keys can be |
| 160 |
added. \fBdelete()\fR and \fBexists()\fR will still work, but will not alter |
| 161 |
the set of allowed keys. \fBNote\fR: the current implementation prevents |
| 162 |
the hash from being \fBbless()\fRed while it is in a locked state. Any attempt |
| 163 |
to do so will raise an exception. Of course you can still \fBbless()\fR |
| 164 |
the hash before you call \fBlock_keys()\fR so this shouldn't be a problem. |
| 165 |
.Sp |
| 166 |
.Vb 1 |
| 167 |
\& unlock_keys(%hash); |
| 168 |
.Ve |
| 169 |
.Sp |
| 170 |
Removes the restriction on the \f(CW%hash\fR's keyset. |
| 171 |
.Sp |
| 172 |
\&\fBNote\fR that if any of the values of the hash have been locked they will not |
| 173 |
be unlocked after this sub executes. |
| 174 |
.Sp |
| 175 |
Both routines return a reference to the hash operated on. |
| 176 |
.IP \fBlock_keys_plus\fR 4 |
| 177 |
.IX Item "lock_keys_plus" |
| 178 |
.Vb 1 |
| 179 |
\& lock_keys_plus(%hash,@additional_keys) |
| 180 |
.Ve |
| 181 |
.Sp |
| 182 |
Similar to \f(CWlock_keys()\fR, with the difference being that the optional key list |
| 183 |
specifies keys that may or may not be already in the hash. Essentially this is |
| 184 |
an easier way to say |
| 185 |
.Sp |
| 186 |
.Vb 1 |
| 187 |
\& lock_keys(%hash,@additional_keys,keys %hash); |
| 188 |
.Ve |
| 189 |
.Sp |
| 190 |
Returns a reference to \f(CW%hash\fR |
| 191 |
.IP \fBlock_value\fR 4 |
| 192 |
.IX Item "lock_value" |
| 193 |
.PD 0 |
| 194 |
.IP \fBunlock_value\fR 4 |
| 195 |
.IX Item "unlock_value" |
| 196 |
.PD |
| 197 |
.Vb 2 |
| 198 |
\& lock_value (%hash, $key); |
| 199 |
\& unlock_value(%hash, $key); |
| 200 |
.Ve |
| 201 |
.Sp |
| 202 |
Locks and unlocks the value for an individual key of a hash. The value of a |
| 203 |
locked key cannot be changed. |
| 204 |
.Sp |
| 205 |
Unless \f(CW%hash\fR has already been locked the key/value could be deleted |
| 206 |
regardless of this setting. |
| 207 |
.Sp |
| 208 |
Returns a reference to the \f(CW%hash\fR. |
| 209 |
.IP \fBlock_hash\fR 4 |
| 210 |
.IX Item "lock_hash" |
| 211 |
.PD 0 |
| 212 |
.IP \fBunlock_hash\fR 4 |
| 213 |
.IX Item "unlock_hash" |
| 214 |
.PD |
| 215 |
.Vb 1 |
| 216 |
\& lock_hash(%hash); |
| 217 |
.Ve |
| 218 |
.Sp |
| 219 |
\&\fBlock_hash()\fR locks an entire hash, making all keys and values read-only. |
| 220 |
No value can be changed, no keys can be added or deleted. |
| 221 |
.Sp |
| 222 |
.Vb 1 |
| 223 |
\& unlock_hash(%hash); |
| 224 |
.Ve |
| 225 |
.Sp |
| 226 |
\&\fBunlock_hash()\fR does the opposite of \fBlock_hash()\fR. All keys and values |
| 227 |
are made writable. All values can be changed and keys can be added |
| 228 |
and deleted. |
| 229 |
.Sp |
| 230 |
Returns a reference to the \f(CW%hash\fR. |
| 231 |
.IP \fBlock_hash_recurse\fR 4 |
| 232 |
.IX Item "lock_hash_recurse" |
| 233 |
.PD 0 |
| 234 |
.IP \fBunlock_hash_recurse\fR 4 |
| 235 |
.IX Item "unlock_hash_recurse" |
| 236 |
.PD |
| 237 |
.Vb 1 |
| 238 |
\& lock_hash_recurse(%hash); |
| 239 |
.Ve |
| 240 |
.Sp |
| 241 |
\&\fBlock_hash()\fR locks an entire hash and any hashes it references recursively, |
| 242 |
making all keys and values read-only. No value can be changed, no keys can |
| 243 |
be added or deleted. |
| 244 |
.Sp |
| 245 |
This method \fBonly\fR recurses into hashes that are referenced by another hash. |
| 246 |
Thus a Hash of Hashes (HoH) will all be restricted, but a Hash of Arrays of |
| 247 |
Hashes (HoAoH) will only have the top hash restricted. |
| 248 |
.Sp |
| 249 |
.Vb 1 |
| 250 |
\& unlock_hash_recurse(%hash); |
| 251 |
.Ve |
| 252 |
.Sp |
| 253 |
\&\fBunlock_hash_recurse()\fR does the opposite of \fBlock_hash_recurse()\fR. All keys and |
| 254 |
values are made writable. All values can be changed and keys can be added |
| 255 |
and deleted. Identical recursion restrictions apply as to \fBlock_hash_recurse()\fR. |
| 256 |
.Sp |
| 257 |
Returns a reference to the \f(CW%hash\fR. |
| 258 |
.IP \fBhashref_locked\fR 4 |
| 259 |
.IX Item "hashref_locked" |
| 260 |
.PD 0 |
| 261 |
.IP \fBhash_locked\fR 4 |
| 262 |
.IX Item "hash_locked" |
| 263 |
.PD |
| 264 |
.Vb 2 |
| 265 |
\& hashref_locked(\e%hash) and print "Hash is locked!\en"; |
| 266 |
\& hash_locked(%hash) and print "Hash is locked!\en"; |
| 267 |
.Ve |
| 268 |
.Sp |
| 269 |
Returns true if the hash and its keys are locked. |
| 270 |
.IP \fBhashref_unlocked\fR 4 |
| 271 |
.IX Item "hashref_unlocked" |
| 272 |
.PD 0 |
| 273 |
.IP \fBhash_unlocked\fR 4 |
| 274 |
.IX Item "hash_unlocked" |
| 275 |
.PD |
| 276 |
.Vb 2 |
| 277 |
\& hashref_unlocked(\e%hash) and print "Hash is unlocked!\en"; |
| 278 |
\& hash_unlocked(%hash) and print "Hash is unlocked!\en"; |
| 279 |
.Ve |
| 280 |
.Sp |
| 281 |
Returns true if the hash and its keys are unlocked. |
| 282 |
.IP \fBlegal_keys\fR 4 |
| 283 |
.IX Item "legal_keys" |
| 284 |
.Vb 1 |
| 285 |
\& my @keys = legal_keys(%hash); |
| 286 |
.Ve |
| 287 |
.Sp |
| 288 |
Returns the list of the keys that are legal in a restricted hash. |
| 289 |
In the case of an unrestricted hash this is identical to calling |
| 290 |
keys(%hash). |
| 291 |
.IP \fBhidden_keys\fR 4 |
| 292 |
.IX Item "hidden_keys" |
| 293 |
.Vb 1 |
| 294 |
\& my @keys = hidden_keys(%hash); |
| 295 |
.Ve |
| 296 |
.Sp |
| 297 |
Returns the list of the keys that are legal in a restricted hash but |
| 298 |
do not have a value associated to them. Thus if 'foo' is a |
| 299 |
"hidden" key of the \f(CW%hash\fR it will return false for both \f(CW\*(C`defined\*(C'\fR |
| 300 |
and \f(CW\*(C`exists\*(C'\fR tests. |
| 301 |
.Sp |
| 302 |
In the case of an unrestricted hash this will return an empty list. |
| 303 |
.Sp |
| 304 |
\&\fBNOTE\fR this is an experimental feature that is heavily dependent |
| 305 |
on the current implementation of restricted hashes. Should the |
| 306 |
implementation change, this routine may become meaningless, in which |
| 307 |
case it will return an empty list. |
| 308 |
.IP \fBall_keys\fR 4 |
| 309 |
.IX Item "all_keys" |
| 310 |
.Vb 1 |
| 311 |
\& all_keys(%hash,@keys,@hidden); |
| 312 |
.Ve |
| 313 |
.Sp |
| 314 |
Populates the arrays \f(CW@keys\fR with the all the keys that would pass |
| 315 |
an \f(CW\*(C`exists\*(C'\fR tests, and populates \f(CW@hidden\fR with the remaining legal |
| 316 |
keys that have not been utilized. |
| 317 |
.Sp |
| 318 |
Returns a reference to the hash. |
| 319 |
.Sp |
| 320 |
In the case of an unrestricted hash this will be equivalent to |
| 321 |
.Sp |
| 322 |
.Vb 5 |
| 323 |
\& $ref = do { |
| 324 |
\& @keys = keys %hash; |
| 325 |
\& @hidden = (); |
| 326 |
\& \e%hash |
| 327 |
\& }; |
| 328 |
.Ve |
| 329 |
.Sp |
| 330 |
\&\fBNOTE\fR this is an experimental feature that is heavily dependent |
| 331 |
on the current implementation of restricted hashes. Should the |
| 332 |
implementation change this routine may become meaningless in which |
| 333 |
case it will behave identically to how it would behave on an |
| 334 |
unrestricted hash. |
| 335 |
.IP \fBhash_seed\fR 4 |
| 336 |
.IX Item "hash_seed" |
| 337 |
.Vb 1 |
| 338 |
\& my $hash_seed = hash_seed(); |
| 339 |
.Ve |
| 340 |
.Sp |
| 341 |
\&\fBhash_seed()\fR returns the seed bytes used to randomise hash ordering. |
| 342 |
.Sp |
| 343 |
\&\fBNote that the hash seed is sensitive information\fR: by knowing it one |
| 344 |
can craft a denial-of-service attack against Perl code, even remotely, |
| 345 |
see "Algorithmic Complexity Attacks" in perlsec for more information. |
| 346 |
\&\fBDo not disclose the hash seed\fR to people who don't need to know it. |
| 347 |
See also "PERL_HASH_SEED_DEBUG" in perlrun. |
| 348 |
.Sp |
| 349 |
Prior to Perl 5.17.6 this function returned a UV, it now returns a string, |
| 350 |
which may be of nearly any size as determined by the hash function your |
| 351 |
Perl has been built with. Possible sizes may be but are not limited to |
| 352 |
4 bytes (for most hash algorithms) and 16 bytes (for siphash). |
| 353 |
.IP \fBhash_value\fR 4 |
| 354 |
.IX Item "hash_value" |
| 355 |
.Vb 2 |
| 356 |
\& my $hash_value = hash_value($string); |
| 357 |
\& my $hash_value = hash_value($string, $seed); |
| 358 |
.Ve |
| 359 |
.Sp |
| 360 |
\&\f(CWhash_value($string)\fR |
| 361 |
returns |
| 362 |
the current perl's internal hash value for a given string. |
| 363 |
\&\f(CW\*(C`hash_value($string, $seed)\*(C'\fR |
| 364 |
returns the hash value as if computed with a different seed. |
| 365 |
If the custom seed is too short, the function errors out. |
| 366 |
The minimum length of the seed is implementation-dependent. |
| 367 |
.Sp |
| 368 |
Returns a 32\-bit integer |
| 369 |
representing the hash value of the string passed in. |
| 370 |
The 1\-parameter value is only reliable |
| 371 |
for the lifetime of the process. |
| 372 |
It may be different |
| 373 |
depending on invocation, environment variables, perl version, |
| 374 |
architectures, and build options. |
| 375 |
.Sp |
| 376 |
\&\fBNote that the hash value of a given string is sensitive information\fR: |
| 377 |
by knowing it one can deduce the hash seed which in turn can allow one to |
| 378 |
craft a denial-of-service attack against Perl code, even remotely, |
| 379 |
see "Algorithmic Complexity Attacks" in perlsec for more information. |
| 380 |
\&\fBDo not disclose the hash value of a string\fR to people who don't need to |
| 381 |
know it. See also "PERL_HASH_SEED_DEBUG" in perlrun. |
| 382 |
.IP \fBbucket_info\fR 4 |
| 383 |
.IX Item "bucket_info" |
| 384 |
Return a set of basic information about a hash. |
| 385 |
.Sp |
| 386 |
.Vb 1 |
| 387 |
\& my ($keys, $buckets, $used, @length_counts)= bucket_info($hash); |
| 388 |
.Ve |
| 389 |
.Sp |
| 390 |
Fields are as follows: |
| 391 |
.Sp |
| 392 |
.Vb 5 |
| 393 |
\& 0: Number of keys in the hash |
| 394 |
\& 1: Number of buckets in the hash |
| 395 |
\& 2: Number of used buckets in the hash |
| 396 |
\& rest : list of counts, Kth element is the number of buckets |
| 397 |
\& with K keys in it. |
| 398 |
.Ve |
| 399 |
.Sp |
| 400 |
See also \fBbucket_stats()\fR and \fBbucket_array()\fR. |
| 401 |
.IP \fBbucket_stats\fR 4 |
| 402 |
.IX Item "bucket_stats" |
| 403 |
Returns a list of statistics about a hash. |
| 404 |
.Sp |
| 405 |
.Vb 3 |
| 406 |
\& my ($keys, $buckets, $used, $quality, $utilization_ratio, |
| 407 |
\& $collision_pct, $mean, $stddev, @length_counts) |
| 408 |
\& = bucket_stats($hashref); |
| 409 |
.Ve |
| 410 |
.Sp |
| 411 |
Fields are as follows: |
| 412 |
.Sp |
| 413 |
.Vb 10 |
| 414 |
\& 0: Number of keys in the hash |
| 415 |
\& 1: Number of buckets in the hash |
| 416 |
\& 2: Number of used buckets in the hash |
| 417 |
\& 3: Hash Quality Score |
| 418 |
\& 4: Percent of buckets used |
| 419 |
\& 5: Percent of keys which are in collision |
| 420 |
\& 6: Mean bucket length of occupied buckets |
| 421 |
\& 7: Standard Deviation of bucket lengths of occupied buckets |
| 422 |
\& rest : list of counts, Kth element is the number of buckets |
| 423 |
\& with K keys in it. |
| 424 |
.Ve |
| 425 |
.Sp |
| 426 |
See also \fBbucket_info()\fR and \fBbucket_array()\fR. |
| 427 |
.Sp |
| 428 |
Note that Hash Quality Score would be 1 for an ideal hash, numbers |
| 429 |
close to and below 1 indicate good hashing, and number significantly |
| 430 |
above indicate a poor score. In practice it should be around 0.95 to 1.05. |
| 431 |
It is defined as: |
| 432 |
.Sp |
| 433 |
.Vb 4 |
| 434 |
\& $score= sum( $count[$length] * ($length * ($length + 1) / 2) ) |
| 435 |
\& / |
| 436 |
\& ( ( $keys / 2 * $buckets ) * |
| 437 |
\& ( $keys + ( 2 * $buckets ) \- 1 ) ) |
| 438 |
.Ve |
| 439 |
.Sp |
| 440 |
The formula is from the Red Dragon book (reformulated to use the data available) |
| 441 |
and is documented at <http://www.strchr.com/hash_functions> |
| 442 |
.IP \fBbucket_array\fR 4 |
| 443 |
.IX Item "bucket_array" |
| 444 |
.Vb 1 |
| 445 |
\& my $array= bucket_array(\e%hash); |
| 446 |
.Ve |
| 447 |
.Sp |
| 448 |
Returns a packed representation of the bucket array associated with a hash. Each element |
| 449 |
of the array is either an integer K, in which case it represents K empty buckets, or |
| 450 |
a reference to another array which contains the keys that are in that bucket. |
| 451 |
.Sp |
| 452 |
\&\fBNote that the information returned by bucket_array is sensitive information\fR: |
| 453 |
by knowing it one can directly attack perl's hash function which in turn may allow |
| 454 |
one to craft a denial-of-service attack against Perl code, even remotely, |
| 455 |
see "Algorithmic Complexity Attacks" in perlsec for more information. |
| 456 |
\&\fBDo not disclose the output of this function\fR to people who don't need to |
| 457 |
know it. See also "PERL_HASH_SEED_DEBUG" in perlrun. This function is provided strictly |
| 458 |
for debugging and diagnostics purposes only, it is hard to imagine a reason why it |
| 459 |
would be used in production code. |
| 460 |
.IP \fBbucket_stats_formatted\fR 4 |
| 461 |
.IX Item "bucket_stats_formatted" |
| 462 |
.Vb 1 |
| 463 |
\& print bucket_stats_formatted($hashref); |
| 464 |
.Ve |
| 465 |
.Sp |
| 466 |
Return a formatted report of the information returned by \fBbucket_stats()\fR. |
| 467 |
An example report looks like this: |
| 468 |
.Sp |
| 469 |
.Vb 12 |
| 470 |
\& Keys: 50 Buckets: 33/64 Quality\-Score: 1.01 (Good) |
| 471 |
\& Utilized Buckets: 51.56% Optimal: 78.12% Keys In Collision: 34.00% |
| 472 |
\& Chain Length \- mean: 1.52 stddev: 0.66 |
| 473 |
\& Buckets 64 [0000000000000000000000000000000111111111111111111122222222222333] |
| 474 |
\& Len 0 Pct: 48.44 [###############################] |
| 475 |
\& Len 1 Pct: 29.69 [###################] |
| 476 |
\& Len 2 Pct: 17.19 [###########] |
| 477 |
\& Len 3 Pct: 4.69 [###] |
| 478 |
\& Keys 50 [11111111111111111111111111111111122222222222222333] |
| 479 |
\& Pos 1 Pct: 66.00 [#################################] |
| 480 |
\& Pos 2 Pct: 28.00 [##############] |
| 481 |
\& Pos 3 Pct: 6.00 [###] |
| 482 |
.Ve |
| 483 |
.Sp |
| 484 |
The first set of stats gives some summary statistical information, |
| 485 |
including the quality score translated into "Good", "Poor" and "Bad", |
| 486 |
(score<=1.05, score<=1.2, score>1.2). See the documentation in |
| 487 |
\&\fBbucket_stats()\fR for more details. |
| 488 |
.Sp |
| 489 |
The two sets of barcharts give stats and a visual indication of performance |
| 490 |
of the hash. |
| 491 |
.Sp |
| 492 |
The first gives data on bucket chain lengths and provides insight on how |
| 493 |
much work a fetch *miss* will take. In this case we have to inspect every item |
| 494 |
in a bucket before we can be sure the item is not in the list. The performance |
| 495 |
for an insert is equivalent to this case, as is a delete where the item |
| 496 |
is not in the hash. |
| 497 |
.Sp |
| 498 |
The second gives data on how many keys are at each depth in the chain, and |
| 499 |
gives an idea of how much work a fetch *hit* will take. The performance for |
| 500 |
an update or delete of an item in the hash is equivalent to this case. |
| 501 |
.Sp |
| 502 |
Note that these statistics are summary only. Actual performance will depend |
| 503 |
on real hit/miss ratios accessing the hash. If you are concerned by hit ratios |
| 504 |
you are recommended to "oversize" your hash by using something like: |
| 505 |
.Sp |
| 506 |
.Vb 1 |
| 507 |
\& keys(%hash)= keys(%hash) << $k; |
| 508 |
.Ve |
| 509 |
.Sp |
| 510 |
With \f(CW$k\fR chosen carefully, and likely to be a small number like 1 or 2. In |
| 511 |
theory the larger the bucket array the less chance of collision. |
| 512 |
.IP \fBhv_store\fR 4 |
| 513 |
.IX Item "hv_store" |
| 514 |
.Vb 4 |
| 515 |
\& my $sv = 0; |
| 516 |
\& hv_store(%hash,$key,$sv) or die "Failed to alias!"; |
| 517 |
\& $hash{$key} = 1; |
| 518 |
\& print $sv; # prints 1 |
| 519 |
.Ve |
| 520 |
.Sp |
| 521 |
Stores an alias to a variable in a hash instead of copying the value. |
| 522 |
.IP \fBhash_traversal_mask\fR 4 |
| 523 |
.IX Item "hash_traversal_mask" |
| 524 |
As of Perl 5.18 every hash has its own hash traversal order, and this order |
| 525 |
changes every time a new element is inserted into the hash. This functionality |
| 526 |
is provided by maintaining an unsigned integer mask (U32) which is xor'ed |
| 527 |
with the actual bucket id during a traversal of the hash buckets using \fBkeys()\fR, |
| 528 |
\&\fBvalues()\fR or \fBeach()\fR. |
| 529 |
.Sp |
| 530 |
You can use this subroutine to get and set the traversal mask for a specific |
| 531 |
hash. Setting the mask ensures that a given hash will produce the same key |
| 532 |
order. \fBNote\fR that this does \fBnot\fR guarantee that \fBtwo\fR hashes will produce |
| 533 |
the same key order for the same hash seed and traversal mask, items that |
| 534 |
collide into one bucket may have different orders regardless of this setting. |
| 535 |
.IP \fBbucket_ratio\fR 4 |
| 536 |
.IX Item "bucket_ratio" |
| 537 |
This function behaves the same way that scalar(%hash) behaved prior to |
| 538 |
Perl 5.25. Specifically if the hash is tied, then it calls the SCALAR tied |
| 539 |
hash method, if untied then if the hash is empty it return 0, otherwise it |
| 540 |
returns a string containing the number of used buckets in the hash, |
| 541 |
followed by a slash, followed by the total number of buckets in the hash. |
| 542 |
.Sp |
| 543 |
.Vb 2 |
| 544 |
\& my %hash=("foo"=>1); |
| 545 |
\& print Hash::Util::bucket_ratio(%hash); # prints "1/8" |
| 546 |
.Ve |
| 547 |
.IP \fBused_buckets\fR 4 |
| 548 |
.IX Item "used_buckets" |
| 549 |
This function returns the count of used buckets in the hash. It is expensive |
| 550 |
to calculate and the value is NOT cached, so avoid use of this function |
| 551 |
in production code. |
| 552 |
.IP \fBnum_buckets\fR 4 |
| 553 |
.IX Item "num_buckets" |
| 554 |
This function returns the total number of buckets the hash holds, or would |
| 555 |
hold if the array were created. (When a hash is freshly created the array |
| 556 |
may not be allocated even though this value will be non-zero.) |
| 557 |
.SS "Operating on references to hashes" |
| 558 |
.IX Subsection "Operating on references to hashes" |
| 559 |
Most subroutines documented in this module have equivalent versions |
| 560 |
that operate on references to hashes instead of native hashes. |
| 561 |
The following is a list of these subs. They are identical except |
| 562 |
in name and in that instead of taking a \f(CW%hash\fR they take a \f(CW$hashref\fR, |
| 563 |
and additionally are not prototyped. |
| 564 |
.IP lock_ref_keys 4 |
| 565 |
.IX Item "lock_ref_keys" |
| 566 |
.PD 0 |
| 567 |
.IP unlock_ref_keys 4 |
| 568 |
.IX Item "unlock_ref_keys" |
| 569 |
.IP lock_ref_keys_plus 4 |
| 570 |
.IX Item "lock_ref_keys_plus" |
| 571 |
.IP lock_ref_value 4 |
| 572 |
.IX Item "lock_ref_value" |
| 573 |
.IP unlock_ref_value 4 |
| 574 |
.IX Item "unlock_ref_value" |
| 575 |
.IP lock_hashref 4 |
| 576 |
.IX Item "lock_hashref" |
| 577 |
.IP unlock_hashref 4 |
| 578 |
.IX Item "unlock_hashref" |
| 579 |
.IP lock_hashref_recurse 4 |
| 580 |
.IX Item "lock_hashref_recurse" |
| 581 |
.IP unlock_hashref_recurse 4 |
| 582 |
.IX Item "unlock_hashref_recurse" |
| 583 |
.IP hash_ref_unlocked 4 |
| 584 |
.IX Item "hash_ref_unlocked" |
| 585 |
.IP legal_ref_keys 4 |
| 586 |
.IX Item "legal_ref_keys" |
| 587 |
.IP hidden_ref_keys 4 |
| 588 |
.IX Item "hidden_ref_keys" |
| 589 |
.PD |
| 590 |
.SH CAVEATS |
| 591 |
.IX Header "CAVEATS" |
| 592 |
Note that the trapping of the restricted operations is not atomic: |
| 593 |
for example |
| 594 |
.PP |
| 595 |
.Vb 1 |
| 596 |
\& eval { %hash = (illegal_key => 1) } |
| 597 |
.Ve |
| 598 |
.PP |
| 599 |
leaves the \f(CW%hash\fR empty rather than with its original contents. |
| 600 |
.SH BUGS |
| 601 |
.IX Header "BUGS" |
| 602 |
The interface exposed by this module is very close to the current |
| 603 |
implementation of restricted hashes. Over time it is expected that |
| 604 |
this behavior will be extended and the interface abstracted further. |
| 605 |
.SH AUTHOR |
| 606 |
.IX Header "AUTHOR" |
| 607 |
Michael G Schwern <[email protected]> on top of code by Nick |
| 608 |
Ing-Simmons and Jeffrey Friedl. |
| 609 |
.PP |
| 610 |
\&\fBhv_store()\fR is from Array::RefElem, Copyright 2000 Gisle Aas. |
| 611 |
.PP |
| 612 |
Additional code by Yves Orton. |
| 613 |
.PP |
| 614 |
Description of \f(CW\*(C`hash_value($string, $seed)\*(C'\fR |
| 615 |
by Christopher Yeleighton <[email protected]> |
| 616 |
.SH "SEE ALSO" |
| 617 |
.IX Header "SEE ALSO" |
| 618 |
Scalar::Util, List::Util and "Algorithmic Complexity Attacks" in perlsec. |
| 619 |
.PP |
| 620 |
Hash::Util::FieldHash. |