Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / man / man3 / Class::Struct.3
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Class::Struct 3"
132.TH Class::Struct 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide"
133.SH "NAME"
134Class::Struct \- declare struct\-like datatypes as Perl classes
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 5
138\& use Class::Struct;
139\& # declare struct, based on array:
140\& struct( CLASS_NAME => [ ELEMENT_NAME => ELEMENT_TYPE, ... ]);
141\& # declare struct, based on hash:
142\& struct( CLASS_NAME => { ELEMENT_NAME => ELEMENT_TYPE, ... });
143.Ve
144.PP
145.Vb 4
146\& package CLASS_NAME;
147\& use Class::Struct;
148\& # declare struct, based on array, implicit class name:
149\& struct( ELEMENT_NAME => ELEMENT_TYPE, ... );
150.Ve
151.PP
152.Vb 3
153\& # Declare struct at compile time
154\& use Class::Struct CLASS_NAME => [ ELEMENT_NAME => ELEMENT_TYPE, ... ];
155\& use Class::Struct CLASS_NAME => { ELEMENT_NAME => ELEMENT_TYPE, ... };
156.Ve
157.PP
158.Vb 3
159\& # declare struct at compile time, based on array, implicit class name:
160\& package CLASS_NAME;
161\& use Class::Struct ELEMENT_NAME => ELEMENT_TYPE, ... ;
162.Ve
163.PP
164.Vb 4
165\& package Myobj;
166\& use Class::Struct;
167\& # declare struct with four types of elements:
168\& struct( s => '$', a => '@', h => '%', c => 'My_Other_Class' );
169.Ve
170.PP
171.Vb 1
172\& $obj = new Myobj; # constructor
173.Ve
174.PP
175.Vb 3
176\& # scalar type accessor:
177\& $element_value = $obj->s; # element value
178\& $obj->s('new value'); # assign to element
179.Ve
180.PP
181.Vb 4
182\& # array type accessor:
183\& $ary_ref = $obj->a; # reference to whole array
184\& $ary_element_value = $obj->a(2); # array element value
185\& $obj->a(2, 'new value'); # assign to array element
186.Ve
187.PP
188.Vb 4
189\& # hash type accessor:
190\& $hash_ref = $obj->h; # reference to whole hash
191\& $hash_element_value = $obj->h('x'); # hash element value
192\& $obj->h('x', 'new value'); # assign to hash element
193.Ve
194.PP
195.Vb 4
196\& # class type accessor:
197\& $element_value = $obj->c; # object reference
198\& $obj->c->method(...); # call method of object
199\& $obj->c(new My_Other_Class); # assign a new object
200.Ve
201.SH "DESCRIPTION"
202.IX Header "DESCRIPTION"
203\&\f(CW\*(C`Class::Struct\*(C'\fR exports a single function, \f(CW\*(C`struct\*(C'\fR.
204Given a list of element names and types, and optionally
205a class name, \f(CW\*(C`struct\*(C'\fR creates a Perl 5 class that implements
206a \*(L"struct\-like\*(R" data structure.
207.PP
208The new class is given a constructor method, \f(CW\*(C`new\*(C'\fR, for creating
209struct objects.
210.PP
211Each element in the struct data has an accessor method, which is
212used to assign to the element and to fetch its value. The
213default accessor can be overridden by declaring a \f(CW\*(C`sub\*(C'\fR of the
214same name in the package. (See Example 2.)
215.PP
216Each element's type can be scalar, array, hash, or class.
217.ie n .Sh "The ""struct()"" function"
218.el .Sh "The \f(CWstruct()\fP function"
219.IX Subsection "The struct() function"
220The \f(CW\*(C`struct\*(C'\fR function has three forms of parameter\-list.
221.PP
222.Vb 3
223\& struct( CLASS_NAME => [ ELEMENT_LIST ]);
224\& struct( CLASS_NAME => { ELEMENT_LIST });
225\& struct( ELEMENT_LIST );
226.Ve
227.PP
228The first and second forms explicitly identify the name of the
229class being created. The third form assumes the current package
230name as the class name.
231.PP
232An object of a class created by the first and third forms is
233based on an array, whereas an object of a class created by the
234second form is based on a hash. The array-based forms will be
235somewhat faster and smaller; the hash-based forms are more
236flexible.
237.PP
238The class created by \f(CW\*(C`struct\*(C'\fR must not be a subclass of another
239class other than \f(CW\*(C`UNIVERSAL\*(C'\fR.
240.PP
241It can, however, be used as a superclass for other classes. To facilitate
242this, the generated constructor method uses a two-argument blessing.
243Furthermore, if the class is hash\-based, the key of each element is
244prefixed with the class name (see \fIPerl Cookbook\fR, Recipe 13.12).
245.PP
246A function named \f(CW\*(C`new\*(C'\fR must not be explicitly defined in a class
247created by \f(CW\*(C`struct\*(C'\fR.
248.PP
249The \fI\s-1ELEMENT_LIST\s0\fR has the form
250.PP
251.Vb 1
252\& NAME => TYPE, ...
253.Ve
254.PP
255Each name-type pair declares one element of the struct. Each
256element name will be defined as an accessor method unless a
257method by that name is explicitly defined; in the latter case, a
258warning is issued if the warning flag (\fB\-w\fR) is set.
259.Sh "Class Creation at Compile Time"
260.IX Subsection "Class Creation at Compile Time"
261\&\f(CW\*(C`Class::Struct\*(C'\fR can create your class at compile time. The main reason
262for doing this is obvious, so your class acts like every other class in
263Perl. Creating your class at compile time will make the order of events
264similar to using any other class ( or Perl module ).
265.PP
266There is no significant speed gain between compile time and run time
267class creation, there is just a new, more standard order of events.
268.Sh "Element Types and Accessor Methods"
269.IX Subsection "Element Types and Accessor Methods"
270The four element types \*(-- scalar, array, hash, and class \*(-- are
271represented by strings \*(-- \f(CW'$'\fR, \f(CW'@'\fR, \f(CW'%'\fR, and a class name \*(--
272optionally preceded by a \f(CW'*'\fR.
273.PP
274The accessor method provided by \f(CW\*(C`struct\*(C'\fR for an element depends
275on the declared type of the element.
276.ie n .IP "Scalar ('$'\fR or \f(CW'*$')" 4
277.el .IP "Scalar (\f(CW'$'\fR or \f(CW'*$'\fR)" 4
278.IX Item "Scalar ('$' or '*$')"
279The element is a scalar, and by default is initialized to \f(CW\*(C`undef\*(C'\fR
280(but see \*(L"Initializing with new\*(R").
281.Sp
282The accessor's argument, if any, is assigned to the element.
283.Sp
284If the element type is \f(CW'$'\fR, the value of the element (after
285assignment) is returned. If the element type is \f(CW'*$'\fR, a reference
286to the element is returned.
287.ie n .IP "Array ('@'\fR or \f(CW'*@')" 4
288.el .IP "Array (\f(CW'@'\fR or \f(CW'*@'\fR)" 4
289.IX Item "Array ('@' or '*@')"
290The element is an array, initialized by default to \f(CW\*(C`()\*(C'\fR.
291.Sp
292With no argument, the accessor returns a reference to the
293element's whole array (whether or not the element was
294specified as \f(CW'@'\fR or \f(CW'*@'\fR).
295.Sp
296With one or two arguments, the first argument is an index
297specifying one element of the array; the second argument, if
298present, is assigned to the array element. If the element type
299is \f(CW'@'\fR, the accessor returns the array element value. If the
300element type is \f(CW'*@'\fR, a reference to the array element is
301returned.
302.Sp
303As a special case, when the accessor is called with an array reference
304as the sole argument, this causes an assignment of the whole array element.
305The object reference is returned.
306.ie n .IP "Hash ('%'\fR or \f(CW'*%')" 4
307.el .IP "Hash (\f(CW'%'\fR or \f(CW'*%'\fR)" 4
308.IX Item "Hash ('%' or '*%')"
309The element is a hash, initialized by default to \f(CW\*(C`()\*(C'\fR.
310.Sp
311With no argument, the accessor returns a reference to the
312element's whole hash (whether or not the element was
313specified as \f(CW'%'\fR or \f(CW'*%'\fR).
314.Sp
315With one or two arguments, the first argument is a key specifying
316one element of the hash; the second argument, if present, is
317assigned to the hash element. If the element type is \f(CW'%'\fR, the
318accessor returns the hash element value. If the element type is
319\&\f(CW'*%'\fR, a reference to the hash element is returned.
320.Sp
321As a special case, when the accessor is called with a hash reference
322as the sole argument, this causes an assignment of the whole hash element.
323The object reference is returned.
324.ie n .IP "Class ('Class_Name'\fR or \f(CW'*Class_Name')" 4
325.el .IP "Class (\f(CW'Class_Name'\fR or \f(CW'*Class_Name'\fR)" 4
326.IX Item "Class ('Class_Name' or '*Class_Name')"
327The element's value must be a reference blessed to the named
328class or to one of its subclasses. The element is not initialized
329by default.
330.Sp
331The accessor's argument, if any, is assigned to the element. The
332accessor will \f(CW\*(C`croak\*(C'\fR if this is not an appropriate object
333reference.
334.Sp
335If the element type does not start with a \f(CW'*'\fR, the accessor
336returns the element value (after assignment). If the element type
337starts with a \f(CW'*'\fR, a reference to the element itself is returned.
338.ie n .Sh "Initializing with ""new"""
339.el .Sh "Initializing with \f(CWnew\fP"
340.IX Subsection "Initializing with new"
341\&\f(CW\*(C`struct\*(C'\fR always creates a constructor called \f(CW\*(C`new\*(C'\fR. That constructor
342may take a list of initializers for the various elements of the new
343struct.
344.PP
345Each initializer is a pair of values: \fIelement name\fR\f(CW\*(C` => \*(C'\fR\fIvalue\fR.
346The initializer value for a scalar element is just a scalar value. The
347initializer for an array element is an array reference. The initializer
348for a hash is a hash reference.
349.PP
350The initializer for a class element is an object of the corresponding class,
351or of one of it's subclasses, or a reference to a hash containing named
352arguments to be passed to the element's constructor.
353.PP
354See Example 3 below for an example of initialization.
355.SH "EXAMPLES"
356.IX Header "EXAMPLES"
357.IP "Example 1" 4
358.IX Item "Example 1"
359Giving a struct element a class type that is also a struct is how
360structs are nested. Here, \f(CW\*(C`Timeval\*(C'\fR represents a time (seconds and
361microseconds), and \f(CW\*(C`Rusage\*(C'\fR has two elements, each of which is of
362type \f(CW\*(C`Timeval\*(C'\fR.
363.Sp
364.Vb 1
365\& use Class::Struct;
366.Ve
367.Sp
368.Vb 4
369\& struct( Rusage => {
370\& ru_utime => 'Timeval', # user time used
371\& ru_stime => 'Timeval', # system time used
372\& });
373.Ve
374.Sp
375.Vb 4
376\& struct( Timeval => [
377\& tv_secs => '$', # seconds
378\& tv_usecs => '$', # microseconds
379\& ]);
380.Ve
381.Sp
382.Vb 2
383\& # create an object:
384\& my $t = Rusage->new(ru_utime=>Timeval->new(), ru_stime=>Timeval->new());
385.Ve
386.Sp
387.Vb 6
388\& # $t->ru_utime and $t->ru_stime are objects of type Timeval.
389\& # set $t->ru_utime to 100.0 sec and $t->ru_stime to 5.0 sec.
390\& $t->ru_utime->tv_secs(100);
391\& $t->ru_utime->tv_usecs(0);
392\& $t->ru_stime->tv_secs(5);
393\& $t->ru_stime->tv_usecs(0);
394.Ve
395.IP "Example 2" 4
396.IX Item "Example 2"
397An accessor function can be redefined in order to provide
398additional checking of values, etc. Here, we want the \f(CW\*(C`count\*(C'\fR
399element always to be nonnegative, so we redefine the \f(CW\*(C`count\*(C'\fR
400accessor accordingly.
401.Sp
402.Vb 2
403\& package MyObj;
404\& use Class::Struct;
405.Ve
406.Sp
407.Vb 2
408\& # declare the struct
409\& struct ( 'MyObj', { count => '$', stuff => '%' } );
410.Ve
411.Sp
412.Vb 10
413\& # override the default accessor method for 'count'
414\& sub count {
415\& my $self = shift;
416\& if ( @_ ) {
417\& die 'count must be nonnegative' if $_[0] < 0;
418\& $self->{'MyObj::count'} = shift;
419\& warn "Too many args to count" if @_;
420\& }
421\& return $self->{'MyObj::count'};
422\& }
423.Ve
424.Sp
425.Vb 4
426\& package main;
427\& $x = new MyObj;
428\& print "\e$x->count(5) = ", $x->count(5), "\en";
429\& # prints '$x->count(5) = 5'
430.Ve
431.Sp
432.Vb 2
433\& print "\e$x->count = ", $x->count, "\en";
434\& # prints '$x->count = 5'
435.Ve
436.Sp
437.Vb 2
438\& print "\e$x->count(-5) = ", $x->count(-5), "\en";
439\& # dies due to negative argument!
440.Ve
441.IP "Example 3" 4
442.IX Item "Example 3"
443The constructor of a generated class can be passed a list
444of \fIelement\fR=>\fIvalue\fR pairs, with which to initialize the struct.
445If no initializer is specified for a particular element, its default
446initialization is performed instead. Initializers for non-existent
447elements are silently ignored.
448.Sp
449Note that the initializer for a nested class may be specified as
450an object of that class, or as a reference to a hash of initializers
451that are passed on to the nested struct's constructor.
452.Sp
453.Vb 1
454\& use Class::Struct;
455.Ve
456.Sp
457.Vb 5
458\& struct Breed =>
459\& {
460\& name => '$',
461\& cross => '$',
462\& };
463.Ve
464.Sp
465.Vb 7
466\& struct Cat =>
467\& [
468\& name => '$',
469\& kittens => '@',
470\& markings => '%',
471\& breed => 'Breed',
472\& ];
473.Ve
474.Sp
475.Vb 6
476\& my $cat = Cat->new( name => 'Socks',
477\& kittens => ['Monica', 'Kenneth'],
478\& markings => { socks=>1, blaze=>"white" },
479\& breed => Breed->new(name=>'short-hair', cross=>1),
480\& or: breed => {name=>'short-hair', cross=>1},
481\& );
482.Ve
483.Sp
484.Vb 3
485\& print "Once a cat called ", $cat->name, "\en";
486\& print "(which was a ", $cat->breed->name, ")\en";
487\& print "had two kittens: ", join(' and ', @{$cat->kittens}), "\en";
488.Ve
489.SH "Author and Modification History"
490.IX Header "Author and Modification History"
491Modified by Damian Conway, 2001\-09\-10, v0.62.
492.PP
493.Vb 11
494\& Modified implicit construction of nested objects.
495\& Now will also take an object ref instead of requiring a hash ref.
496\& Also default initializes nested object attributes to undef, rather
497\& than calling object constructor without args
498\& Original over-helpfulness was fraught with problems:
499\& * the class's constructor might not be called 'new'
500\& * the class might not have a hash-like-arguments constructor
501\& * the class might not have a no-argument constructor
502\& * "recursive" data structures didn't work well:
503\& package Person;
504\& struct { mother => 'Person', father => 'Person'};
505.Ve
506.PP
507Modified by Casey West, 2000\-11\-08, v0.59.
508.PP
509.Vb 1
510\& Added the ability for compile time class creation.
511.Ve
512.PP
513Modified by Damian Conway, 1999\-03\-05, v0.58.
514.PP
515.Vb 1
516\& Added handling of hash-like arg list to class ctor.
517.Ve
518.PP
519.Vb 2
520\& Changed to two-argument blessing in ctor to support
521\& derivation from created classes.
522.Ve
523.PP
524.Vb 2
525\& Added classname prefixes to keys in hash-based classes
526\& (refer to "Perl Cookbook", Recipe 13.12 for rationale).
527.Ve
528.PP
529.Vb 5
530\& Corrected behaviour of accessors for '*@' and '*%' struct
531\& elements. Package now implements documented behaviour when
532\& returning a reference to an entire hash or array element.
533\& Previously these were returned as a reference to a reference
534\& to the element.
535.Ve
536.PP
537Renamed to \f(CW\*(C`Class::Struct\*(C'\fR and modified by Jim Miner, 1997\-04\-02.
538.PP
539.Vb 8
540\& members() function removed.
541\& Documentation corrected and extended.
542\& Use of struct() in a subclass prohibited.
543\& User definition of accessor allowed.
544\& Treatment of '*' in element types corrected.
545\& Treatment of classes as element types corrected.
546\& Class name to struct() made optional.
547\& Diagnostic checks added.
548.Ve
549.PP
550Originally \f(CW\*(C`Class::Template\*(C'\fR by Dean Roehrich.
551.PP
552.Vb 19
553\& # Template.pm --- struct/member template builder
554\& # 12mar95
555\& # Dean Roehrich
556\& #
557\& # changes/bugs fixed since 28nov94 version:
558\& # - podified
559\& # changes/bugs fixed since 21nov94 version:
560\& # - Fixed examples.
561\& # changes/bugs fixed since 02sep94 version:
562\& # - Moved to Class::Template.
563\& # changes/bugs fixed since 20feb94 version:
564\& # - Updated to be a more proper module.
565\& # - Added "use strict".
566\& # - Bug in build_methods, was using @var when @$var needed.
567\& # - Now using my() rather than local().
568\& #
569\& # Uses perl5 classes to create nested data types.
570\& # This is offered as one implementation of Tom Christiansen's "structs.pl"
571\& # idea.
572.Ve