NAME
Class::Struct::FIELDS - Combine Class::Struct and fields
SYNOPSIS
use Class::Struct::FIELDS;
# declare struct, based on fields, explicit class name:
struct (CLASS_NAME => { ELEMENT_NAME => ELEMENT_TYPE, ... });
use Class::Struct::FIELDS;
# declare struct, based on fields, explicit class name
# with inheritance:
struct (CLASS_NAME => [qw(BASE_CLASSES ...)],
{ ELEMENT_NAME => ELEMENT_TYPE, ... });
package CLASS_NAME;
use Class::Struct::FIELDS;
# declare struct, based on fields, implicit class name:
struct (ELEMENT_NAME => ELEMENT_TYPE, ...);
package CLASS_NAME;
use Class::Struct::FIELDS;
# declare struct, based on fields, implicit class name
# with inheritance:
struct ([qw(BASE_CLASSES ...)], ELEMENT_NAME => ELEMENT_TYPE, ...);
package MyObj;
use Class::Struct::FIELDS;
# declare struct with four types of elements:
struct (s => '$', a => '@', h => '%', x => '&', c => 'My_Other_Class');
$obj = new MyObj; # constructor
# scalar type accessor:
$element_value = $obj->s; # element value
$obj->s ('new value'); # assign to element
# array type accessor:
$ary_ref = $obj->a; # reference to whole array
$ary_element_value = $obj->a->[2]; # array element value
$ary_element_value = $obj->a (2); # same thing
$obj->a->[2] = 'new value'; # assign to array element
$obj->a (2, 'newer value'); # same thing
# hash type accessor:
$hash_ref = $obj->h; # reference to whole hash
$hash_element_value = $obj->h->{x}; # hash element value
$hash_element_value = $obj->h (x); # same thing
$obj->h->{x} = 'new value'; # assign to hash element
$obj->h (x, 'newer value'); # same thing
# code type accessor:
$code_ref = $obj->x; # reference to code
$obj->x->(...); # call code
$obj->x (sub {...}); # assign to element
# regexp type accessor:
$regexp = $obj->r; # reference to code
$string =~ m/$obj->r/; # match regexp
$obj->r (qr/ ... /); # assign to element
# class type accessor:
$element_value = $obj->c; # object reference
$obj->c->method (...); # call method of object
$obj->c (My_Other_Class::->new); # assign a new object
DESCRIPTION
Class::Struct::FIELDS
exports a single function, struct
. Given a list of element names and types, and optionally a class name and/or an array reference of base classes, struct
creates a Perl 5 class that implements a "struct-like" data structure with inheritance.
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.)
Each element's type can be scalar, array, hash, code or class.
Differences from Class::Struct
and fields
Class::Struct::FIELDS
is a combination of Class::Struct
, base
and fields
.
Unlike Class::Struct
, inheritance is explicitly supported. One result is that you may no longer use the array ([]
) notation for indicating internal representation. Also, Class::Struct::FIELDS
relies on fields
for internal representation.
Also, Class::Struct::FIELDS
supports code and regular expression elements. (Class::Struct
handles code and regular expressions as scalars.)
Unlike fields
, each element has a data type, and is automatically created at first access.
The struct()
function
The struct
function has two forms of parameter-list:
struct (CLASS_NAME => { ELEMENT_LIST });
struct (ELEMENT_LIST);
The first form explicitly identifies the name of the class being created. The second form assumes the current package name as the class name.
Optionally, you may specify base classes with an array reference as the first non-class-name argument:
struct (CLASS_NAME => [qw(BASE_CLASSES ...)], { ELEMENT_LIST });
struct (CLASS_NAME => [qw(BASE_CLASSES ...)], ELEMENT_LIST);
struct ([qw(BASE_CLASSES ...)], { ELEMENT_LIST });
struct ([qw(BASE_CLASSES ...)], ELEMENT_LIST);
(Since there is no ambiguity between CLASS_NAME and ELEMENT_LIST with the interposing array reference, you may always make ELEMENT_LIST a list or a hash reference with this form.)
The class created by struct
may be either a subclass or superclass of other classes. See base and fields for details.
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. XXX
struct
returns the name of the newly-constructed package.
Element Types and Accessor Methods
The five element types -- scalar, array, hash, code and class -- are represented by strings -- $
, @
, %
, &
, /
and a class name.
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 by default is initialized to
undef
(but see "Initializing with new").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\$
or*$
, a reference to the element is returned. - Array (
@
,\@
or*@
) -
The element is an array, initialized by default to
()
.With no argument, the accessor returns a reference to the element's whole array (whether or not the element was specified as
@
,\@
or*@
).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\@
or*@
, a reference to the array element is returned. - Hash (
%
,\%
or*%
) -
The element is a hash, initialized by default to
()
.With no argument, the accessor returns a reference to the element's whole hash (whether or not the element was specified as
%
,\%
or*%
).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\%
or*%
, a reference to the hash element is returned. - Code (
&
,\&
or*&
) -
The element is code, and by default is initialized to
undef
(but see "Initializing with new").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\&
or*&
, a reference to the element is returned. (It is unclear of what value this facility is. XXX) - Regexp (
/
,\/
or*/
) -
If the element type is
/
, the value of the element (after assignment) is returned. If the element type is\/
or*/
, a reference to the element is returned. (It is unclear of what value this facility is. XXX)Regular expressions really are special in that you create them with special syntax, not with a call to a constructor:
$obj->r (qr/^$/); # fine $obj->r (Regexp->new); # WRONG
- Class (
Class_Name
,\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
\
or*
, the accessor returns the element value (after assignment). If the element type starts with a\
or*
, a reference to the element itself is returned.The class is automatically required for you so that, for example, you can safely write:
struct MyObj {io => 'IO::Scalar'};
and access
io
immediately. The same applies for nested structs:BEGIN { struct Alice { when => '$' }; struct Bob { who => 'Alice' }; } my Bob $b = Bob::->new; $b->who->when ('what');
Note, however, the
BEGIN
block so that this example can use the "my Dog $spot
" syntax formy Bob $b
. Also, no actual import happens for the caller -- the automatic use is only for convenience in auto-constructing members, not magic. Another way to do this is:{ package Bob; use Class::Struct::FIELDS; struct } my Bob $b = Bob::->new;
- What about globs (
*
) and other funny types? -
At present,
Class::Struct::FIELDS
does not support special notation for other intrinsic types. Use a scalar to hold a reference to globs and other unusual specimens, or wrap them in a class such asIO::Handle
(globs). XXX
Initializing with new
struct
always creates a constructor called new
. That constructor may take a list of initializers for the various elements of the new struct.
Each initializer is a pair of values: element name =>
value. The initializer value for a scalar element is just a scalar value. The initializer for an array element is an array reference. The initializer for a hash is a hash reference. The initializer for code is a code reference.
The initializer for a class element is also a hash reference, and the contents of that hash are passed to the element's own constructor.
new
tries to be as clever as possible in deducing what type of object to construct. All of these are valid:
{ package Bob; use Class::Struct::FIELDS; struct }
my Bob $b = Bob::->new; # good style
my Bob $b2 = $b->new; # works fine
my Bob $b3 = &Bob::new; # if you insist
my Bob $b4 = Bob::new (apple => 3, banana => 'four'); # WRONG!
The last case doesn't behave as hoped for: new
tries to construct an object of package apple
(and hopefully fails, unless you actually have a package named apple
), not an object of package Bob
.
See Example 3 below for an example of initialization.
Initializing with init
You may also use init
as a constructor to assign initial values to new objects. (In fact, this is the preferred method.) struct
will see to it that you have a ready object to work with, and pass you any arguments used in the call to new
:
sub init {
my MyObj $self = shift;
@self->a->[0..3] = (a..d);
return $self;
}
It is essential that you return an object from init
, as this is returned to the caller of new
. You may return a different object if you wish, but this would be rather uncommon.
First, new
arranges for any constructor argument list to be processed first before calling init
.
Second, new
arranges to call init
for base classes, calling them in bottom-up order, before calling init
. This is so that ancestors may construct an object before descendents.
There is no corresponding facility for DESTROY. XXX
Private fields
Fields starting with a leading underscore, _
, are private: they are still valid fields, but Class::Struct::FIELDS
does not create subroutines to access them. Instead, you should access them the usual way for hash members:
$self->{_private_key}; # ok
$self->_private_key; # Compilation error
See fields for more details.
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), andrusage
has two elements, each of which is of typetimeval
.use Class::Struct::FIELDS; struct (rusage => { ru_utime => timeval, # seconds ru_stime => timeval, # microseconds }); struct (timeval => { tv_secs => '$', tv_usecs => '$', }); # create an object: my $t = new rusage; # $t->ru_utime and $t->ru_stime are objects of type timeval. # set $t->ru_utime to 100.0 sec and $t->ru_stime to 5.0 sec. $t->ru_utime->tv_secs (100); $t->ru_utime->tv_usecs (0); $t->ru_stime->tv_secs (5); $t->ru_stime->tv_usecs (0);
- Example 2
-
An accessor function can be redefined in order to provide additional checking of values, etc. Here, we want the
count
element always to be nonnegative, so we redefine thecount
accessor accordingly.package MyObj; use Class::Struct::FIELDS; # declare the struct struct (MyObj => {count => '$', stuff => '%'}); # override the default accessor method for 'count' sub count { my MyObj $self = shift; if (@_) { die 'count must be nonnegative' if $_[0] < 0; $self->{count} = shift; warn "Too many args to count" if @_; } return $self->{count}; } package main; $x = new MyObj; print "\$x->count (5) = ", $x->count (5), "\n"; # prints '$x->count (5) = 5' print "\$x->count = ", $x->count, "\n"; # prints '$x->count = 5' print "\$x->count (-5) = ", $x->count (-5), "\n"; # dies due to negative argument!
- Example 3
-
The constructor of a generated class can be passed a list of element=>value pairs, with which to initialize the struct. If no initializer is specified for a particular element, its default initialization is performed instead. Initializers for non-existent elements are silently ignored.
Note that the initializer for a nested struct is specified as an anonymous hash of initializers, which is passed on to the nested struct's constructor.
use Class::Struct::FIELDS; struct Breed => { name => '$', cross => '$', }; struct Cat => { name => '$', kittens => '@', markings => '%', breed => 'Breed', }; my $cat = Cat->new (name => 'Socks', kittens => ['Monica', 'Kenneth'], markings => { socks => 1, blaze => "white" }, breed => { name => 'short-hair', cross => 1 }); print "Once a cat called ", $cat->name, "\n"; print "(which was a ", $cat->breed->name, ")\n"; print "had two kittens: ", join(' and ', @{$cat->kittens}), "\n";
- Example 4
-
Class::Struct::FIELDS
has a very elegant idiom for creating inheritance trees:use Class::Struct::FIELDS; struct Fred => []; struct Barney => [qw(Fred)]; struct Wilma => [qw(Barney)], aa => '@', bb => 'IO::Scalar';
That's all the code it takes!
EXPORTS
struct
DIAGNOSTICS
The following are diagnostics generated by Class::Struct::Fields. Items marked "(W)" are non-fatal (invoke Carp::carp
); those marked "(F)" are fatal (invoke Carp::croak
).
- 'struct' usage error
-
(F) The caller failed to read the documentation for
Class::Struct::FIELDS
and follow the advice therein. - Accessor '%s' defined in package '%s' hides method in base class
-
(W) There is already a subroutine, with the name of one of the accessors, located in a base class of the given package. You should consider renaming the field with the given name.
- Method '%s' defined in package '%s' overrides accessor
-
(W) There is already a subroutine, with the name of one of the accessors, located in the given package. You may have intended this, however, if defining your own custom accessors.
- Method 'new' already defined in package '%s'
-
(W) There is already a 'new' subroutine located in the given package. As long as the caveats for defining your own
new
are followed, this warning is harmless; otherwise your objects may not be properly initialized.
BUGS AND CAVEATS
NB -- Talk about this superceding Class::Class
. Or explicitly rename this package.
CREDITS
This documentation is amazingly like that of Class::Struct
. I wonder why. Credit to Dr. Damian Conway <damian@conway.org>.
AUTHOR
B. K. Oxley (binkley) <binkley@bigfoot.com>
Copyright (c) 2000 B. K. Oxley (binkley). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
- Class::Contract
-
Class::Contract
is an extension module by Damian Conway for writing in a design-by-contract object-oriented style. - Class::Struct
-
Class::Struct
is a standard module for creating simple, non-inherited data structures. - base
-
base
is a standard module for establishing IS-A relationships with base classes at compile time. - fields
-
fields
is a standard module for imbuing your class with efficient pseudo-hashes for data members.