DESCRIPTION

Class::Struct exports a single function, struct. Given a list of element names and types, and optionally a class name, struct creates a Perl 5 class that implements a "struct-like" data structure.

The new class is given a constructor method, new, for creating struct objects.

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. (See Example 2.)

The struct() function

The first and second forms explicitly identify the name of the class being created. The third form assumes the current package name as the class name.

An object of a class created by the first and third forms is based on an array, whereas an object of a class created by the second form is based on a hash. The array-based forms will be somewhat faster and smaller; the hash-based forms are more flexible.

The class created by struct must not be a subclass of another class other than UNIVERSAL.

A function named new must not be explicitly defined in a class created by struct.

The ELEMENT_LIST has the form

NAME => TYPE, ...

Each name-type pair declares one element of the struct. Each element name will be defined as an accessor method unless a method by that name is explicitly defined; in the latter case, a warning is issued if the warning flag (-w) is set.

Element Types and Accessor Methods

The four element types -- scalar, array, hash, and class -- are represented by strings -- '$', '@', '%', and a class name -- optionally preceded by a '*'.

The accessor method provided by struct for an element depends on the declared type of the element.

Scalar ('$' or '*$')

The element is a scalar, and is initialized to undef.

The accessor's argument, if any, is assigned to the element.

If the element type is '$', the value of the element (after assignment) is returned. If the element type is '*$', a reference to the element is returned.

Array ('@' 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 assigned to the array element. If the element type is '@', the accessor returns the array element value. If the element type is '*@', a reference to the array element is returned.

Hash ('%' 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 assigned to the hash element. If the element type is '%', the accessor returns the hash element value. If the element type is '*%', a reference to the hash element is returned.

Class ('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 assigned to the element. The accessor will croak if this is not an appropriate object reference.

If the element type does not start with a '*', the accessor returns the element value (after assignment). If the element type starts with a '*', a reference to the element itself is returned.

EXAMPLES

Example 1

Giving a struct element a class type that is also a struct is how structs are nested. Here, timeval represents a time (seconds and microseconds), and rusage has two elements, each of which is of type timeval.