Manual Pages for Linux CentOS command on man Hash::Util
MyWebUniversity

Manual Pages for Linux CentOS command on man Hash::Util

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

NAME

Hash::Util - A selection of general-utility hash subroutines SYNOPSIS

# Restricted hashes use Hash::Util qw( hashseed allkeys lockkeys unlockkeys lockvalue unlockvalue lockhash unlockhash lockkeysplus hashlocked hiddenkeys legalkeys );

%hash = (foo => 42, bar => 23);

# Ways to restrict a hash

lockkeys(%hash);

lockkeys(%hash, @keyset);

lockkeysplus(%hash, @additionalkeys);

# Ways to inspect the properties of a restricted hash

my @legal = legalkeys(%hash);

my @hidden = hiddenkeys(%hash);

my $ref = allkeys(%hash,@keys,@hidden);

my $islocked = hashlocked(%hash);

# Remove restrictions on the hash

unlockkeys(%hash);

# Lock individual values in a hash

lockvalue (%hash, 'foo');

unlockvalue(%hash, 'foo');

# Ways to change the restrictions on both keys and values

lockhash (%hash);

unlockhash(%hash);

my $hashesarerandomised = hashseed() != 0; DESCRIPTION "Hash::Util" and "Hash::Util::FieldHash" contain special functions for manipulating hashes that don't really warrant a keyword. "Hash::Util" contains a set of functions that support restricted hashes. These are described in this document. "Hash::Util::FieldHash" contains an (unrelated) set of functions that support the use of hashes

in inside-out classes, described in Hash::Util::FieldHash. By default "Hash::Util" does not export anything. Restricted hashes 5.8.0 introduces the ability to restrict a hash to a certain set of keys. No keys outside of this set can be added. It also introduces the ability to lock an individual key so it cannot be deleted and the ability to ensure that an individual value cannot be changed.

This is intended to largely replace the deprecated pseudo-hashes. lockkeys unlockkeys

lockkeys(%hash);

lockkeys(%hash, @keys);

Restricts the given %hash's set of keys to @keys. If @keys is not given it restricts it to its current keyset. No more keys can be added. delete() and exists() will still work, but will not alter the set of allowed keys. Note: the current implementation prevents the hash from being bless()ed while it is in a locked state. Any attempt to do so will raise an exception. Of course you can still bless() the hash before you call lockkeys() so this shouldn't be a problem.

unlockkeys(%hash);

Removes the restriction on the %hash's keyset. Note that if any of the values of the hash have been locked they will not be unlocked after this sub executes. Both routines return a reference to the hash operated on. lockkeysplus

lockkeysplus(%hash,@additionalkeys) Similar to "lockkeys()", with the difference being that the optional key list specifies keys that may or may not be already in the hash. Essentially this is an easier way to say

lockkeys(%hash,@additionalkeys,keys %hash);

Returns a reference to %hash lockvalue unlockvalue

lockvalue (%hash, $key);

unlockvalue(%hash, $key); Locks and unlocks the value for an individual key of a hash. The value of a locked key cannot be changed.

Unless %hash has already been locked the key/value could be deleted regardless of this setting.

Returns a reference to the %hash. lockhash unlockhash

lockhash(%hash);

lockhash() locks an entire hash, making all keys and values read- only. No value can be changed, no keys can be added or deleted.

unlockhash(%hash); unlockhash() does the opposite of lockhash(). All keys and values are made writable. All values can be changed and keys can be added and deleted.

Returns a reference to the %hash. lockhashrecurse unlockhashrecurse

lockhashrecurse(%hash); lockhash() locks an entire hash and any hashes it references

recursively, making all keys and values read-only. No value can be changed, no keys can be added or deleted. Only recurses into hashes that are referenced by another hash. Thus a Hash of Hashes (HoH) will all be restricted, but a Hash of Arrays of Hashes (HoAoH) will only have the top hash restricted.

unlockhashrecurse(%hash); unlockhashrecurse() does the opposite of lockhashrecurse(). All keys and values are made writable. All values can be changed and keys can be added and deleted. Identical recursion restrictions apply as to lockhashrecurse().

Returns a reference to the %hash. hashunlocked

hashunlocked(%hash) and print "Hash is unlocked!\n"; Returns true if the hash and its keys are unlocked. legalkeys

my @keys = legalkeys(%hash); Returns the list of the keys that are legal in a restricted hash. In the case of an unrestricted hash this is identical to calling

keys(%hash). hiddenkeys

my @keys = hiddenkeys(%hash); Returns the list of the keys that are legal in a restricted hash but do not have a value associated to them. Thus if 'foo' is a

"hidden" key of the %hash it will return false for both "defined" and "exists" tests. In the case of an unrestricted hash this will return an empty list. NOTE this is an experimental feature that is heavily dependent on the current implementation of restricted hashes. Should the implementation change, this routine may become meaningless, in which case it will return an empty list. allkeys

allkeys(%hash,@keys,@hidden); Populates the arrays @keys with the all the keys that would pass an "exists" tests, and populates @hidden with the remaining legal keys that have not been utilized. Returns a reference to the hash. In the case of an unrestricted hash this will be equivalent to

$ref = do {

@keys = keys %hash; @hidden = ();

\%hash }; NOTE this is an experimental feature that is heavily dependent on the current implementation of restricted hashes. Should the implementation change this routine may become meaningless in which case it will behave identically to how it would behave on an unrestricted hash. hashseed

my $hashseed = hashseed(); hashseed() returns the seed number used to randomise hash

ordering. Zero means the "traditional" random hash ordering, non- zero means the new even more random hash ordering introduced in Perl 5.8.1. Note that the hash seed is sensitive information: by knowing it one

can craft a denial-of-service attack against Perl code, even remotely, see "Algorithmic Complexity Attacks" in perlsec for more information. Do not disclose the hash seed to people who don't need to know it. See also "PERLHASHSEEDDEBUG" in perlrun. hvstore

my $sv = 0;

hvstore(%hash,$key,$sv) or die "Failed to alias!";

$hash{$key} = 1;

print $sv; # prints 1 Stores an alias to a variable in a hash instead of copying the value. Operating on references to hashes. Most subroutines documented in this module have equivalent versions that operate on references to hashes instead of native hashes. The following is a list of these subs. They are identical except in name

and in that instead of taking a %hash they take a $hashref, and additionally are not prototyped. lockrefkeys unlockrefkeys lockrefkeysplus lockrefvalue unlockrefvalue lockhashref unlockhashref lockhashrefrecurse unlockhashrefrecurse hashrefunlocked legalrefkeys hiddenrefkeys CAVEATS Note that the trapping of the restricted operations is not atomic: for example

eval { %hash = (illegalkey => 1) }

leaves the %hash empty rather than with its original contents. BUGS The interface exposed by this module is very close to the current implementation of restricted hashes. Over time it is expected that this behavior will be extended and the interface abstracted further. AUTHOR

Michael G Schwern on top of code by Nick Ing- Simmons and Jeffrey Friedl. hvstore() is from Array::RefElem, Copyright 2000 Gisle Aas. Additional code by Yves Orton. SEE ALSO Scalar::Util, List::Util and "Algorithmic Complexity Attacks" in perlsec. Hash::Util::FieldHash.

perl v5.16.3 2013-03-04 Hash::Util(3pm)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™