Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / Class / Struct.html
blob697a76efa079558bdac8023499b403ecae97408a
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>Class::Struct - declare struct-like datatypes as Perl classes</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;Class::Struct - declare struct-like datatypes as Perl classes</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <ul>
27 <li><a href="#the_struct___function">The <code>struct()</code> function</a></li>
28 <li><a href="#class_creation_at_compile_time">Class Creation at Compile Time</a></li>
29 <li><a href="#element_types_and_accessor_methods">Element Types and Accessor Methods</a></li>
30 <li><a href="#initializing_with_new">Initializing with <code>new</code></a></li>
31 </ul>
33 <li><a href="#examples">EXAMPLES</a></li>
34 <li><a href="#author_and_modification_history">Author and Modification History</a></li>
35 </ul>
36 <!-- INDEX END -->
38 <hr />
39 <p>
40 </p>
41 <h1><a name="name">NAME</a></h1>
42 <p>Class::Struct - declare struct-like datatypes as Perl classes</p>
43 <p>
44 </p>
45 <hr />
46 <h1><a name="synopsis">SYNOPSIS</a></h1>
47 <pre>
48 use Class::Struct;
49 # declare struct, based on array:
50 struct( CLASS_NAME =&gt; [ ELEMENT_NAME =&gt; ELEMENT_TYPE, ... ]);
51 # declare struct, based on hash:
52 struct( CLASS_NAME =&gt; { ELEMENT_NAME =&gt; ELEMENT_TYPE, ... });</pre>
53 <pre>
54 package CLASS_NAME;
55 use Class::Struct;
56 # declare struct, based on array, implicit class name:
57 struct( ELEMENT_NAME =&gt; ELEMENT_TYPE, ... );</pre>
58 <pre>
59 # Declare struct at compile time
60 use Class::Struct CLASS_NAME =&gt; [ ELEMENT_NAME =&gt; ELEMENT_TYPE, ... ];
61 use Class::Struct CLASS_NAME =&gt; { ELEMENT_NAME =&gt; ELEMENT_TYPE, ... };</pre>
62 <pre>
63 # declare struct at compile time, based on array, implicit class name:
64 package CLASS_NAME;
65 use Class::Struct ELEMENT_NAME =&gt; ELEMENT_TYPE, ... ;</pre>
66 <pre>
67 package Myobj;
68 use Class::Struct;
69 # declare struct with four types of elements:
70 struct( s =&gt; '$', a =&gt; '@', h =&gt; '%', c =&gt; 'My_Other_Class' );</pre>
71 <pre>
72 $obj = new Myobj; # constructor</pre>
73 <pre>
74 # scalar type accessor:
75 $element_value = $obj-&gt;s; # element value
76 $obj-&gt;s('new value'); # assign to element</pre>
77 <pre>
78 # array type accessor:
79 $ary_ref = $obj-&gt;a; # reference to whole array
80 $ary_element_value = $obj-&gt;a(2); # array element value
81 $obj-&gt;a(2, 'new value'); # assign to array element</pre>
82 <pre>
83 # hash type accessor:
84 $hash_ref = $obj-&gt;h; # reference to whole hash
85 $hash_element_value = $obj-&gt;h('x'); # hash element value
86 $obj-&gt;h('x', 'new value'); # assign to hash element</pre>
87 <pre>
88 # class type accessor:
89 $element_value = $obj-&gt;c; # object reference
90 $obj-&gt;c-&gt;method(...); # call method of object
91 $obj-&gt;c(new My_Other_Class); # assign a new object</pre>
92 <p>
93 </p>
94 <hr />
95 <h1><a name="description">DESCRIPTION</a></h1>
96 <p><code>Class::Struct</code> exports a single function, <code>struct</code>.
97 Given a list of element names and types, and optionally
98 a class name, <code>struct</code> creates a Perl 5 class that implements
99 a ``struct-like'' data structure.</p>
100 <p>The new class is given a constructor method, <code>new</code>, for creating
101 struct objects.</p>
102 <p>Each element in the struct data has an accessor method, which is
103 used to assign to the element and to fetch its value. The
104 default accessor can be overridden by declaring a <code>sub</code> of the
105 same name in the package. (See Example 2.)</p>
106 <p>Each element's type can be scalar, array, hash, or class.</p>
108 </p>
109 <h2><a name="the_struct___function">The <code>struct()</code> function</a></h2>
110 <p>The <code>struct</code> function has three forms of parameter-list.</p>
111 <pre>
112 struct( CLASS_NAME =&gt; [ ELEMENT_LIST ]);
113 struct( CLASS_NAME =&gt; { ELEMENT_LIST });
114 struct( ELEMENT_LIST );</pre>
115 <p>The first and second forms explicitly identify the name of the
116 class being created. The third form assumes the current package
117 name as the class name.</p>
118 <p>An object of a class created by the first and third forms is
119 based on an array, whereas an object of a class created by the
120 second form is based on a hash. The array-based forms will be
121 somewhat faster and smaller; the hash-based forms are more
122 flexible.</p>
123 <p>The class created by <code>struct</code> must not be a subclass of another
124 class other than <code>UNIVERSAL</code>.</p>
125 <p>It can, however, be used as a superclass for other classes. To facilitate
126 this, the generated constructor method uses a two-argument blessing.
127 Furthermore, if the class is hash-based, the key of each element is
128 prefixed with the class name (see <em>Perl Cookbook</em>, Recipe 13.12).</p>
129 <p>A function named <code>new</code> must not be explicitly defined in a class
130 created by <code>struct</code>.</p>
131 <p>The <em>ELEMENT_LIST</em> has the form</p>
132 <pre>
133 NAME =&gt; TYPE, ...</pre>
134 <p>Each name-type pair declares one element of the struct. Each
135 element name will be defined as an accessor method unless a
136 method by that name is explicitly defined; in the latter case, a
137 warning is issued if the warning flag (<strong>-w</strong>) is set.</p>
139 </p>
140 <h2><a name="class_creation_at_compile_time">Class Creation at Compile Time</a></h2>
141 <p><code>Class::Struct</code> can create your class at compile time. The main reason
142 for doing this is obvious, so your class acts like every other class in
143 Perl. Creating your class at compile time will make the order of events
144 similar to using any other class ( or Perl module ).</p>
145 <p>There is no significant speed gain between compile time and run time
146 class creation, there is just a new, more standard order of events.</p>
148 </p>
149 <h2><a name="element_types_and_accessor_methods">Element Types and Accessor Methods</a></h2>
150 <p>The four element types -- scalar, array, hash, and class -- are
151 represented by strings -- <code>'$'</code>, <code>'@'</code>, <code>'%'</code>, and a class name --
152 optionally preceded by a <code>'*'</code>.</p>
153 <p>The accessor method provided by <code>struct</code> for an element depends
154 on the declared type of the element.</p>
155 <dl>
156 <dt><strong><a name="item_scalar">Scalar (<code>'$'</code> or <code>'*$'</code>)</a></strong>
158 <dd>
159 <p>The element is a scalar, and by default is initialized to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_undef"><code>undef</code></a>
160 (but see <a href="#initializing_with_new">Initializing with new</a>).</p>
161 </dd>
162 <dd>
163 <p>The accessor's argument, if any, is assigned to the element.</p>
164 </dd>
165 <dd>
166 <p>If the element type is <code>'$'</code>, the value of the element (after
167 assignment) is returned. If the element type is <code>'*$'</code>, a reference
168 to the element is returned.</p>
169 </dd>
170 </li>
171 <dt><strong><a name="item_array">Array (<code>'@'</code> or <code>'*@'</code>)</a></strong>
173 <dd>
174 <p>The element is an array, initialized by default to <code>()</code>.</p>
175 </dd>
176 <dd>
177 <p>With no argument, the accessor returns a reference to the
178 element's whole array (whether or not the element was
179 specified as <code>'@'</code> or <code>'*@'</code>).</p>
180 </dd>
181 <dd>
182 <p>With one or two arguments, the first argument is an index
183 specifying one element of the array; the second argument, if
184 present, is assigned to the array element. If the element type
185 is <code>'@'</code>, the accessor returns the array element value. If the
186 element type is <code>'*@'</code>, a reference to the array element is
187 returned.</p>
188 </dd>
189 <dd>
190 <p>As a special case, when the accessor is called with an array reference
191 as the sole argument, this causes an assignment of the whole array element.
192 The object reference is returned.</p>
193 </dd>
194 </li>
195 <dt><strong><a name="item_hash">Hash (<code>'%'</code> or <code>'*%'</code>)</a></strong>
197 <dd>
198 <p>The element is a hash, initialized by default to <code>()</code>.</p>
199 </dd>
200 <dd>
201 <p>With no argument, the accessor returns a reference to the
202 element's whole hash (whether or not the element was
203 specified as <code>'%'</code> or <code>'*%'</code>).</p>
204 </dd>
205 <dd>
206 <p>With one or two arguments, the first argument is a key specifying
207 one element of the hash; the second argument, if present, is
208 assigned to the hash element. If the element type is <code>'%'</code>, the
209 accessor returns the hash element value. If the element type is
210 <code>'*%'</code>, a reference to the hash element is returned.</p>
211 </dd>
212 <dd>
213 <p>As a special case, when the accessor is called with a hash reference
214 as the sole argument, this causes an assignment of the whole hash element.
215 The object reference is returned.</p>
216 </dd>
217 </li>
218 <dt><strong><a name="item_class">Class (<code>'Class_Name'</code> or <code>'*Class_Name'</code>)</a></strong>
220 <dd>
221 <p>The element's value must be a reference blessed to the named
222 class or to one of its subclasses. The element is not initialized
223 by default.</p>
224 </dd>
225 <dd>
226 <p>The accessor's argument, if any, is assigned to the element. The
227 accessor will <code>croak</code> if this is not an appropriate object
228 reference.</p>
229 </dd>
230 <dd>
231 <p>If the element type does not start with a <code>'*'</code>, the accessor
232 returns the element value (after assignment). If the element type
233 starts with a <code>'*'</code>, a reference to the element itself is returned.</p>
234 </dd>
235 </li>
236 </dl>
238 </p>
239 <h2><a name="initializing_with_new">Initializing with <code>new</code></a></h2>
240 <p><code>struct</code> always creates a constructor called <code>new</code>. That constructor
241 may take a list of initializers for the various elements of the new
242 struct.</p>
243 <p>Each initializer is a pair of values: <em>element name</em><code> =&gt; </code><em>value</em>.
244 The initializer value for a scalar element is just a scalar value. The
245 initializer for an array element is an array reference. The initializer
246 for a hash is a hash reference.</p>
247 <p>The initializer for a class element is an object of the corresponding class,
248 or of one of it's subclasses, or a reference to a hash containing named
249 arguments to be passed to the element's constructor.</p>
250 <p>See Example 3 below for an example of initialization.</p>
252 </p>
253 <hr />
254 <h1><a name="examples">EXAMPLES</a></h1>
255 <dl>
256 <dt><strong><a name="item_example_1">Example 1</a></strong>
258 <dd>
259 <p>Giving a struct element a class type that is also a struct is how
260 structs are nested. Here, <code>Timeval</code> represents a time (seconds and
261 microseconds), and <code>Rusage</code> has two elements, each of which is of
262 type <code>Timeval</code>.</p>
263 </dd>
264 <dd>
265 <pre>
266 use Class::Struct;</pre>
267 </dd>
268 <dd>
269 <pre>
270 struct( Rusage =&gt; {
271 ru_utime =&gt; 'Timeval', # user time used
272 ru_stime =&gt; 'Timeval', # system time used
273 });</pre>
274 </dd>
275 <dd>
276 <pre>
277 struct( Timeval =&gt; [
278 tv_secs =&gt; '$', # seconds
279 tv_usecs =&gt; '$', # microseconds
280 ]);</pre>
281 </dd>
282 <dd>
283 <pre>
284 # create an object:
285 my $t = Rusage-&gt;new(ru_utime=&gt;Timeval-&gt;new(), ru_stime=&gt;Timeval-&gt;new());</pre>
286 </dd>
287 <dd>
288 <pre>
289 # $t-&gt;ru_utime and $t-&gt;ru_stime are objects of type Timeval.
290 # set $t-&gt;ru_utime to 100.0 sec and $t-&gt;ru_stime to 5.0 sec.
291 $t-&gt;ru_utime-&gt;tv_secs(100);
292 $t-&gt;ru_utime-&gt;tv_usecs(0);
293 $t-&gt;ru_stime-&gt;tv_secs(5);
294 $t-&gt;ru_stime-&gt;tv_usecs(0);</pre>
295 </dd>
296 </li>
297 <dt><strong><a name="item_example_2">Example 2</a></strong>
299 <dd>
300 <p>An accessor function can be redefined in order to provide
301 additional checking of values, etc. Here, we want the <code>count</code>
302 element always to be nonnegative, so we redefine the <code>count</code>
303 accessor accordingly.</p>
304 </dd>
305 <dd>
306 <pre>
307 package MyObj;
308 use Class::Struct;</pre>
309 </dd>
310 <dd>
311 <pre>
312 # declare the struct
313 struct ( 'MyObj', { count =&gt; '$', stuff =&gt; '%' } );</pre>
314 </dd>
315 <dd>
316 <pre>
317 # override the default accessor method for 'count'
318 sub count {
319 my $self = shift;
320 if ( @_ ) {
321 die 'count must be nonnegative' if $_[0] &lt; 0;
322 $self-&gt;{'MyObj::count'} = shift;
323 warn &quot;Too many args to count&quot; if @_;
325 return $self-&gt;{'MyObj::count'};
326 }</pre>
327 </dd>
328 <dd>
329 <pre>
330 package main;
331 $x = new MyObj;
332 print &quot;\$x-&gt;count(5) = &quot;, $x-&gt;count(5), &quot;\n&quot;;
333 # prints '$x-&gt;count(5) = 5'</pre>
334 </dd>
335 <dd>
336 <pre>
337 print &quot;\$x-&gt;count = &quot;, $x-&gt;count, &quot;\n&quot;;
338 # prints '$x-&gt;count = 5'</pre>
339 </dd>
340 <dd>
341 <pre>
342 print &quot;\$x-&gt;count(-5) = &quot;, $x-&gt;count(-5), &quot;\n&quot;;
343 # dies due to negative argument!</pre>
344 </dd>
345 </li>
346 <dt><strong><a name="item_example_3">Example 3</a></strong>
348 <dd>
349 <p>The constructor of a generated class can be passed a list
350 of <em>element</em>=&gt;<em>value</em> pairs, with which to initialize the struct.
351 If no initializer is specified for a particular element, its default
352 initialization is performed instead. Initializers for non-existent
353 elements are silently ignored.</p>
354 </dd>
355 <dd>
356 <p>Note that the initializer for a nested class may be specified as
357 an object of that class, or as a reference to a hash of initializers
358 that are passed on to the nested struct's constructor.</p>
359 </dd>
360 <dd>
361 <pre>
362 use Class::Struct;</pre>
363 </dd>
364 <dd>
365 <pre>
366 struct Breed =&gt;
368 name =&gt; '$',
369 cross =&gt; '$',
370 };</pre>
371 </dd>
372 <dd>
373 <pre>
374 struct Cat =&gt;
376 name =&gt; '$',
377 kittens =&gt; '@',
378 markings =&gt; '%',
379 breed =&gt; 'Breed',
380 ];</pre>
381 </dd>
382 <dd>
383 <pre>
384 my $cat = Cat-&gt;new( name =&gt; 'Socks',
385 kittens =&gt; ['Monica', 'Kenneth'],
386 markings =&gt; { socks=&gt;1, blaze=&gt;&quot;white&quot; },
387 breed =&gt; Breed-&gt;new(name=&gt;'short-hair', cross=&gt;1),
388 or: breed =&gt; {name=&gt;'short-hair', cross=&gt;1},
389 );</pre>
390 </dd>
391 <dd>
392 <pre>
393 print &quot;Once a cat called &quot;, $cat-&gt;name, &quot;\n&quot;;
394 print &quot;(which was a &quot;, $cat-&gt;breed-&gt;name, &quot;)\n&quot;;
395 print &quot;had two kittens: &quot;, join(' and ', @{$cat-&gt;kittens}), &quot;\n&quot;;</pre>
396 </dd>
397 </li>
398 </dl>
400 </p>
401 <hr />
402 <h1><a name="author_and_modification_history">Author and Modification History</a></h1>
403 <p>Modified by Damian Conway, 2001-09-10, v0.62.</p>
404 <pre>
405 Modified implicit construction of nested objects.
406 Now will also take an object ref instead of requiring a hash ref.
407 Also default initializes nested object attributes to undef, rather
408 than calling object constructor without args
409 Original over-helpfulness was fraught with problems:
410 * the class's constructor might not be called 'new'
411 * the class might not have a hash-like-arguments constructor
412 * the class might not have a no-argument constructor
413 * &quot;recursive&quot; data structures didn't work well:
414 package Person;
415 struct { mother =&gt; 'Person', father =&gt; 'Person'};</pre>
416 <p>Modified by Casey West, 2000-11-08, v0.59.</p>
417 <pre>
418 Added the ability for compile time class creation.</pre>
419 <p>Modified by Damian Conway, 1999-03-05, v0.58.</p>
420 <pre>
421 Added handling of hash-like arg list to class ctor.</pre>
422 <pre>
423 Changed to two-argument blessing in ctor to support
424 derivation from created classes.</pre>
425 <pre>
426 Added classname prefixes to keys in hash-based classes
427 (refer to &quot;Perl Cookbook&quot;, Recipe 13.12 for rationale).</pre>
428 <pre>
429 Corrected behaviour of accessors for '*@' and '*%' struct
430 elements. Package now implements documented behaviour when
431 returning a reference to an entire hash or array element.
432 Previously these were returned as a reference to a reference
433 to the element.</pre>
434 <p>Renamed to <code>Class::Struct</code> and modified by Jim Miner, 1997-04-02.</p>
435 <pre>
436 members() function removed.
437 Documentation corrected and extended.
438 Use of struct() in a subclass prohibited.
439 User definition of accessor allowed.
440 Treatment of '*' in element types corrected.
441 Treatment of classes as element types corrected.
442 Class name to struct() made optional.
443 Diagnostic checks added.</pre>
444 <p>Originally <code>Class::Template</code> by Dean Roehrich.</p>
445 <pre>
446 # Template.pm --- struct/member template builder
447 # 12mar95
448 # Dean Roehrich
450 # changes/bugs fixed since 28nov94 version:
451 # - podified
452 # changes/bugs fixed since 21nov94 version:
453 # - Fixed examples.
454 # changes/bugs fixed since 02sep94 version:
455 # - Moved to Class::Template.
456 # changes/bugs fixed since 20feb94 version:
457 # - Updated to be a more proper module.
458 # - Added &quot;use strict&quot;.
459 # - Bug in build_methods, was using @var when @$var needed.
460 # - Now using my() rather than local().
462 # Uses perl5 classes to create nested data types.
463 # This is offered as one implementation of Tom Christiansen's &quot;structs.pl&quot;
464 # idea.</pre>
465 <table border="0" width="100%" cellspacing="0" cellpadding="3">
466 <tr><td class="block" style="background-color: #cccccc" valign="middle">
467 <big><strong><span class="block">&nbsp;Class::Struct - declare struct-like datatypes as Perl classes</span></strong></big>
468 </td></tr>
469 </table>
471 </body>
473 </html>