1Class::MethodMaker::hasUhs(e3r)Contributed Perl DocumentCaltaisosn::MethodMaker::hash(3)
2
3
4
6 Class::Method::hash - Create methods for handling a hash value.
7
9 use Class::MethodMaker
10 [ hash => [qw/ x /] ];
11
12 $instance->x; # empty
13 $instance->x(a => 1, b => 2, c => 3);
14 $instance->x_count == 3; # true
15 $instance->x = (b => 5, d => 8); # Note this *replaces* the hash,
16 # not adds to it
17 $instance->x_index('b') == 5; # true
18 $instance->x_exists('c'); # false
19 $instance->x_exists('d'); # true
20
22 Creates methods to handle hash values in an object. For a component
23 named "x", by default creates methods "x", "x_reset", "x_clear",
24 "x_isset", "x_count", "x_index", "x_keys", "x_values", "x_each",
25 "x_exists", "x_delete", "x_set", "x_get".
26
27 Methods available are:
28
29 "*"
30
31 Created by default. This method returns the list of keys and values
32 stored in the slot (they are returned pairwise, i.e., key, value, key,
33 value; as with perl hashes, no order of keys is guaranteed). If any
34 arguments are provided to this method, they replace the current hash
35 contents. In an array context it returns the keys, values as an array
36 and in a scalar context as a hash-reference. Note that this reference
37 is no longer a direct reference to the storage, in contrast to
38 Class::MethodMaker v1. This is to protect encapsulation. See x_ref if
39 you need that functionality (and are prepared to take the associated
40 risk.)
41
42 If a single argument is provided that is an arrayref or hashref, it is
43 expanded and its contents used in place of the existing contents. This
44 is a more efficient passing mechanism for large numbers of values.
45
46 *_reset
47
48 Created by default. Called without an argument, this resets the
49 component as a whole; deleting any associated storage, and returning
50 the component to its default state. Normally, this means that *_isset
51 will return false, and * will return undef. If "-default" is in
52 effect, then the component will be set to the default value, and
53 *_isset will return true. If "-default_ctor" is in effect, then the
54 default subr will be invoked, and its return value used to set the
55 value of the component, and *_isset will return true.
56
57 If called with arguments, these arguments are treated as indexes into
58 the component, and the individual elements thus referenced are reset
59 (their storage deleted, so that *_isset(n) will return false for
60 appropriate n, except where "-default" or "-default_ctor" are in force,
61 as above). As with perl arrays, resetting the highest set value
62 implicitly decreases the count (but x_reset(n) never unsets the
63 aggregate itself, even if all the elements are not set).
64
65 *_clear
66
67 Created by default. Empty the component of all elements, but without
68 deleting the storage itself.
69
70 If given a list of keys, then the elements that exist indexed by those
71 keys are set to undef (but not deleted).
72
73 Note the very different semantics: "$x->a_clear('b')" sets the value of
74 "b" in component 'a' to undef (if "b") already exists (so
75 "$x->a_isset('b'))" returns true), but "$x->a_clear()" deletes the
76 element "b" from component 'a' (so "$x->a_isset('b'))" returns false).
77
78 *_isset
79
80 Created by default. Whether the component is currently set. This is
81 different from being defined; initially, the component is not set (and
82 if read, will return undef); it can be set to undef (which is a set
83 value, which also returns undef). Having been set, the only way to
84 unset the component is with *_reset.
85
86 If a default value is in effect, then *_isset will always return true.
87
88 *_isset() tests the component as a whole. *_isset(a) tests the element
89 indexed by a. *_isset(a,b) tests the elements indexed by a, b, and
90 returns the logical conjunction (and) of the tests.
91
92 *_count
93
94 Created by default. Returns the number of elements in this component.
95 This is not affected by presence (or lack) of a "default" (or
96 "default_ctor"). Returns "undef" if whole component not set (as per
97 *_isset).
98
99 *_index
100
101 Created by default. Takes a list of indices, returns a list of the
102 corresponding values.
103
104 If a default (or a default ctor) is in force, then a lookup by index
105 will vivify & set to the default the respective elements (and therefore
106 the aggregate data-structure also, if it's not already).
107
108 *_keys
109
110 Created by default. The known keys, as a list in list context, as an
111 arrayref in scalar context.
112
113 If you're expecting a count of the keys in scalar context, see *_count.
114
115 *_values
116
117 Created by default. The known values, as a list in list context, as an
118 arrayref in scalar context.
119
120 *_each
121
122 Created by default. The next pair of key, value (as a list) from the
123 hash.
124
125 *_exists
126
127 Created by default. Takes any number of arguments, considers each as a
128 key, and determines whether the key exists in the has. Returns the
129 logical conjunction (and).
130
131 *_delete
132
133 Created by default. This operates exactly like *_reset, except that
134 calling this with no args does nothing. This is provided for
135 orthogonality with the Perl "delete" operator, while *_reset is
136 provided for orthogonality with other component types.
137
138 *_set
139
140 %n = $x->h; # (a=>1,b=>2,c=>3) (in some order)
141 $h->h_set(b=>4,d=>7);
142 %n = $h->a; # (a=>1,b=>4,c=>3,d=>7) (in some order)
143
144 Created by default. Takes a list, treated as pairs of index => value;
145 each given index is set to the corresponding value. No return.
146
147 If two arguments are given, of which the first is an arrayref, then it
148 is treated as a list of indices of which the second argument (which
149 must also be an arrayref) are the corresponding values. Thus the
150 following two commands are equivalent:
151
152 $x->a_set(b=>4,d=>7);
153 $x->a_set(['b','d'],[4,7]);
154
155 *_get
156
157 Created by default. Retrieves the value of the component without
158 setting (ignores any arguments passed).
159
160
161
162perl v5.30.0 2019-07-26 Class::MethodMaker::hash(3)