Debian Codegraph @

upstream/mageia-cauldron/man3pm/Class::Struct.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 "Class::Struct 3pm"
61 .TH Class::Struct 3pm 2023-12-22 "perl v5.42.0" "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 Class::Struct \- declare struct\-like datatypes as Perl classes
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 5
71 \& use Class::Struct;
72 \& # declare struct, based on array:
73 \& struct( CLASS_NAME => [ ELEMENT_NAME => ELEMENT_TYPE, ... ]);
74 \& # declare struct, based on hash:
75 \& struct( CLASS_NAME => { ELEMENT_NAME => ELEMENT_TYPE, ... });
76 \&
77 \& package CLASS_NAME;
78 \& use Class::Struct;
79 \& # declare struct, based on array, implicit class name:
80 \& struct( ELEMENT_NAME => ELEMENT_TYPE, ... );
81 \&
82 \& # Declare struct at compile time
83 \& use Class::Struct CLASS_NAME => [ELEMENT_NAME => ELEMENT_TYPE, ...];
84 \& use Class::Struct CLASS_NAME => {ELEMENT_NAME => ELEMENT_TYPE, ...};
85 \&
86 \& # declare struct at compile time, based on array, implicit
87 \& # class name:
88 \& package CLASS_NAME;
89 \& use Class::Struct ELEMENT_NAME => ELEMENT_TYPE, ... ;
90 \&
91 \& package Myobj;
92 \& use Class::Struct;
93 \& # declare struct with four types of elements:
94 \& struct( s => \*(Aq$\*(Aq, a => \*(Aq@\*(Aq, h => \*(Aq%\*(Aq, c => \*(AqMy_Other_Class\*(Aq );
95 \&
96 \& my $obj = Myobj\->new; # constructor
97 \&
98 \& # scalar type accessor:
99 \& my $element_value = $obj\->s; # element value
100 \& $obj\->s(\*(Aqnew value\*(Aq); # assign to element
101 \&
102 \& # array type accessor:
103 \& my $ary_ref = $obj\->a; # reference to whole array
104 \& my $ary_element_value = $obj\->a(2); # array element value
105 \& $obj\->a(2, \*(Aqnew value\*(Aq); # assign to array element
106 \&
107 \& # hash type accessor:
108 \& my $hash_ref = $obj\->h; # reference to whole hash
109 \& my $hash_element_value = $obj\->h(\*(Aqx\*(Aq); # hash element value
110 \& $obj\->h(\*(Aqx\*(Aq, \*(Aqnew value\*(Aq); # assign to hash element
111 \&
112 \& # class type accessor:
113 \& my $element_value = $obj\->c; # object reference
114 \& $obj\->c\->method(...); # call method of object
115 \& $obj\->c(new My_Other_Class); # assign a new object
116 .Ve
117 .SH DESCRIPTION
118 .IX Header "DESCRIPTION"
119 \&\f(CW\*(C`Class::Struct\*(C'\fR exports a single function, \f(CW\*(C`struct\*(C'\fR.
120 Given a list of element names and types, and optionally
121 a class name, \f(CW\*(C`struct\*(C'\fR creates a Perl 5 class that implements
122 a "struct\-like" data structure.
123 .PP
124 The new class is given a constructor method, \f(CW\*(C`new\*(C'\fR, for creating
125 struct objects.
126 .PP
127 Each element in the struct data has an accessor method, which is
128 used to assign to the element and to fetch its value. The
129 default accessor can be overridden by declaring a \f(CW\*(C`sub\*(C'\fR of the
130 same name in the package. (See Example 2.)
131 .PP
132 Each element\*(Aqs type can be scalar, array, hash, or class.
133 .ie n .SS "The struct() function"
134 .el .SS "The \f(CWstruct()\fP function"
135 .IX Subsection "The struct() function"
136 The \f(CW\*(C`struct\*(C'\fR function has three forms of parameter\-list.
137 .PP
138 .Vb 3
139 \& struct( CLASS_NAME => [ ELEMENT_LIST ]);
140 \& struct( CLASS_NAME => { ELEMENT_LIST });
141 \& struct( ELEMENT_LIST );
142 .Ve
143 .PP
144 The first and second forms explicitly identify the name of the
145 class being created. The third form assumes the current package
146 name as the class name.
147 .PP
148 An object of a class created by the first and third forms is
149 based on an array, whereas an object of a class created by the
150 second form is based on a hash. The array\-based forms will be
151 somewhat faster and smaller; the hash\-based forms are more
152 flexible.
153 .PP
154 The class created by \f(CW\*(C`struct\*(C'\fR must not be a subclass of another
155 class other than \f(CW\*(C`UNIVERSAL\*(C'\fR.
156 .PP
157 It can, however, be used as a superclass for other classes. To facilitate
158 this, the generated constructor method uses a two\-argument blessing.
159 Furthermore, if the class is hash\-based, the key of each element is
160 prefixed with the class name (see \fIPerl Cookbook\fR, Recipe 13.12).
161 .PP
162 A function named \f(CW\*(C`new\*(C'\fR must not be explicitly defined in a class
163 created by \f(CW\*(C`struct\*(C'\fR.
164 .PP
165 The \fIELEMENT_LIST\fR has the form
166 .PP
167 .Vb 1
168 \& NAME => TYPE, ...
169 .Ve
170 .PP
171 Each name\-type pair declares one element of the struct. Each
172 element name will be defined as an accessor method unless a
173 method by that name is explicitly defined; in the latter case, a
174 warning is issued if the warning flag (\fB\-w\fR) is set.
175 .SS "Class Creation at Compile Time"
176 .IX Subsection "Class Creation at Compile Time"
177 \&\f(CW\*(C`Class::Struct\*(C'\fR can create your class at compile time. The main reason
178 for doing this is obvious, so your class acts like every other class in
179 Perl. Creating your class at compile time will make the order of events
180 similar to using any other class ( or Perl module ).
181 .PP
182 There is no significant speed gain between compile time and run time
183 class creation, there is just a new, more standard order of events.
184 .SS "Element Types and Accessor Methods"
185 .IX Subsection "Element Types and Accessor Methods"
186 The four element types \-\- scalar, array, hash, and class \-\- are
187 represented by strings \-\- \f(CW\*(Aq$\*(Aq\fR, \f(CW\*(Aq@\*(Aq\fR, \f(CW\*(Aq%\*(Aq\fR, and a class name \-\-
188 optionally preceded by a \f(CW\*(Aq*\*(Aq\fR.
189 .PP
190 The accessor method provided by \f(CW\*(C`struct\*(C'\fR for an element depends
191 on the declared type of the element.
192 .ie n .IP "Scalar (\*(Aq$\*(Aq or \*(Aq*$\*(Aq)" 4
193 .el .IP "Scalar (\f(CW\*(Aq$\*(Aq\fR or \f(CW\*(Aq*$\*(Aq\fR)" 4
194 .IX Item "Scalar ('$' or '*$')"
195 The element is a scalar, and by default is initialized to \f(CW\*(C`undef\*(C'\fR
196 (but see "Initializing with new").
197 .Sp
198 The accessor\*(Aqs argument, if any, is assigned to the element.
199 .Sp
200 If the element type is \f(CW\*(Aq$\*(Aq\fR, the value of the element (after
201 assignment) is returned. If the element type is \f(CW\*(Aq*$\*(Aq\fR, a reference
202 to the element is returned.
203 .ie n .IP "Array (\*(Aq@\*(Aq or \*(Aq*@\*(Aq)" 4
204 .el .IP "Array (\f(CW\*(Aq@\*(Aq\fR or \f(CW\*(Aq*@\*(Aq\fR)" 4
205 .IX Item "Array ('@' or '*@')"
206 The element is an array, initialized by default to \f(CW\*(C`()\*(C'\fR.
207 .Sp
208 With no argument, the accessor returns a reference to the
209 element\*(Aqs whole array (whether or not the element was
210 specified as \f(CW\*(Aq@\*(Aq\fR or \f(CW\*(Aq*@\*(Aq\fR).
211 .Sp
212 With one or two arguments, the first argument is an index
213 specifying one element of the array; the second argument, if
214 present, is assigned to the array element. If the element type
215 is \f(CW\*(Aq@\*(Aq\fR, the accessor returns the array element value. If the
216 element type is \f(CW\*(Aq*@\*(Aq\fR, a reference to the array element is
217 returned.
218 .Sp
219 As a special case, when the accessor is called with an array reference
220 as the sole argument, this causes an assignment of the whole array element.
221 The object reference is returned.
222 .ie n .IP "Hash (\*(Aq%\*(Aq or \*(Aq*%\*(Aq)" 4
223 .el .IP "Hash (\f(CW\*(Aq%\*(Aq\fR or \f(CW\*(Aq*%\*(Aq\fR)" 4
224 .IX Item "Hash ('%' or '*%')"
225 The element is a hash, initialized by default to \f(CW\*(C`()\*(C'\fR.
226 .Sp
227 With no argument, the accessor returns a reference to the
228 element\*(Aqs whole hash (whether or not the element was
229 specified as \f(CW\*(Aq%\*(Aq\fR or \f(CW\*(Aq*%\*(Aq\fR).
230 .Sp
231 With one or two arguments, the first argument is a key specifying
232 one element of the hash; the second argument, if present, is
233 assigned to the hash element. If the element type is \f(CW\*(Aq%\*(Aq\fR, the
234 accessor returns the hash element value. If the element type is
235 \&\f(CW\*(Aq*%\*(Aq\fR, a reference to the hash element is returned.
236 .Sp
237 As a special case, when the accessor is called with a hash reference
238 as the sole argument, this causes an assignment of the whole hash element.
239 The object reference is returned.
240 .ie n .IP "Class (\*(AqClass_Name\*(Aq or \*(Aq*Class_Name\*(Aq)" 4
241 .el .IP "Class (\f(CW\*(AqClass_Name\*(Aq\fR or \f(CW\*(Aq*Class_Name\*(Aq\fR)" 4
242 .IX Item "Class ('Class_Name' or '*Class_Name')"
243 The element\*(Aqs value must be a reference blessed to the named
244 class or to one of its subclasses. The element is not initialized
245 by default.
246 .Sp
247 The accessor\*(Aqs argument, if any, is assigned to the element. The
248 accessor will \f(CW\*(C`croak\*(C'\fR if this is not an appropriate object
249 reference.
250 .Sp
251 If the element type does not start with a \f(CW\*(Aq*\*(Aq\fR, the accessor
252 returns the element value (after assignment). If the element type
253 starts with a \f(CW\*(Aq*\*(Aq\fR, a reference to the element itself is returned.
254 .ie n .SS "Initializing with ""new"""
255 .el .SS "Initializing with \f(CWnew\fP"
256 .IX Subsection "Initializing with new"
257 \&\f(CW\*(C`struct\*(C'\fR always creates a constructor called \f(CW\*(C`new\*(C'\fR. That constructor
258 may take a list of initializers for the various elements of the new
259 struct.
260 .PP
261 Each initializer is a pair of values: \fIelement name\fR\f(CW\*(C` => \*(C'\fR\fIvalue\fR.
262 The initializer value for a scalar element is just a scalar value. The
263 initializer for an array element is an array reference. The initializer
264 for a hash is a hash reference.
265 .PP
266 The initializer for a class element is an object of the corresponding class,
267 or of one of it\*(Aqs subclasses, or a reference to a hash containing named
268 arguments to be passed to the element\*(Aqs constructor.
269 .PP
270 See Example 3 below for an example of initialization.
271 .SH EXAMPLES
272 .IX Header "EXAMPLES"
273 .IP "Example 1" 4
274 .IX Item "Example 1"
275 Giving a struct element a class type that is also a struct is how
276 structs are nested. Here, \f(CW\*(C`Timeval\*(C'\fR represents a time (seconds and
277 microseconds), and \f(CW\*(C`Rusage\*(C'\fR has two elements, each of which is of
278 type \f(CW\*(C`Timeval\*(C'\fR.
279 .Sp
280 .Vb 1
281 \& use Class::Struct;
282 \&
283 \& struct( Rusage => {
284 \& ru_utime => \*(AqTimeval\*(Aq, # user time used
285 \& ru_stime => \*(AqTimeval\*(Aq, # system time used
286 \& });
287 \&
288 \& struct( Timeval => [
289 \& tv_secs => \*(Aq$\*(Aq, # seconds
290 \& tv_usecs => \*(Aq$\*(Aq, # microseconds
291 \& ]);
292 \&
293 \& # create an object:
294 \& my $t = Rusage\->new(ru_utime=>Timeval\->new(),
295 \& ru_stime=>Timeval\->new());
296 \&
297 \& # $t\->ru_utime and $t\->ru_stime are objects of type Timeval.
298 \& # set $t\->ru_utime to 100.0 sec and $t\->ru_stime to 5.0 sec.
299 \& $t\->ru_utime\->tv_secs(100);
300 \& $t\->ru_utime\->tv_usecs(0);
301 \& $t\->ru_stime\->tv_secs(5);
302 \& $t\->ru_stime\->tv_usecs(0);
303 .Ve
304 .IP "Example 2" 4
305 .IX Item "Example 2"
306 An accessor function can be redefined in order to provide
307 additional checking of values, etc. Here, we want the \f(CW\*(C`count\*(C'\fR
308 element always to be nonnegative, so we redefine the \f(CW\*(C`count\*(C'\fR
309 accessor accordingly.
310 .Sp
311 .Vb 2
312 \& package MyObj;
313 \& use Class::Struct;
314 \&
315 \& # declare the struct
316 \& struct ( \*(AqMyObj\*(Aq, { count => \*(Aq$\*(Aq, stuff => \*(Aq%\*(Aq } );
317 \&
318 \& # override the default accessor method for \*(Aqcount\*(Aq
319 \& sub count {
320 \& my $self = shift;
321 \& if ( @_ ) {
322 \& die \*(Aqcount must be nonnegative\*(Aq if $_[0] < 0;
323 \& $self\->{\*(AqMyObj::count\*(Aq} = shift;
324 \& warn "Too many args to count" if @_;
325 \& }
326 \& return $self\->{\*(AqMyObj::count\*(Aq};
327 \& }
328 \&
329 \& package main;
330 \& $x = new MyObj;
331 \& print "\e$x\->count(5) = ", $x\->count(5), "\en";
332 \& # prints \*(Aq$x\->count(5) = 5\*(Aq
333 \&
334 \& print "\e$x\->count = ", $x\->count, "\en";
335 \& # prints \*(Aq$x\->count = 5\*(Aq
336 \&
337 \& print "\e$x\->count(\-5) = ", $x\->count(\-5), "\en";
338 \& # dies due to negative argument!
339 .Ve
340 .IP "Example 3" 4
341 .IX Item "Example 3"
342 The constructor of a generated class can be passed a list
343 of \fIelement\fR=>\fIvalue\fR pairs, with which to initialize the struct.
344 If no initializer is specified for a particular element, its default
345 initialization is performed instead. Initializers for non\-existent
346 elements are silently ignored.
347 .Sp
348 Note that the initializer for a nested class may be specified as
349 an object of that class, or as a reference to a hash of initializers
350 that are passed on to the nested struct\*(Aqs constructor.
351 .Sp
352 .Vb 1
353 \& use Class::Struct;
354 \&
355 \& struct Breed =>
356 \& {
357 \& name => \*(Aq$\*(Aq,
358 \& cross => \*(Aq$\*(Aq,
359 \& };
360 \&
361 \& struct Cat =>
362 \& [
363 \& name => \*(Aq$\*(Aq,
364 \& kittens => \*(Aq@\*(Aq,
365 \& markings => \*(Aq%\*(Aq,
366 \& breed => \*(AqBreed\*(Aq,
367 \& ];
368 \&
369 \&
370 \& my $cat = Cat\->new( name => \*(AqSocks\*(Aq,
371 \& kittens => [\*(AqMonica\*(Aq, \*(AqKenneth\*(Aq],
372 \& markings => { socks=>1, blaze=>"white" },
373 \& breed => Breed\->new(name=>\*(Aqshort\-hair\*(Aq, cross=>1),
374 \& or: breed => {name=>\*(Aqshort\-hair\*(Aq, cross=>1},
375 \& );
376 \&
377 \& print "Once a cat called ", $cat\->name, "\en";
378 \& print "(which was a ", $cat\->breed\->name, ")\en";
379 \& print "had 2 kittens: ", join(\*(Aq and \*(Aq, @{$cat\->kittens}), "\en";
380 .Ve
381 .SH "Author and Modification History"
382 .IX Header "Author and Modification History"
383 Modified by Damian Conway, 2001\-09\-10, v0.62.
384 .PP
385 .Vb 11
386 \& Modified implicit construction of nested objects.
387 \& Now will also take an object ref instead of requiring a hash ref.
388 \& Also default initializes nested object attributes to undef, rather
389 \& than calling object constructor without args
390 \& Original over\-helpfulness was fraught with problems:
391 \& * the class\*(Aqs constructor might not be called \*(Aqnew\*(Aq
392 \& * the class might not have a hash\-like\-arguments constructor
393 \& * the class might not have a no\-argument constructor
394 \& * "recursive" data structures didn\*(Aqt work well:
395 \& package Person;
396 \& struct { mother => \*(AqPerson\*(Aq, father => \*(AqPerson\*(Aq};
397 .Ve
398 .PP
399 Modified by Casey West, 2000\-11\-08, v0.59.
400 .PP
401 .Vb 1
402 \& Added the ability for compile time class creation.
403 .Ve
404 .PP
405 Modified by Damian Conway, 1999\-03\-05, v0.58.
406 .PP
407 .Vb 1
408 \& Added handling of hash\-like arg list to class ctor.
409 \&
410 \& Changed to two\-argument blessing in ctor to support
411 \& derivation from created classes.
412 \&
413 \& Added classname prefixes to keys in hash\-based classes
414 \& (refer to "Perl Cookbook", Recipe 13.12 for rationale).
415 \&
416 \& Corrected behaviour of accessors for \*(Aq*@\*(Aq and \*(Aq*%\*(Aq struct
417 \& elements. Package now implements documented behaviour when
418 \& returning a reference to an entire hash or array element.
419 \& Previously these were returned as a reference to a reference
420 \& to the element.
421 .Ve
422 .PP
423 Renamed to \f(CW\*(C`Class::Struct\*(C'\fR and modified by Jim Miner, 1997\-04\-02.
424 .PP
425 .Vb 8
426 \& members() function removed.
427 \& Documentation corrected and extended.
428 \& Use of struct() in a subclass prohibited.
429 \& User definition of accessor allowed.
430 \& Treatment of \*(Aq*\*(Aq in element types corrected.
431 \& Treatment of classes as element types corrected.
432 \& Class name to struct() made optional.
433 \& Diagnostic checks added.
434 .Ve
435 .PP
436 Originally \f(CW\*(C`Class::Template\*(C'\fR by Dean Roehrich.
437 .PP
438 .Vb 10
439 \& # Template.pm \-\-\- struct/member template builder
440 \& # 12mar95
441 \& # Dean Roehrich
442 \& #
443 \& # changes/bugs fixed since 28nov94 version:
444 \& # \- podified
445 \& # changes/bugs fixed since 21nov94 version:
446 \& # \- Fixed examples.
447 \& # changes/bugs fixed since 02sep94 version:
448 \& # \- Moved to Class::Template.
449 \& # changes/bugs fixed since 20feb94 version:
450 \& # \- Updated to be a more proper module.
451 \& # \- Added "use strict".
452 \& # \- Bug in build_methods, was using @var when @$var needed.
453 \& # \- Now using my() rather than local().
454 \& #
455 \& # Uses perl5 classes to create nested data types.
456 \& # This is offered as one implementation of Tom Christiansen\*(Aqs
457 \& # "structs.pl" idea.
458 .Ve