8.43. Class::StructFormerly named Class::Template; exports a single function, struct. struct takes a list of element names and types, and optionally a class name, and creates a Perl class that implements a struct-like data structure. It also creates a constructor method, new, for creating objects of the class (so a class created this way must not itself define a new method). Each element in the struct data has an accessor method, which is used to assign to the element and to fetch its value. The default accessor can be overridden by declaring a sub of the same name in the package. Each element's type can be scalar, array, hash, or class.
struct (paramlist) Creates a class, with object methods for each element of the class. The parameter list paramlist can take one of three forms: struct( CLASS_NAME => [ ELEMENT_LIST ]); # Object is array-based struct( CLASS_NAME => { ELEMENT_LIST }); # Object is hash-based struct( ELEMENT_LIST ); # Class name is current, and package name # object is array-based The array-based element lists are faster and smaller, but the hash-based list is more flexible. The class that is created can be a subclass of the UNIVERSAL class, but not of any other class. Element listThe items in the ELEMENT_LIST are of the form: NAME => TYPE, ... in which each NAME=>TYPE pair declares one element of the struct. Each element name is defined as an accessor method, unless a method is explicitly defined with that name. (In that case, a warning is issued if the -w flag is set.) Element types and accessor methodsThere are four possible element types, each represented by a string. Each string may start with an asterisk (*), indicating that a reference to the element will be returned. The type of an element determines the accessor method provided. The following list shows the element types, the strings that represent them, and the accessor:
Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|