Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

hash::withdefaults(3pm) [debian man page]

Hash::WithDefaults(3pm) 				User Contributed Perl Documentation				   Hash::WithDefaults(3pm)

NAME
Hash::WithDefaults - class for hashes with key-casing requirements supporting defaults version 0.05 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. 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. 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. Functions 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! 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! 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 ;-) AUTHOR
Jan Krynicky <Jenda@Krynicky.cz> http://Jenda.Krynicky.cz 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. perl v5.14.2 2012-05-26 Hash::WithDefaults(3pm)

Check Out this Related Man Page

Tie::Hash(3pm)						 Perl Programmers Reference Guide					    Tie::Hash(3pm)

NAME
Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes SYNOPSIS
package NewHash; require Tie::Hash; @ISA = qw(Tie::Hash); sub DELETE { ... } # Provides needed method sub CLEAR { ... } # Overrides inherited method package NewStdHash; require Tie::Hash; @ISA = qw(Tie::StdHash); # All methods provided by default, define only those needing overrides # Accessors access the storage in %{$_[0]}; # TIEHASH should return a reference to the actual storage sub DELETE { ... } package NewExtraHash; require Tie::Hash; @ISA = qw(Tie::ExtraHash); # All methods provided by default, define only those needing overrides # Accessors access the storage in %{$_[0][0]}; # TIEHASH should return an array reference with the first element being # the reference to the actual storage sub DELETE { $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer delete $_[0][0]->{$_[1]}; # $_[0]->SUPER::DELETE($_[1]) } package main; tie %new_hash, 'NewHash'; tie %new_std_hash, 'NewStdHash'; tie %new_extra_hash, 'NewExtraHash', sub {warn "Doing U$_[1]E of $_[2]. "}; DESCRIPTION
This module provides some skeletal methods for hash-tying classes. See perltie for a list of the functions required in order to tie a hash to a package. The basic Tie::Hash package provides a "new" method, as well as methods "TIEHASH", "EXISTS" and "CLEAR". The Tie::StdHash and Tie::ExtraHash packages provide most methods for hashes described in perltie (the exceptions are "UNTIE" and "DESTROY"). They cause tied hashes to behave exactly like standard hashes, and allow for selective overwriting of methods. Tie::Hash grandfathers the "new" method: it is used if "TIEHASH" is not defined in the case a class forgets to include a "TIEHASH" method. For developers wishing to write their own tied hashes, the required methods are briefly defined below. See the perltie section for more detailed descriptive, as well as example code: TIEHASH classname, LIST The method invoked by the command "tie %hash, classname". Associates a new hash instance with the specified class. "LIST" would represent additional arguments (along the lines of AnyDBM_File and compatriots) needed to complete the association. STORE this, key, value Store datum value into key for the tied hash this. FETCH this, key Retrieve the datum in key for the tied hash this. FIRSTKEY this Return the first key in the hash. NEXTKEY this, lastkey Return the next key in the hash. EXISTS this, key Verify that key exists with the tied hash this. The Tie::Hash implementation is a stub that simply croaks. DELETE this, key Delete the key key from the tied hash this. CLEAR this Clear all values from the tied hash this. SCALAR this Returns what evaluating the hash in scalar context yields. Tie::Hash does not implement this method (but Tie::StdHash and Tie::ExtraHash do). Inheriting from Tie::StdHash The accessor methods assume that the actual storage for the data in the tied hash is in the hash referenced by "tied(%tiedhash)". Thus overwritten "TIEHASH" method should return a hash reference, and the remaining methods should operate on the hash referenced by the first argument: package ReportHash; our @ISA = 'Tie::StdHash'; sub TIEHASH { my $storage = bless {}, shift; warn "New ReportHash created, stored in $storage. "; $storage } sub STORE { warn "Storing data with key $_[1] at $_[0]. "; $_[0]{$_[1]} = $_[2] } Inheriting from Tie::ExtraHash The accessor methods assume that the actual storage for the data in the tied hash is in the hash referenced by "(tied(%tiedhash))->[0]". Thus overwritten "TIEHASH" method should return an array reference with the first element being a hash reference, and the remaining methods should operate on the hash "%{ $_[0]->[0] }": package ReportHash; our @ISA = 'Tie::ExtraHash'; sub TIEHASH { my $class = shift; my $storage = bless [{}, @_], $class; warn "New ReportHash created, stored in $storage. "; $storage; } sub STORE { warn "Storing data with key $_[1] at $_[0]. "; $_[0][0]{$_[1]} = $_[2] } The default "TIEHASH" method stores "extra" arguments to tie() starting from offset 1 in the array referenced by "tied(%tiedhash)"; this is the same storage algorithm as in TIEHASH subroutine above. Hence, a typical package inheriting from Tie::ExtraHash does not need to overwrite this method. "SCALAR", "UNTIE" and "DESTROY" The methods "UNTIE" and "DESTROY" are not defined in Tie::Hash, Tie::StdHash, or Tie::ExtraHash. Tied hashes do not require presence of these methods, but if defined, the methods will be called in proper time, see perltie. "SCALAR" is only defined in Tie::StdHash and Tie::ExtraHash. If needed, these methods should be defined by the package inheriting from Tie::Hash, Tie::StdHash, or Tie::ExtraHash. See "SCALAR" in perltie to find out what happens when "SCALAR" does not exist. MORE INFORMATION
The packages relating to various DBM-related implementations (DB_File, NDBM_File, etc.) show examples of general tied hashes, as does the Config module. While these do not utilize Tie::Hash, they serve as good working examples. perl v5.18.2 2014-01-06 Tie::Hash(3pm)
Man Page