/usr/lib/perl/5.18.2/Hash/Util.pm is in perl-base 5.18.2-2ubuntu1.7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | package Hash::Util;
require 5.007003;
use strict;
use Carp;
use warnings;
no warnings 'uninitialized';
use warnings::register;
use Scalar::Util qw(reftype);
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
fieldhash fieldhashes
all_keys
lock_keys unlock_keys
lock_value unlock_value
lock_hash unlock_hash
lock_keys_plus
hash_locked hash_unlocked
hashref_locked hashref_unlocked
hidden_keys legal_keys
lock_ref_keys unlock_ref_keys
lock_ref_value unlock_ref_value
lock_hashref unlock_hashref
lock_ref_keys_plus
hidden_ref_keys legal_ref_keys
hash_seed hash_value hv_store
bucket_stats bucket_info bucket_array
lock_hash_recurse unlock_hash_recurse
hash_traversal_mask
);
our $VERSION = '0.16';
require XSLoader;
XSLoader::load();
sub import {
my $class = shift;
if ( grep /fieldhash/, @_ ) {
require Hash::Util::FieldHash;
Hash::Util::FieldHash->import(':all'); # for re-export
}
unshift @_, $class;
goto &Exporter::import;
}
sub lock_ref_keys {
my($hash, @keys) = @_;
Internals::hv_clear_placeholders %$hash;
if( @keys ) {
my %keys = map { ($_ => 1) } @keys;
my %original_keys = map { ($_ => 1) } keys %$hash;
foreach my $k (keys %original_keys) {
croak "Hash has key '$k' which is not in the new key set"
unless $keys{$k};
}
foreach my $k (@keys) {
$hash->{$k} = undef unless exists $hash->{$k};
}
Internals::SvREADONLY %$hash, 1;
foreach my $k (@keys) {
delete $hash->{$k} unless $original_keys{$k};
}
}
else {
Internals::SvREADONLY %$hash, 1;
}
return $hash;
}
sub unlock_ref_keys {
my $hash = shift;
Internals::SvREADONLY %$hash, 0;
return $hash;
}
sub lock_keys (\%;@) { lock_ref_keys(@_) }
sub unlock_keys (\%) { unlock_ref_keys(@_) }
sub lock_ref_keys_plus {
my ($hash,@keys) = @_;
my @delete;
Internals::hv_clear_placeholders(%$hash);
foreach my $key (@keys) {
unless (exists($hash->{$key})) {
$hash->{$key}=undef;
push @delete,$key;
}
}
Internals::SvREADONLY(%$hash,1);
delete @{$hash}{@delete};
return $hash
}
sub lock_keys_plus(\%;@) { lock_ref_keys_plus(@_) }
sub lock_ref_value {
my($hash, $key) = @_;
# I'm doubtful about this warning, as it seems not to be true.
# Marking a value in the hash as RO is useful, regardless
# of the status of the hash itself.
carp "Cannot usefully lock values in an unlocked hash"
if !Internals::SvREADONLY(%$hash) && warnings::enabled;
Internals::SvREADONLY $hash->{$key}, 1;
return $hash
}
sub unlock_ref_value {
my($hash, $key) = @_;
Internals::SvREADONLY $hash->{$key}, 0;
return $hash
}
sub lock_value (\%$) { lock_ref_value(@_) }
sub unlock_value (\%$) { unlock_ref_value(@_) }
sub lock_hashref {
my $hash = shift;
lock_ref_keys($hash);
foreach my $value (values %$hash) {
Internals::SvREADONLY($value,1);
}
return $hash;
}
sub unlock_hashref {
my $hash = shift;
foreach my $value (values %$hash) {
Internals::SvREADONLY($value, 0);
}
unlock_ref_keys($hash);
return $hash;
}
sub lock_hash (\%) { lock_hashref(@_) }
sub unlock_hash (\%) { unlock_hashref(@_) }
sub lock_hashref_recurse {
my $hash = shift;
lock_ref_keys($hash);
foreach my $value (values %$hash) {
my $type = reftype($value);
if (defined($type) and $type eq 'HASH') {
lock_hashref_recurse($value);
}
Internals::SvREADONLY($value,1);
}
return $hash
}
sub unlock_hashref_recurse {
my $hash = shift;
foreach my $value (values %$hash) {
my $type = reftype($value);
if (defined($type) and $type eq 'HASH') {
unlock_hashref_recurse($value);
}
Internals::SvREADONLY($value,1);
}
unlock_ref_keys($hash);
return $hash;
}
sub lock_hash_recurse (\%) { lock_hashref_recurse(@_) }
sub unlock_hash_recurse (\%) { unlock_hashref_recurse(@_) }
sub hashref_locked {
my $hash=shift;
Internals::SvREADONLY(%$hash);
}
sub hash_locked(\%) { hashref_locked(@_) }
sub hashref_unlocked {
my $hash=shift;
!Internals::SvREADONLY(%$hash);
}
sub hash_unlocked(\%) { hashref_unlocked(@_) }
sub legal_keys(\%) { legal_ref_keys(@_) }
sub hidden_keys(\%){ hidden_ref_keys(@_) }
sub bucket_stats {
my ($hash) = @_;
my ($keys, $buckets, $used, @length_counts) = bucket_info($hash);
my $sum;
my $score;
for (0 .. $#length_counts) {
$sum += ($length_counts[$_] * $_);
$score += $length_counts[$_] * ( $_ * ($_ + 1 ) / 2 );
}
$score = $score /
(( $keys / (2 * $buckets )) * ( $keys + ( 2 * $buckets ) - 1 ))
if $keys;
my $mean= $sum/$buckets;
$sum= 0;
$sum += ($length_counts[$_] * (($_-$mean)**2)) for 0 .. $#length_counts;
my $stddev= sqrt($sum/$buckets);
return $keys, $buckets, $used, $keys ? ($score, $used/$buckets, ($keys-$used)/$keys, $mean, $stddev, @length_counts) : ();
}
1;
|