Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Class::Struct.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
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 "2002-06-01" "perl v5.8.0" "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 4
159\& package Myobj;
160\& use Class::Struct;
161\& # declare struct with four types of elements:
162\& struct( s => '$', a => '@', h => '%', c => 'My_Other_Class' );
163.Ve
164.PP
165.Vb 1
166\& $obj = new Myobj; # constructor
167.Ve
168.PP
169.Vb 3
170\& # scalar type accessor:
171\& $element_value = $obj->s; # element value
172\& $obj->s('new value'); # assign to element
173.Ve
174.PP
175.Vb 4
176\& # array type accessor:
177\& $ary_ref = $obj->a; # reference to whole array
178\& $ary_element_value = $obj->a(2); # array element value
179\& $obj->a(2, 'new value'); # assign to array element
180.Ve
181.PP
182.Vb 4
183\& # hash type accessor:
184\& $hash_ref = $obj->h; # reference to whole hash
185\& $hash_element_value = $obj->h('x'); # hash element value
186\& $obj->h('x', 'new value'); # assign to hash element
187.Ve
188.PP
189.Vb 4
190\& # class type accessor:
191\& $element_value = $obj->c; # object reference
192\& $obj->c->method(...); # call method of object
193\& $obj->c(new My_Other_Class); # assign a new object
194.Ve
195.SH "DESCRIPTION"
196.IX Header "DESCRIPTION"
197\&\f(CW\*(C`Class::Struct\*(C'\fR exports a single function, \f(CW\*(C`struct\*(C'\fR.
198Given a list of element names and types, and optionally
199a class name, \f(CW\*(C`struct\*(C'\fR creates a Perl 5 class that implements
200a \*(L"struct\-like\*(R" data structure.
201.PP
202The new class is given a constructor method, \f(CW\*(C`new\*(C'\fR, for creating
203struct objects.
204.PP
205Each element in the struct data has an accessor method, which is
206used to assign to the element and to fetch its value. The
207default accessor can be overridden by declaring a \f(CW\*(C`sub\*(C'\fR of the
208same name in the package. (See Example 2.)
209.PP
210Each element's type can be scalar, array, hash, or class.
211.ie n .Sh "The ""struct()"" function"
212.el .Sh "The \f(CWstruct()\fP function"
213.IX Subsection "The struct() function"
214The \f(CW\*(C`struct\*(C'\fR function has three forms of parameter\-list.
215.PP
216.Vb 3
217\& struct( CLASS_NAME => [ ELEMENT_LIST ]);
218\& struct( CLASS_NAME => { ELEMENT_LIST });
219\& struct( ELEMENT_LIST );
220.Ve
221.PP
222The first and second forms explicitly identify the name of the
223class being created. The third form assumes the current package
224name as the class name.
225.PP
226An object of a class created by the first and third forms is
227based on an array, whereas an object of a class created by the
228second form is based on a hash. The array-based forms will be
229somewhat faster and smaller; the hash-based forms are more
230flexible.
231.PP
232The class created by \f(CW\*(C`struct\*(C'\fR must not be a subclass of another
233class other than \f(CW\*(C`UNIVERSAL\*(C'\fR.
234.PP
235It can, however, be used as a superclass for other classes. To facilitate
236this, the generated constructor method uses a two-argument blessing.
237Furthermore, if the class is hash\-based, the key of each element is
238prefixed with the class name (see \fIPerl Cookbook\fR, Recipe 13.12).
239.PP
240A function named \f(CW\*(C`new\*(C'\fR must not be explicitly defined in a class
241created by \f(CW\*(C`struct\*(C'\fR.
242.PP
243The \fI\s-1ELEMENT_LIST\s0\fR has the form
244.PP
245.Vb 1
246\& NAME => TYPE, ...
247.Ve
248.PP
249Each name-type pair declares one element of the struct. Each
250element name will be defined as an accessor method unless a
251method by that name is explicitly defined; in the latter case, a
252warning is issued if the warning flag (\fB\-w\fR) is set.
253.Sh "Class Creation at Compile Time"
254.IX Subsection "Class Creation at Compile Time"
255\&\f(CW\*(C`Class::Struct\*(C'\fR can create your class at compile time. The main reason
256for doing this is obvious, so your class acts like every other class in
257Perl. Creating your class at compile time will make the order of events
258similar to using any other class ( or Perl module ).
259.PP
260There is no significant speed gain between compile time and run time
261class creation, there is just a new, more standard order of events.
262.Sh "Element Types and Accessor Methods"
263.IX Subsection "Element Types and Accessor Methods"
264The four element types \*(-- scalar, array, hash, and class \*(-- are
265represented by strings \*(-- \f(CW'$'\fR, \f(CW'@'\fR, \f(CW'%'\fR, and a class name \*(--
266optionally preceded by a \f(CW'*'\fR.
267.PP
268The accessor method provided by \f(CW\*(C`struct\*(C'\fR for an element depends
269on the declared type of the element.
270.ie n .IP "Scalar ('$'\fR or \f(CW'*$')" 4
271.el .IP "Scalar (\f(CW'$'\fR or \f(CW'*$'\fR)" 4
272.IX Item "Scalar ('$' or '*$')"
273The element is a scalar, and by default is initialized to \f(CW\*(C`undef\*(C'\fR
274(but see \*(L"Initializing with new\*(R").
275.Sp
276The accessor's argument, if any, is assigned to the element.
277.Sp
278If the element type is \f(CW'$'\fR, the value of the element (after
279assignment) is returned. If the element type is \f(CW'*$'\fR, a reference
280to the element is returned.
281.ie n .IP "Array ('@'\fR or \f(CW'*@')" 4
282.el .IP "Array (\f(CW'@'\fR or \f(CW'*@'\fR)" 4
283.IX Item "Array ('@' or '*@')"
284The element is an array, initialized by default to \f(CW\*(C`()\*(C'\fR.
285.Sp
286With no argument, the accessor returns a reference to the
287element's whole array (whether or not the element was
288specified as \f(CW'@'\fR or \f(CW'*@'\fR).
289.Sp
290With one or two arguments, the first argument is an index
291specifying one element of the array; the second argument, if
292present, is assigned to the array element. If the element type
293is \f(CW'@'\fR, the accessor returns the array element value. If the
294element type is \f(CW'*@'\fR, a reference to the array element is
295returned.
296.Sp
297As a special case, when the accessor is called with an array reference
298as the sole argument, this causes an assignment of the whole array element.
299The object reference is returned.
300.ie n .IP "Hash ('%'\fR or \f(CW'*%')" 4
301.el .IP "Hash (\f(CW'%'\fR or \f(CW'*%'\fR)" 4
302.IX Item "Hash ('%' or '*%')"
303The element is a hash, initialized by default to \f(CW\*(C`()\*(C'\fR.
304.Sp
305With no argument, the accessor returns a reference to the
306element's whole hash (whether or not the element was
307specified as \f(CW'%'\fR or \f(CW'*%'\fR).
308.Sp
309With one or two arguments, the first argument is a key specifying
310one element of the hash; the second argument, if present, is
311assigned to the hash element. If the element type is \f(CW'%'\fR, the
312accessor returns the hash element value. If the element type is
313\&\f(CW'*%'\fR, a reference to the hash element is returned.
314.Sp
315As a special case, when the accessor is called with a hash reference
316as the sole argument, this causes an assignment of the whole hash element.
317The object reference is returned.
318.ie n .IP "Class ('Class_Name'\fR or \f(CW'*Class_Name')" 4
319.el .IP "Class (\f(CW'Class_Name'\fR or \f(CW'*Class_Name'\fR)" 4
320.IX Item "Class ('Class_Name' or '*Class_Name')"
321The element's value must be a reference blessed to the named
322class or to one of its subclasses. The element is not initialized
323by default.
324.Sp
325The accessor's argument, if any, is assigned to the element. The
326accessor will \f(CW\*(C`croak\*(C'\fR if this is not an appropriate object
327reference.
328.Sp
329If the element type does not start with a \f(CW'*'\fR, the accessor
330returns the element value (after assignment). If the element type
331starts with a \f(CW'*'\fR, a reference to the element itself is returned.
332.ie n .Sh "Initializing with ""new"""
333.el .Sh "Initializing with \f(CWnew\fP"
334.IX Subsection "Initializing with new"
335\&\f(CW\*(C`struct\*(C'\fR always creates a constructor called \f(CW\*(C`new\*(C'\fR. That constructor
336may take a list of initializers for the various elements of the new
337struct.
338.PP
339Each initializer is a pair of values: \fIelement name\fR\f(CW\*(C` => \*(C'\fR\fIvalue\fR.
340The initializer value for a scalar element is just a scalar value. The
341initializer for an array element is an array reference. The initializer
342for a hash is a hash reference.
343.PP
344The initializer for a class element is an object of the corresponding class,
345or of one of it's subclasses, or a reference to a hash containing named
346arguments to be passed to the element's constructor.
347.PP
348See Example 3 below for an example of initialization.
349.SH "EXAMPLES"
350.IX Header "EXAMPLES"
351.IP "Example 1" 4
352.IX Item "Example 1"
353Giving a struct element a class type that is also a struct is how
354structs are nested. Here, \f(CW\*(C`timeval\*(C'\fR represents a time (seconds and
355microseconds), and \f(CW\*(C`rusage\*(C'\fR has two elements, each of which is of
356type \f(CW\*(C`timeval\*(C'\fR.
357.Sp
358.Vb 1
359\& use Class::Struct;
360.Ve
361.Sp
362.Vb 4
363\& struct( rusage => {
364\& ru_utime => timeval, # seconds
365\& ru_stime => timeval, # microseconds
366\& });
367.Ve
368.Sp
369.Vb 4
370\& struct( timeval => [
371\& tv_secs => '$',
372\& tv_usecs => '$',
373\& ]);
374.Ve
375.Sp
376.Vb 2
377\& # create an object:
378\& my $t = new rusage;
379.Ve
380.Sp
381.Vb 6
382\& # $t->ru_utime and $t->ru_stime are objects of type timeval.
383\& # set $t->ru_utime to 100.0 sec and $t->ru_stime to 5.0 sec.
384\& $t->ru_utime->tv_secs(100);
385\& $t->ru_utime->tv_usecs(0);
386\& $t->ru_stime->tv_secs(5);
387\& $t->ru_stime->tv_usecs(0);
388.Ve
389.IP "Example 2" 4
390.IX Item "Example 2"
391An accessor function can be redefined in order to provide
392additional checking of values, etc. Here, we want the \f(CW\*(C`count\*(C'\fR
393element always to be nonnegative, so we redefine the \f(CW\*(C`count\*(C'\fR
394accessor accordingly.
395.Sp
396.Vb 2
397\& package MyObj;
398\& use Class::Struct;
399.Ve
400.Sp
401.Vb 2
402\& # declare the struct
403\& struct ( 'MyObj', { count => '$', stuff => '%' } );
404.Ve
405.Sp
406.Vb 10
407\& # override the default accessor method for 'count'
408\& sub count {
409\& my $self = shift;
410\& if ( @_ ) {
411\& die 'count must be nonnegative' if $_[0] < 0;
412\& $self->{'count'} = shift;
413\& warn "Too many args to count" if @_;
414\& }
415\& return $self->{'count'};
416\& }
417.Ve
418.Sp
419.Vb 4
420\& package main;
421\& $x = new MyObj;
422\& print "\e$x->count(5) = ", $x->count(5), "\en";
423\& # prints '$x->count(5) = 5'
424.Ve
425.Sp
426.Vb 2
427\& print "\e$x->count = ", $x->count, "\en";
428\& # prints '$x->count = 5'
429.Ve
430.Sp
431.Vb 2
432\& print "\e$x->count(-5) = ", $x->count(-5), "\en";
433\& # dies due to negative argument!
434.Ve
435.IP "Example 3" 4
436.IX Item "Example 3"
437The constructor of a generated class can be passed a list
438of \fIelement\fR=>\fIvalue\fR pairs, with which to initialize the struct.
439If no initializer is specified for a particular element, its default
440initialization is performed instead. Initializers for non-existent
441elements are silently ignored.
442.Sp
443Note that the initializer for a nested class may be specified as
444an object of that class, or as a reference to a hash of initializers
445that are passed on to the nested struct's constructor.
446.Sp
447.Vb 1
448\& use Class::Struct;
449.Ve
450.Sp
451.Vb 5
452\& struct Breed =>
453\& {
454\& name => '$',
455\& cross => '$',
456\& };
457.Ve
458.Sp
459.Vb 7
460\& struct Cat =>
461\& [
462\& name => '$',
463\& kittens => '@',
464\& markings => '%',
465\& breed => 'Breed',
466\& ];
467.Ve
468.Sp
469.Vb 6
470\& my $cat = Cat->new( name => 'Socks',
471\& kittens => ['Monica', 'Kenneth'],
472\& markings => { socks=>1, blaze=>"white" },
473\& breed => Breed->new(name=>'short-hair', cross=>1),
474\& or: breed => {name=>'short-hair', cross=>1},
475\& );
476.Ve
477.Sp
478.Vb 3
479\& print "Once a cat called ", $cat->name, "\en";
480\& print "(which was a ", $cat->breed->name, ")\en";
481\& print "had two kittens: ", join(' and ', @{$cat->kittens}), "\en";
482.Ve
483.SH "Author and Modification History"
484.IX Header "Author and Modification History"
485Modified by Damian Conway, 2001\-09\-10, v0.62.
486.PP
487.Vb 11
488\& Modified implicit construction of nested objects.
489\& Now will also take an object ref instead of requiring a hash ref.
490\& Also default initializes nested object attributes to undef, rather
491\& than calling object constructor without args
492\& Original over-helpfulness was fraught with problems:
493\& * the class's constructor might not be called 'new'
494\& * the class might not have a hash-like-arguments constructor
495\& * the class might not have a no-argument constructor
496\& * "recursive" data structures didn't work well:
497\& package Person;
498\& struct { mother => 'Person', father => 'Person'};
499.Ve
500.PP
501Modified by Casey West, 2000\-11\-08, v0.59.
502.PP
503.Vb 1
504\& Added the ability for compile time class creation.
505.Ve
506.PP
507Modified by Damian Conway, 1999\-03\-05, v0.58.
508.PP
509.Vb 1
510\& Added handling of hash-like arg list to class ctor.
511.Ve
512.PP
513.Vb 2
514\& Changed to two-argument blessing in ctor to support
515\& derivation from created classes.
516.Ve
517.PP
518.Vb 2
519\& Added classname prefixes to keys in hash-based classes
520\& (refer to "Perl Cookbook", Recipe 13.12 for rationale).
521.Ve
522.PP
523.Vb 5
524\& Corrected behaviour of accessors for '*@' and '*%' struct
525\& elements. Package now implements documented behaviour when
526\& returning a reference to an entire hash or array element.
527\& Previously these were returned as a reference to a reference
528\& to the element.
529.Ve
530.PP
531Renamed to \f(CW\*(C`Class::Struct\*(C'\fR and modified by Jim Miner, 1997\-04\-02.
532.PP
533.Vb 8
534\& members() function removed.
535\& Documentation corrected and extended.
536\& Use of struct() in a subclass prohibited.
537\& User definition of accessor allowed.
538\& Treatment of '*' in element types corrected.
539\& Treatment of classes as element types corrected.
540\& Class name to struct() made optional.
541\& Diagnostic checks added.
542.Ve
543.PP
544Originally \f(CW\*(C`Class::Template\*(C'\fR by Dean Roehrich.
545.PP
546.Vb 19
547\& # Template.pm --- struct/member template builder
548\& # 12mar95
549\& # Dean Roehrich
550\& #
551\& # changes/bugs fixed since 28nov94 version:
552\& # - podified
553\& # changes/bugs fixed since 21nov94 version:
554\& # - Fixed examples.
555\& # changes/bugs fixed since 02sep94 version:
556\& # - Moved to Class::Template.
557\& # changes/bugs fixed since 20feb94 version:
558\& # - Updated to be a more proper module.
559\& # - Added "use strict".
560\& # - Bug in build_methods, was using @var when @$var needed.
561\& # - Now using my() rather than local().
562\& #
563\& # Uses perl5 classes to create nested data types.
564\& # This is offered as one implementation of Tom Christiansen's "structs.pl"
565\& # idea.
566.Ve