Class::MethodMaker::hash(3) User Contributed Perl Documentation Class::MethodMaker::hash(3)

Class::Method::hash - Create methods for handling a hash value.

use Class::MethodMaker
  [ hash => [qw/ x /] ];
$instance->x;                 # empty
$instance->x(a => 1, b => 2, c => 3);
$instance->x_count == 3;      # true
$instance->x = (b => 5, d => 8); # Note this *replaces* the hash,
                                 # not adds to it
$instance->x_index('b') == 5; # true
$instance->x_exists('c');     # false
$instance->x_exists('d');     # true

Creates methods to handle hash values in an object. For a component named "x", by default creates methods "x", "x_reset", "x_clear", "x_isset", "x_count", "x_index", "x_keys", "x_values", "x_each", "x_exists", "x_delete", "x_set", "x_get".

Methods available are:


Created by default. This method returns the list of keys and values stored in the slot (they are returned pairwise, i.e., key, value, key, value; as with perl hashes, no order of keys is guaranteed). If any arguments are provided to this method, they replace the current hash contents. In an array context it returns the keys, values as an array and in a scalar context as a hash-reference. Note that this reference is no longer a direct reference to the storage, in contrast to Class::MethodMaker v1. This is to protect encapsulation. See x_ref if you need that functionality (and are prepared to take the associated risk.)

If a single argument is provided that is an arrayref or hashref, it is expanded and its contents used in place of the existing contents. This is a more efficient passing mechanism for large numbers of values.


Created by default. Called without an argument, this resets the component as a whole; deleting any associated storage, and returning the component to its default state. Normally, this means that *_isset will return false, and * will return undef. If "-default" is in effect, then the component will be set to the default value, and *_isset will return true. If "-default_ctor" is in effect, then the default subr will be invoked, and its return value used to set the value of the component, and *_isset will return true.

If called with arguments, these arguments are treated as indexes into the component, and the individual elements thus referenced are reset (their storage deleted, so that *_isset(n) will return false for appropriate n, except where "-default" or "-default_ctor" are in force, as above). As with perl arrays, resetting the highest set value implicitly decreases the count (but x_reset(n) never unsets the aggregate itself, even if all the elements are not set).


Created by default. Empty the component of all elements, but without deleting the storage itself.

If given a list of keys, then the elements that exist indexed by those keys are set to undef (but not deleted).

Note the very different semantics: "$x->a_clear('b')" sets the value of "b" in component 'a' to undef (if "b") already exists (so "$x->a_isset('b'))" returns true), but "$x->a_clear()" deletes the element "b" from component 'a' (so "$x->a_isset('b'))" returns false).


Created by default. Whether the component is currently set. This is different from being defined; initially, the component is not set (and if read, will return undef); it can be set to undef (which is a set value, which also returns undef). Having been set, the only way to unset the component is with *_reset.

If a default value is in effect, then *_isset will always return true.

*_isset() tests the component as a whole. *_isset(a) tests the element indexed by a. *_isset(a,b) tests the elements indexed by a, b, and returns the logical conjunction (and) of the tests.


Created by default. Returns the number of elements in this component. This is not affected by presence (or lack) of a "default" (or "default_ctor"). Returns "undef" if whole component not set (as per *_isset).


Created by default. Takes a list of indices, returns a list of the corresponding values.

If a default (or a default ctor) is in force, then a lookup by index will vivify & set to the default the respective elements (and therefore the aggregate data-structure also, if it's not already).


Created by default. The known keys, as a list in list context, as an arrayref in scalar context.

If you're expecting a count of the keys in scalar context, see *_count.


Created by default. The known values, as a list in list context, as an arrayref in scalar context.


Created by default. The next pair of key, value (as a list) from the hash.


Created by default. Takes any number of arguments, considers each as a key, and determines whether the key exists in the has. Returns the logical conjunction (and).


Created by default. This operates exactly like *_reset, except that calling this with no args does nothing. This is provided for orthogonality with the Perl "delete" operator, while *_reset is provided for orthogonality with other component types.


%n = $x->h; # (a=>1,b=>2,c=>3) (in some order)
%n = $h->a; # (a=>1,b=>4,c=>3,d=>7) (in some order)

Created by default. Takes a list, treated as pairs of index => value; each given index is set to the corresponding value. No return.

If two arguments are given, of which the first is an arrayref, then it is treated as a list of indices of which the second argument (which must also be an arrayref) are the corresponding values. Thus the following two commands are equivalent:



Created by default. Retrieves the value of the component without setting (ignores any arguments passed).

2023-07-25 perl v5.38.0