Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Test2::Util::HashBase.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Test2::Util::HashBase 3pm"
61 .TH Test2::Util::HashBase 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Test2::Util::HashBase \- Build hash based classes.
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 A class:
71 .PP
72 .Vb 3
73 \& package My::Class;
74 \& use strict;
75 \& use warnings;
76 \&
77 \& # Generate 3 accessors
78 \& use Test2::Util::HashBase qw/foo \-bar ^baz <bat >ban +boo/;
79 \&
80 \& # Chance to initialize defaults
81 \& sub init {
82 \& my $self = shift; # No other args
83 \& $self\->{+FOO} ||= "foo";
84 \& $self\->{+BAR} ||= "bar";
85 \& $self\->{+BAZ} ||= "baz";
86 \& $self\->{+BAT} ||= "bat";
87 \& $self\->{+BAN} ||= "ban";
88 \& $self\->{+BOO} ||= "boo";
89 \& }
90 \&
91 \& sub print {
92 \& print join ", " => map { $self\->{$_} } FOO, BAR, BAZ, BAT, BAN, BOO;
93 \& }
94 .Ve
95 .PP
96 Subclass it
97 .PP
98 .Vb 3
99 \& package My::Subclass;
100 \& use strict;
101 \& use warnings;
102 \&
103 \& # Note, you should subclass before loading HashBase.
104 \& use base \*(AqMy::Class\*(Aq;
105 \& use Test2::Util::HashBase qw/bub/;
106 \&
107 \& sub init {
108 \& my $self = shift;
109 \&
110 \& # We get the constants from the base class for free.
111 \& $self\->{+FOO} ||= \*(AqSubFoo\*(Aq;
112 \& $self\->{+BUB} ||= \*(Aqbub\*(Aq;
113 \&
114 \& $self\->SUPER::init();
115 \& }
116 .Ve
117 .PP
118 use it:
119 .PP
120 .Vb 4
121 \& package main;
122 \& use strict;
123 \& use warnings;
124 \& use My::Class;
125 \&
126 \& # These are all functionally identical
127 \& my $one = My::Class\->new(foo => \*(AqMyFoo\*(Aq, bar => \*(AqMyBar\*(Aq);
128 \& my $two = My::Class\->new({foo => \*(AqMyFoo\*(Aq, bar => \*(AqMyBar\*(Aq});
129 \& my $three = My::Class\->new([\*(AqMyFoo\*(Aq, \*(AqMyBar\*(Aq]);
130 \&
131 \& # Readers!
132 \& my $foo = $one\->foo; # \*(AqMyFoo\*(Aq
133 \& my $bar = $one\->bar; # \*(AqMyBar\*(Aq
134 \& my $baz = $one\->baz; # Defaulted to: \*(Aqbaz\*(Aq
135 \& my $bat = $one\->bat; # Defaulted to: \*(Aqbat\*(Aq
136 \& # \*(Aq>ban\*(Aq means setter only, no reader
137 \& # \*(Aq+boo\*(Aq means no setter or reader, just the BOO constant
138 \&
139 \& # Setters!
140 \& $one\->set_foo(\*(AqA Foo\*(Aq);
141 \&
142 \& #\*(Aq\-bar\*(Aq means read\-only, so the setter will throw an exception (but is defined).
143 \& $one\->set_bar(\*(AqA bar\*(Aq);
144 \&
145 \& # \*(Aq^baz\*(Aq means deprecated setter, this will warn about the setter being
146 \& # deprecated.
147 \& $one\->set_baz(\*(AqA Baz\*(Aq);
148 \&
149 \& # \*(Aq<bat\*(Aq means no setter defined at all
150 \& # \*(Aq+boo\*(Aq means no setter or reader, just the BOO constant
151 \&
152 \& $one\->{+FOO} = \*(Aqxxx\*(Aq;
153 .Ve
154 .SH DESCRIPTION
155 .IX Header "DESCRIPTION"
156 This package is used to generate classes based on hashrefs. Using this class
157 will give you a \f(CWnew()\fR method, as well as generating accessors you request.
158 Generated accessors will be getters, \f(CW\*(C`set_ACCESSOR\*(C'\fR setters will also be
159 generated for you. You also get constants for each accessor (all caps) which
160 return the key into the hash for that accessor. Single inheritance is also
161 supported.
162 .SH "THIS IS A BUNDLED COPY OF HASHBASE"
163 .IX Header "THIS IS A BUNDLED COPY OF HASHBASE"
164 This is a bundled copy of Object::HashBase. This file was generated using
165 the
166 \&\f(CW\*(C`/home/exodist/perl5/perlbrew/perls/main/bin/hashbase_inc.pl\*(C'\fR
167 script.
168 .SH METHODS
169 .IX Header "METHODS"
170 .SS "PROVIDED BY HASH BASE"
171 .IX Subsection "PROVIDED BY HASH BASE"
172 .ie n .IP "$it = $class\->new(%PAIRS)" 4
173 .el .IP "\f(CW$it\fR = \f(CW$class\fR\->new(%PAIRS)" 4
174 .IX Item "$it = $class->new(%PAIRS)"
175 .PD 0
176 .ie n .IP "$it = $class\->new(\e%PAIRS)" 4
177 .el .IP "\f(CW$it\fR = \f(CW$class\fR\->new(\e%PAIRS)" 4
178 .IX Item "$it = $class->new(%PAIRS)"
179 .ie n .IP "$it = $class\->new(\e@ORDERED_VALUES)" 4
180 .el .IP "\f(CW$it\fR = \f(CW$class\fR\->new(\e@ORDERED_VALUES)" 4
181 .IX Item "$it = $class->new(@ORDERED_VALUES)"
182 .PD
183 Create a new instance.
184 .Sp
185 HashBase will not export \f(CWnew()\fR if there is already a \f(CWnew()\fR method in your
186 packages inheritance chain.
187 .Sp
188 \&\fBIf you do not want this method you can define your own\fR you just have to
189 declare it before loading Test2::Util::HashBase.
190 .Sp
191 .Vb 1
192 \& package My::Package;
193 \&
194 \& # predeclare new() so that HashBase does not give us one.
195 \& sub new;
196 \&
197 \& use Test2::Util::HashBase qw/foo bar baz/;
198 \&
199 \& # Now we define our own new method.
200 \& sub new { ... }
201 .Ve
202 .Sp
203 This makes it so that HashBase sees that you have your own \f(CWnew()\fR method.
204 Alternatively you can define the method before loading HashBase instead of just
205 declaring it, but that scatters your use statements.
206 .Sp
207 The most common way to create an object is to pass in key/value pairs where
208 each key is an attribute and each value is what you want assigned to that
209 attribute. No checking is done to verify the attributes or values are valid,
210 you may do that in \f(CWinit()\fR if desired.
211 .Sp
212 If you would like, you can pass in a hashref instead of pairs. When you do so
213 the hashref will be copied, and the copy will be returned blessed as an object.
214 There is no way to ask HashBase to bless a specific hashref.
215 .Sp
216 In some cases an object may only have 1 or 2 attributes, in which case a
217 hashref may be too verbose for your liking. In these cases you can pass in an
218 arrayref with only values. The values will be assigned to attributes in the
219 order the attributes were listed. When there is inheritance involved the
220 attributes from parent classes will come before subclasses.
221 .SS HOOKS
222 .IX Subsection "HOOKS"
223 .ie n .IP $self\->\fBinit()\fR 4
224 .el .IP \f(CW$self\fR\->\fBinit()\fR 4
225 .IX Item "$self->init()"
226 This gives you the chance to set some default values to your fields. The only
227 argument is \f(CW$self\fR with its indexes already set from the constructor.
228 .Sp
229 \&\fBNote:\fR Test2::Util::HashBase checks for an init using \f(CW\*(C`$class\->can(\*(Aqinit\*(Aq)\*(C'\fR
230 during construction. It DOES NOT call \f(CWcan()\fR on the created object. Also note
231 that the result of the check is cached, it is only ever checked once, the first
232 time an instance of your class is created. This means that adding an \f(CWinit()\fR
233 method AFTER the first construction will result in it being ignored.
234 .SH ACCESSORS
235 .IX Header "ACCESSORS"
236 .SS READ/WRITE
237 .IX Subsection "READ/WRITE"
238 To generate accessors you list them when using the module:
239 .PP
240 .Vb 1
241 \& use Test2::Util::HashBase qw/foo/;
242 .Ve
243 .PP
244 This will generate the following subs in your namespace:
245 .IP \fBfoo()\fR 4
246 .IX Item "foo()"
247 Getter, used to get the value of the \f(CW\*(C`foo\*(C'\fR field.
248 .IP \fBset_foo()\fR 4
249 .IX Item "set_foo()"
250 Setter, used to set the value of the \f(CW\*(C`foo\*(C'\fR field.
251 .IP \fBFOO()\fR 4
252 .IX Item "FOO()"
253 Constant, returns the field \f(CW\*(C`foo\*(C'\fR\*(Aqs key into the class hashref. Subclasses will
254 also get this function as a constant, not simply a method, that means it is
255 copied into the subclass namespace.
256 .Sp
257 The main reason for using these constants is to help avoid spelling mistakes
258 and similar typos. It will not help you if you forget to prefix the \*(Aq+\*(Aq though.
259 .SS "READ ONLY"
260 .IX Subsection "READ ONLY"
261 .Vb 1
262 \& use Test2::Util::HashBase qw/\-foo/;
263 .Ve
264 .IP \fBset_foo()\fR 4
265 .IX Item "set_foo()"
266 Throws an exception telling you the attribute is read\-only. This is exported to
267 override any active setters for the attribute in a parent class.
268 .SS "DEPRECATED SETTER"
269 .IX Subsection "DEPRECATED SETTER"
270 .Vb 1
271 \& use Test2::Util::HashBase qw/^foo/;
272 .Ve
273 .IP \fBset_foo()\fR 4
274 .IX Item "set_foo()"
275 This will set the value, but it will also warn you that the method is
276 deprecated.
277 .SS "NO SETTER"
278 .IX Subsection "NO SETTER"
279 .Vb 1
280 \& use Test2::Util::HashBase qw/<foo/;
281 .Ve
282 .PP
283 Only gives you a reader, no \f(CW\*(C`set_foo\*(C'\fR method is defined at all.
284 .SS "NO READER"
285 .IX Subsection "NO READER"
286 .Vb 1
287 \& use Test2::Util::HashBase qw/>foo/;
288 .Ve
289 .PP
290 Only gives you a write (\f(CW\*(C`set_foo\*(C'\fR), no \f(CW\*(C`foo\*(C'\fR method is defined at all.
291 .SS "CONSTANT ONLY"
292 .IX Subsection "CONSTANT ONLY"
293 .Vb 1
294 \& use Test2::Util::HashBase qw/+foo/;
295 .Ve
296 .PP
297 This does not create any methods for you, it just adds the \f(CW\*(C`FOO\*(C'\fR constant.
298 .SH SUBCLASSING
299 .IX Header "SUBCLASSING"
300 You can subclass an existing HashBase class.
301 .PP
302 .Vb 2
303 \& use base \*(AqAnother::HashBase::Class\*(Aq;
304 \& use Test2::Util::HashBase qw/foo bar baz/;
305 .Ve
306 .PP
307 The base class is added to \f(CW@ISA\fR for you, and all constants from base classes
308 are added to subclasses automatically.
309 .SH "GETTING A LIST OF ATTRIBUTES FOR A CLASS"
310 .IX Header "GETTING A LIST OF ATTRIBUTES FOR A CLASS"
311 Test2::Util::HashBase provides a function for retrieving a list of attributes for an
312 Test2::Util::HashBase class.
313 .ie n .IP "@list = Test2::Util::HashBase::attr_list($class)" 4
314 .el .IP "\f(CW@list\fR = Test2::Util::HashBase::attr_list($class)" 4
315 .IX Item "@list = Test2::Util::HashBase::attr_list($class)"
316 .PD 0
317 .ie n .IP "@list = $class\->\fBTest2::Util::HashBase::attr_list()\fR" 4
318 .el .IP "\f(CW@list\fR = \f(CW$class\fR\->\fBTest2::Util::HashBase::attr_list()\fR" 4
319 .IX Item "@list = $class->Test2::Util::HashBase::attr_list()"
320 .PD
321 Either form above will work. This will return a list of attributes defined on
322 the object. This list is returned in the attribute definition order, parent
323 class attributes are listed before subclass attributes. Duplicate attributes
324 will be removed before the list is returned.
325 .Sp
326 \&\fBNote:\fR This list is used in the \f(CW\*(C`$class\->new(\e@ARRAY)\*(C'\fR constructor to
327 determine the attribute to which each value will be paired.
328 .SH SOURCE
329 .IX Header "SOURCE"
330 The source code repository for HashBase can be found at
331 <https://github.com/Test\-More/HashBase/>.
332 .SH MAINTAINERS
333 .IX Header "MAINTAINERS"
334 .IP "Chad Granum <[email protected]>" 4
335 .IX Item "Chad Granum <[email protected]>"
336 .SH AUTHORS
337 .IX Header "AUTHORS"
338 .IP "Chad Granum <[email protected]>" 4
339 .IX Item "Chad Granum <[email protected]>"
340 .SH COPYRIGHT
341 .IX Header "COPYRIGHT"
342 Copyright Chad Granum <[email protected]>.
343 .PP
344 This program is free software; you can redistribute it and/or
345 modify it under the same terms as Perl itself.
346 .PP
347 See <https://dev.perl.org/licenses/>