/usr/share/perl5/Hash/WithDefaults.pm is in libhash-withdefaults-perl 0.05-2.
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | package Hash::WithDefaults;
use strict;
use Carp;
require Tie::Hash;
use vars qw(@ISA $VERSION);
@ISA = qw(Tie::StdHash);
$VERSION = '0.05';
sub _DATA () {0}
sub _DEFAULTS () {1}
sub _ACTDEFAULT () {2}
sub _SEEN () {3}
sub _makeTIEHASH {
my ($class, $set) = @_;
$class = 'Hash::WithDefaults::' . $class;
eval "sub ${class}::TIEHASH {" . <<'*END*' . "\t\t\t" . $set . <<'*END*' . "\t\t\t" . $set . <<'*END*';
my $class = shift();
my $data = {};
if (! @_) {
# no parameters
return bless [ $data, []], $class;
}
if (@_ == 1 and ref $_[0] eq 'HASH') {
my $input=$_[0];
my ($key,$value);
while (($key,$value) = each(%$input)) {
*END*
}
} else {
my ($i, $arr) = (0);
if (ref $_[0] eq 'ARRAY') {
$arr = $_[0];
} elsif (@_ % 2 == 0) {
$arr = \@_;
} else {
croak "Ussage: tie %hashname, $class, \%hash\n or tie %hashname, $class, \\\%hash\n or tie %hashname, $class, \\\@array\n";
}
while ($i <= $#$arr) {
my ($key,$value)=($arr->[$i],$arr->[$i+1]); $i+=2;
*END*
}
}
bless [$data, []], $class;
}
*END*
}
_makeTIEHASH 'sensitive', '$data->{$key} = $value;';
_makeTIEHASH 'tolower', '$data->{lc $key} = $value;';
_makeTIEHASH 'toupper', '$data->{uc $key} = $value;';
_makeTIEHASH 'lower', '$data->{lc $key} = $value;';
_makeTIEHASH 'upper', '$data->{uc $key} = $value;';
_makeTIEHASH 'preserve', '$data->{lc $key} = [$key,$value];';
sub TIEHASH {
shift(); # shift out class name
if (@_ == 0) {
# no parameters
unshift @_, 'Hash::WithDefaults::preserve';
goto &Hash::WithDefaults::preserve::TIEHASH;
}
if (!ref $_[0] and (ref $_[1] eq 'HASH' or ref $_[1] eq 'ARRAY' or @_ % 2 == 1)) {
# type plus either \%hash or %hash
my $type = lc(splice(@_, 0, 1));
if ($type =~ /^(?:sensitive|preserve|lower|upper|tolower|toupper)$/) {
unshift @_, 'Hash::WithDefaults::' . $type;
no strict 'refs';
goto &{"Hash::WithDefaults::".$type."::TIEHASH"};
} else {
croak "Unknown type '$type'! Use one of:\n\tsensitive, preserve, lower, upper, tolower, toupper";
}
} else {
unshift @_, 'Hash::WithDefaults::preserve';
goto &Hash::WithDefaults::preserve::TIEHASH;
}
}
sub AddDefault {
push @{$_[0]->[_DEFAULTS]}, $_[1];
return 1;
}
sub GetDefaults {
my $self = shift;
return $self->[_DEFAULTS];
}
sub CLEAR {
my $self = shift;
undef $self->[_DATA];
@{$self->[_DEFAULTS]} = ();
undef $self->[_SEEN];
undef $self->[_ACTDEFAULT];
$self
}
#############################
package Hash::WithDefaults::preserve;
BEGIN {*Hash::WithDefaults::Preserve:: = \%Hash::WithDefaults::preserve::;}
@Hash::WithDefaults::preserve::ISA = qw(Hash::WithDefaults);
sub _DATA () {0}
sub _DEFAULTS () {1}
sub _ACTDEFAULT () {2}
sub _SEEN () {3}
sub STORE {
$_[0]->[_DATA]->{lc $_[1]} = [$_[1],$_[2]];
}
sub FETCH {
my $lc_key = lc $_[1];
return ${$_[0]->[_DATA]->{$lc_key}}[1]
if exists $_[0]->[_DATA]->{$lc_key};
foreach my $default (@{$_[0]->[_DEFAULTS]}) {
return $default->{$_[1]}
if exists($default->{$_[1]});
}
return;
}
sub EXISTS {
return 1
if exists $_[0]->[_DATA]->{lc $_[1]};
foreach my $default (@{$_[0]->[_DEFAULTS]}) {
return 1
if exists($default->{$_[1]});
}
return;
}
sub DELETE {
delete $_[0]->[_DATA]->{lc $_[1]}
}
sub FIRSTKEY {
my $self = $_[0];
undef $self->[_ACTDEFAULT];
$self->[_SEEN] = {};
keys %{$self->[_DATA]};
my ($key,$val);
if (($key,$val) = each %{$self->[_DATA]}) {
$self->[_SEEN]->{$key}=1;
return wantarray ? ($val->[0], $val->[1]) : $val->[0];
} elsif (@{$self->[_DEFAULTS]}) {
return $self->NEXTKEY();
} else {
return;
}
}
sub NEXTKEY {
my $self = $_[0];
my $seen = $self->[_SEEN];
my ($key,$val);
if (!defined $self->[_ACTDEFAULT]) {
# processing the base hash
if (($key,$val) = each %{$self->[_DATA]}) {
$seen->{$key}=1;
return wantarray ? ($val->[0], $val->[1]) : $val->[0];
} else {
# base hash done
if (! @{$self->[_DEFAULTS]}) {
# no defaults
return;
} else {
$self->[_ACTDEFAULT]=0;
# reset the first default
keys %{$self->[_DEFAULTS]->[0]};
}
}
}
while (exists $self->[_DEFAULTS]->[$self->[_ACTDEFAULT]]) {
while (($key,$val) = each %{$self->[_DEFAULTS]->[$self->[_ACTDEFAULT]]}) {
return wantarray ? ($key, $val) : $key
unless $seen->{lc $key}++;
}
$self->[_ACTDEFAULT]++;
keys %{$self->[_DEFAULTS]->[$self->[_ACTDEFAULT]]}
if exists $self->[_DEFAULTS]->[$self->[_ACTDEFAULT]];
}
# all hashes done. Cleanup
undef $self->[_SEEN];
undef $self->[_ACTDEFAULT];
return;
}
#############################
package Hash::WithDefaults::lower;
BEGIN {*Hash::WithDefaults::Lower:: = \%Hash::WithDefaults::lower::;}
@Hash::WithDefaults::lower::ISA = qw(Hash::WithDefaults::preserve);
sub _DATA () {0}
sub _DEFAULTS () {1}
sub _ACTDEFAULT () {2}
sub _SEEN () {3}
sub STORE {
$_[0]->[_DATA]->{lc $_[1]} = $_[2];
}
sub FETCH {
return $_[0]->[_DATA]->{lc $_[1]}
if exists $_[0]->[_DATA]->{lc $_[1]};
foreach my $default (@{$_[0]->[_DEFAULTS]}) {
return $default->{$_[1]}
if exists($default->{$_[1]});
}
return;
}
sub EXISTS {
return 1
if exists $_[0]->[_DATA]->{lc $_[1]};
foreach my $default (@{$_[0]->[_DEFAULTS]}) {
return 1
if exists($default->{$_[1]});
}
return;
}
sub DELETE {
delete $_[0]->[_DATA]->{lc $_[1]}
}
sub FIRSTKEY {
my $self = $_[0];
$self->[_ACTDEFAULT] = -1;
$self->[_SEEN] = {};
keys %{$self->[_DATA]};
my ($key,$val);
if (($key,$val) = each %{$self->[_DATA]}) {
$self->[_SEEN]->{$key}=1;
return wantarray ? ($key, $val) : $key;
} elsif (@{$self->[_DEFAULTS]}) {
return $self->NEXTKEY();
} else {
return;
}
}
sub NEXTKEY {
my $self = $_[0];
my $seen = $self->[_SEEN];
my $defaults = $self->[_DEFAULTS];
my ($key,$val);
if ($self->[_ACTDEFAULT] == -1) {
# processing the base hash
if (($key,$val) = each %{$self->[_DATA]}) {
$seen->{$key}=1;
return wantarray ? ($key, $val) : $key;
} else {
# base hash done
$self->[_ACTDEFAULT]=0;
if (! @$defaults) {
# no defaults
return;
} else {
# reset the first default
keys %{$defaults->[0]};
}
}
}
while (exists $defaults->[$self->[_ACTDEFAULT]]) {
while (($key,$val) = each %{$defaults->[$self->[_ACTDEFAULT]]}) {
return wantarray ? ($key, $val) : $key
unless $seen->{lc $key}++;
}
$self->[_ACTDEFAULT]++;
keys %{$defaults->[$self->[_ACTDEFAULT]]}
if exists $defaults->[$self->[_ACTDEFAULT]];
}
# all hashes done. Cleanup
undef $self->[_SEEN];
undef $self->[_ACTDEFAULT];
return;
}
#############################
package Hash::WithDefaults::upper;
BEGIN {*Hash::WithDefaults::Upper:: = \%Hash::WithDefaults::upper::;}
@Hash::WithDefaults::upper::ISA = qw(Hash::WithDefaults::preserve);
sub _DATA () {0}
sub _DEFAULTS () {1}
sub _ACTDEFAULT () {2}
sub _SEEN () {3}
sub STORE {
$_[0]->[_DATA]->{uc $_[1]} = $_[2];
}
sub FETCH {
return $_[0]->[_DATA]->{uc $_[1]}
if exists $_[0]->[_DATA]->{uc $_[1]};
foreach my $default (@{$_[0]->[_DEFAULTS]}) {
return $default->{$_[1]}
if exists($default->{$_[1]});
}
return;
}
sub EXISTS {
return 1
if exists $_[0]->[_DATA]->{uc $_[1]};
foreach my $default (@{$_[0]->[_DEFAULTS]}) {
return 1
if exists($default->{$_[1]});
}
return;
}
sub DELETE {
delete $_[0]->[_DATA]->{uc $_[1]}
}
sub FIRSTKEY {
my $self = $_[0];
$self->[_ACTDEFAULT] = -1;
$self->[_SEEN] = {};
keys %{$self->[_DATA]};
my ($key,$val);
if (($key,$val) = each %{$self->[_DATA]}) {
$self->[_SEEN]->{$key}=1;
return wantarray ? ($key, $val) : $key;
} elsif (@{$self->[_DEFAULTS]}) {
return $self->NEXTKEY();
} else {
return;
}
}
sub NEXTKEY {
my $self = $_[0];
my $seen = $self->[_SEEN];
my $defaults = $self->[_DEFAULTS];
my ($key,$val);
if ($self->[_ACTDEFAULT] == -1) {
# processing the base hash
if (($key,$val) = each %{$self->[_DATA]}) {
$seen->{$key}=1;
return wantarray ? ($key, $val) : $key;
} else {
# base hash done
$self->[_ACTDEFAULT]=0;
if (! @$defaults) {
# no defaults
return;
} else {
# reset the first default
keys %{$defaults->[0]};
}
}
}
while (exists $defaults->[$self->[_ACTDEFAULT]]) {
while (($key,$val) = each %{$defaults->[$self->[_ACTDEFAULT]]}) {
return wantarray ? ($key, $val) : $key
unless $seen->{uc $key}++;
}
$self->[_ACTDEFAULT]++;
keys %{$defaults->[$self->[_ACTDEFAULT]]}
if exists $defaults->[$self->[_ACTDEFAULT]];
}
# all hashes done. Cleanup
undef $self->[_SEEN];
undef $self->[_ACTDEFAULT];
return;
}
#############################
package Hash::WithDefaults::sensitive;
BEGIN {*Hash::WithDefaults::Sensitive:: = \%Hash::WithDefaults::sensitive::;}
@Hash::WithDefaults::sensitive::ISA = qw(Hash::WithDefaults);
sub _DATA () {0}
sub _DEFAULTS () {1}
sub _ACTDEFAULT () {2}
sub _SEEN () {3}
sub STORE {
$_[0]->[_DATA]->{$_[1]} = $_[2];
}
sub FETCH {
return $_[0]->[_DATA]->{$_[1]}
if exists $_[0]->[_DATA]->{$_[1]};
foreach my $default (@{$_[0]->[_DEFAULTS]}) {
return $default->{$_[1]}
if exists($default->{$_[1]});
}
return;
}
sub EXISTS {
return 1
if exists $_[0]->[_DATA]->{$_[1]};
foreach my $default (@{$_[0]->[_DEFAULTS]}) {
return 1
if exists($default->{$_[1]});
}
return;
}
sub DELETE {
delete $_[0]->[_DATA]->{$_[1]}
}
sub FIRSTKEY {
my $self = $_[0];
$self->[_ACTDEFAULT] = -1;
$self->[_SEEN] = {};
keys %{$self->[_DATA]};
my ($key,$val);
if (($key,$val) = each %{$self->[_DATA]}) {
$self->[_SEEN]->{$key}=1;
return wantarray ? ($key, $val) : $key;
} elsif (@{$self->[_DEFAULTS]}) {
return $self->NEXTKEY();
} else {
return;
}
}
sub NEXTKEY {
my $self = $_[0];
my $seen = $self->[_SEEN];
my $defaults = $self->[_DEFAULTS];
my ($key,$val);
if ($self->[_ACTDEFAULT] == -1) {
# processing the base hash
if (($key,$val) = each %{$self->[_DATA]}) {
$seen->{$key}=1;
return wantarray ? ($key, $val) : $key;
} else {
# base hash done
$self->[_ACTDEFAULT]=0;
if (! @$defaults) {
# no defaults
return;
} else {
# reset the first default
keys %{$defaults->[0]};
}
}
}
while (exists $defaults->[$self->[_ACTDEFAULT]]) {
while (($key,$val) = each %{$defaults->[$self->[_ACTDEFAULT]]}) {
return wantarray ? ($key, $val) : $key
unless $seen->{$key}++;
}
$self->[_ACTDEFAULT]++;
keys %{$defaults->[$self->[_ACTDEFAULT]]}
if exists $defaults->[$self->[_ACTDEFAULT]];
}
# all hashes done. Cleanup
undef $self->[_SEEN];
undef $self->[_ACTDEFAULT];
return;
}
#############################
package Hash::WithDefaults::toupper;
BEGIN {*Hash::WithDefaults::Toupper:: = \%Hash::WithDefaults::toupper::;}
@Hash::WithDefaults::toupper::ISA = qw(Hash::WithDefaults::sensitive);
sub _DATA () {0}
sub _DEFAULTS () {1}
sub _ACTDEFAULT () {2}
sub _SEEN () {3}
sub STORE {
$_[0]->[_DATA]->{uc $_[1]} = $_[2];
}
#############################
package Hash::WithDefaults::tolower;
BEGIN {*Hash::WithDefaults::Tolower:: = \%Hash::WithDefaults::tolower::;}
@Hash::WithDefaults::tolower::ISA = qw(Hash::WithDefaults::sensitive);
sub _DATA () {0}
sub _DEFAULTS () {1}
sub _ACTDEFAULT () {2}
sub _SEEN () {3}
sub STORE {
$_[0]->[_DATA]->{lc $_[1]} = $_[2];
}
1;
__END__
=head1 NAME
Hash::WithDefaults - class for hashes with key-casing requirements supporting defaults
version 0.05
=head1 SYNOPSIS
use Hash::WithDefaults;
%main = ( ... );
tie %h1, 'Hash::WithDefaults', {...};
tied(%h1)->AddDefault(\%main);
tie %h2, 'Hash::WithDefaults', [...];
tied(%h2)->AddDefault(\%main);
# now if you use $h1{$key}, the value is looked up first
# in %h1, then in %main.
=head1 DESCRIPTION
This module implements hashes that support "defaults". That is you may specify
several more hashes in which the data will be looked up in case it is not found in
the current hash.
=head2 Object creation
tie %hash, 'Hash::WithDefault', [$case_option], [\%values];
tie %hash, 'Hash::WithDefault', [$case_option], [\@values];
tie %hash, 'Hash::WithDefault', [$case_option], [%values];
The optional $case_option may be one of these values:
Sensitive - the hash will be case sensitive
Tolower - the hash will be case sensitive, all keys are made lowercase
Toupper - the hash will be case sensitive, all keys are made uppercase
Preserve - the hash will be case insensitive, the case is preserved
Lower - the hash will be case insensitive, all keys are made lowercase
Upper - the hash will be case insensitive, all keys are made uppercase
If you pass a hash or array reference or an even list of keys and values to the tie() function,
those keys and values will be COPIED to the resulting magical hash!
After you tie() the hash, you use it just like any other hash.
=head2 Functions
=head3 AddDefault
tied(%hash)->AddDefault(\%defaults);
This instructs the object to include the %defaults in the search for values.
After this the value will be looked up first in %hash itself and then in %defaults.
You may keep modifying the %defaults and your changes WILL be visible through %hash!
You may add as many defaults to one Hash::WithDefaults object as you like, they will be searched
in the order you add them.
If you delete a key from the tied hash, it's only deleted from the list of specific keys, the defaults
are never modified through the tied hash. This means that you may get a default value for a key
after you deletethe key from the tied hash!
=head3 GetDefaults
$defaults = tied(%hash)->GetDefaults();
push @$defaults, \%another_default;
Returns a reference to the array that stores the defaults.
You may delete or insert hash references into the array, but make sure you
NEVER EVER insert anything else than a hash reference into the array!
=head2 Config::IniHash example
use Config::IniHash;
$config = ReadIni $inifile, withdefaults => 1, case => 'preserve';
if (exists $config->{':default'}) {
my $default = $config->{':default'};
foreach my $section (keys %$config) {
next if $section =~ /^:/;
tied(%{$config->{$section}})->AddDefault($default)
}
}
And now all normal sections will get the default values from [:default] section ;-)
=head1 AUTHOR
Jan Krynicky <Jenda@Krynicky.cz>
http://Jenda.Krynicky.cz
=head1 COPYRIGHT
Copyright (c) 2002-2009 Jan Krynicky <Jenda@Krynicky.cz>. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
|