1Hash::WithDefaults(3) User Contributed Perl DocumentationHash::WithDefaults(3)
2
3
4
6 Hash::WithDefaults
7
8 - class for hashes with key-casing requirements supporting defaults
9
10 version 0.05
11
13 use Hash::WithDefaults;
14
15 %main = ( ... );
16 tie %h1, 'Hash::WithDefaults', {...};
17 tied(%h1)->AddDefault(\%main);
18 tie %h2, 'Hash::WithDefaults', [...];
19 tied(%h2)->AddDefault(\%main);
20
21 # now if you use $h1{$key}, the value is looked up first
22 # in %h1, then in %main.
23
25 This module implements hashes that support "defaults". That is you may
26 specify several more hashes in which the data will be looked up in case
27 it is not found in the current hash.
28
29 Object creation
30 tie %hash, 'Hash::WithDefault', [$case_option], [\%values];
31 tie %hash, 'Hash::WithDefault', [$case_option], [\@values];
32 tie %hash, 'Hash::WithDefault', [$case_option], [%values];
33
34 The optional $case_option may be one of these values:
35
36 Sensitive - the hash will be case sensitive
37 Tolower - the hash will be case sensitive, all keys are made lowercase
38 Toupper - the hash will be case sensitive, all keys are made uppercase
39 Preserve - the hash will be case insensitive, the case is preserved
40 Lower - the hash will be case insensitive, all keys are made lowercase
41 Upper - the hash will be case insensitive, all keys are made uppercase
42
43 If you pass a hash or array reference or an even list of keys and
44 values to the tie() function, those keys and values will be COPIED to
45 the resulting magical hash!
46
47 After you tie() the hash, you use it just like any other hash.
48
49 Functions
50 AddDefault
51
52 tied(%hash)->AddDefault(\%defaults);
53
54 This instructs the object to include the %defaults in the search for
55 values. After this the value will be looked up first in %hash itself
56 and then in %defaults.
57
58 You may keep modifying the %defaults and your changes WILL be visible
59 through %hash!
60
61 You may add as many defaults to one Hash::WithDefaults object as you
62 like, they will be searched in the order you add them.
63
64 If you delete a key from the tied hash, it's only deleted from the list
65 of specific keys, the defaults are never modified through the tied
66 hash. This means that you may get a default value for a key after you
67 deletethe key from the tied hash!
68
69 GetDefaults
70
71 $defaults = tied(%hash)->GetDefaults();
72 push @$defaults, \%another_default;
73
74 Returns a reference to the array that stores the defaults. You may
75 delete or insert hash references into the array, but make sure you
76 NEVER EVER insert anything else than a hash reference into the array!
77
78 Config::IniHash example
79 use Config::IniHash;
80 $config = ReadIni $inifile, withdefaults => 1, case => 'preserve';
81
82 if (exists $config->{':default'}) {
83 my $default = $config->{':default'};
84 foreach my $section (keys %$config) {
85 next if $section =~ /^:/;
86 tied(%{$config->{$section}})->AddDefault($default)
87 }
88 }
89
90 And now all normal sections will get the default values from [:default]
91 section ;-)
92
94 Jan Krynicky <Jenda@Krynicky.cz> http://Jenda.Krynicky.cz
95
97 Copyright (c) 2002-2009 Jan Krynicky <Jenda@Krynicky.cz>. All rights
98 reserved.
99
100 This program is free software; you can redistribute it and/or modify it
101 under the same terms as Perl itself.
102
103
104
105perl v5.34.0 2021-07-22 Hash::WithDefaults(3)