Debian Codegraph @

upstream/debian-unstable/man3/Class::Struct.3perl

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