# man > Class::Struct

---
type: CommandReference
command: Class::Struct
mode: perldoc
section: 3
source: perldoc
---

## Quick Reference
- `struct( CLASS_NAME => [ ELEMENT => TYPE, ... ] );` — declare array-based struct
- `struct( CLASS_NAME => { ELEMENT => TYPE, ... } );` — declare hash-based struct
- `use Class::Struct CLASS_NAME => [ ELEMENT => TYPE, ... ];` — compile-time declaration
- `$obj = CLASS_NAME->new( elem => $val, ... );` — constructor with initializers
- `$obj->scalar_element('value');` / `$value = $obj->scalar_element;` — scalar accessor
- `$obj->array_element(2, 'new');` / `$ary_ref = $obj->array_element;` — array accessor
- `$obj->hash_element('key', 'val');` / `$hash_ref = $obj->hash_element;` — hash accessor
- `$obj->class_element->method();` / `$obj->class_element(new OtherClass);` — class-type accessor

## Name
Class::Struct – declare struct-like datatypes as Perl classes

## Synopsis
perl
# Explicit class name, array‑based
use Class::Struct;
struct( CLASS_NAME => [ ELEMENT_NAME => ELEMENT_TYPE, ... ] );

# Explicit class name, hash‑based
struct( CLASS_NAME => { ELEMENT_NAME => ELEMENT_TYPE, ... } );

# Implicit class name (current package), array‑based
package CLASS_NAME;
use Class::Struct;
struct( ELEMENT_NAME => ELEMENT_TYPE, ... );

# Compile‑time creation
use Class::Struct CLASS_NAME => [ ... ];
use Class::Struct CLASS_NAME => { ... };
package CLASS_NAME;
use Class::Struct ELEMENT_NAME => ELEMENT_TYPE, ... ;
## Options

### `struct()` function forms
- `struct( CLASS_NAME, [ LIST ] )` — array‑based class; faster, smaller.
- `struct( CLASS_NAME, { LIST } )` — hash‑based class; more flexible.
- `struct( LIST )` — uses current package name as class; array‑based.

### Element type strings
- `'$'` — scalar element; accessor returns the value after assignment.
- `'*$'` — scalar element; accessor returns a reference to the element.
- `'@'` — array element; accessor returns the value of an indexed element (with args), or a reference to the whole array (no args).
- `'*@'` — like `'@'` but returns a reference to the array element when called with index.
- `'%'` — hash element; accessor returns value of a key (with args), or reference to whole hash (no args).
- `'*%'` — like `'%'` but returns a reference to the hash element when called with key.
- `'ClassName'` — element must be a blessed object of that class; accessor returns the object.
- `'*ClassName'` — returns a reference to the element itself.
- Prefixing with `*` on any type returns a reference instead of the value.

### Accessor overrides
- A declared element may be overridden by defining a `sub` of the same name in the package.
- The `new` constructor must not be overridden.

## Examples

### Nested structs
perl
use Class::Struct;

struct( Timeval => [ tv_secs => '$', tv_usecs => '$' ] );
struct( Rusage  => { ru_utime => 'Timeval', ru_stime => 'Timeval' } );

my $t = Rusage->new(
    ru_utime => Timeval->new(),
    ru_stime => Timeval->new(),
);
$t->ru_utime->tv_secs(100);
$t->ru_stime->tv_secs(5);
### Custom accessor with validation
perl
package MyObj;
use Class::Struct;
struct( 'MyObj', { count => '$', stuff => '%' } );

sub count {
    my $self = shift;
    if (@_) {
        die 'count must be nonnegative' if $_[0] < 0;
        $self->{'MyObj::count'} = shift;
    }
    return $self->{'MyObj::count'};
}

my $x = MyObj->new;
$x->count(5);          # sets to 5
print $x->count;       # prints 5
$x->count(-5);         # dies
### Constructor initializers
perl
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 },  # pass hash ref to nested ctor
);

print $cat->name;                        # Socks
print $cat->breed->name;                 # short-hair
print join(' and ', @{ $cat->kittens }); # Monica and Kenneth
## See Also
- [perlref](http://localhost/phpMan.php/perldoc/perlref/markdown) – Perl references and nested data structures
- [perlootut](http://localhost/phpMan.php/perldoc/perlootut/markdown) – Object-Oriented Programming in Perl Tutorial
- [perlobj](http://localhost/phpMan.php/perldoc/perlobj/markdown) – Perl object reference and `bless`
- [Class::MethodMaker](http://localhost/phpMan.php/perldoc/Class%3A%3AMethodMaker/markdown) – alternative class builder

## Exit Codes
Not applicable.