struct (
paramlist
)
Creates a class, with object methods for each element of the class. The parameter list paramlist can take one of three forms:
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.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 package name # and object is array-based
The items in the ELEMENT_LIST are of the form:
where eachNAME => TYPE, ...
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.)
There are four possible element types, each represented by a string. Each string may start with an asterisk (
*
), indicating that a reference to the element is to 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:
@
or
*@
)
The element is an array, initialized to
()
. With no argument, the accessor returns a reference to the element's whole array. With one or two arguments, the first argument is an index specifying one element of the array; the second argument, if present, is the value to be assigned to that array element.
Class_Name
or
*Class_Name
)
The element's value must be a reference blessed to the named class or to one of its subclasses. The element is initialized to the result of calling the
new
constructor of the named class. The accessor's argument, if any, is the value to be assigned to the element. The accessor
croak
s if it's not an appropriate object reference.
%
or
*%
)
The element is a hash, initialized to
()
. With no argument, the accessor returns a reference to the element's whole hash. With one or two arguments, the first argument is a key specifying one element of the hash; the second argument, if present, is the value to be assigned to that hash element.
$
or
*$
)
The element is a scalar, initialized to
undef
. The accessor's argument, if any, is assigned to the element.