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