NAME

slot - Simple, efficient, comple-time class declaration

VERSION

version 0.01

SYNOPSIS

package Point;
use Types::Standard -types;

use slot x => Int, rw => 1, req => 1;
use slot y => Int, rw => 1, req => 1;
use slot z => Int, rw => 1, def => 0;

1;

my $p = Point->new(x => 10, y => 20);
$p->x(30); # x is set to 30
$p->y;     # 20
$p->z;     # 0

DESCRIPTION

Similar to the fields pragma, slot declares individual fields in a class, building a constructor and slot accessor methods.

Although not nearly as full-featured as other solutions, slot is light-weight, fast, works with basic Perl objects, and imposes no dependencies outside of the Perl core distribution. Currently, only the unit tests require non-core packages.

slot is intended for use with Perl's bare metal objects. It provides a simple mechanism for building accessor and constructor code at compile time.

It does not provide inheritance; that is done by setting @ISA or via the base or parent pragmas.

It does not provide method wrappers; that is done with the SUPER pseudo-class.

It does build a constructor method, new, with support for default and required slots as keyword arguments and type validation of caller-supplied values.

It does build accesor methods (reader or combined reader/writer, using the slot's name) for each slot declared, with support for type validation.

CONSTRUCTOR

slot generates a constructor method named new. If there is already an existing method with that name, it may be overwritten, depending on the order in which slot was imported.

Because slots are declared individually, the constructor as well as the accessor methods are generated on the first call to new.

DECLARING SLOTS

The pragma itself accepts two positional parameters: the slot name and optional type. The type is validated during construction and in the setter, if the slot is read-write.

Slot names must be valid perl identifiers suitable for subroutine names. Types must be an instance of a class that supports the check and inline_check methods (see "Inlining methods" in Type::Tiny).

OPTIONS

rw

When true, the accessor method accepts a single parameter to modify the slot value. If the slot declares a type, the accessor will croak if the new value does not validate.

req

When true, this constructor will croak if the slot is missing from the named parameters passed to the constructor. If the slot also declares a default value, this attribute is moot.

def

When present, this value or code ref which returns a value is used as the default if the slot is missing from the named parameters passed to the constructor.

If the default is a code ref which generates a value and a type is specified, note that the code ref will be called during compilation to validate its type rather than re-validating it with every accessor call.

INHERITANCE

When a class declares a slot which is also declared in the parent class, the parent class' settings are overridden. Any options not included in the overriding class' slot declaration remain in effect in the child class.

package A;

use slot 'foo', rw => 1;
use slot 'bar', req => 1, rw => 1;

1;

package B;

use parent -norequire, 'A';

use slot 'foo', req => 1; # B->foo is req, inherits rw
use slot 'bar', rw => 0;  # B->bar inherits req, but is no longer rw

1;

DEBUGGING

Adding use slot -debug to your class will cause slot to print the generated constructor and accessor code when new is first called.

PERFORMANCE

slot is designed to be fast and have a low overhead. When available, Class::XSAccessor is used to generate the class accessors. This applies to slots that are not writable or are writable but have no declared type.

A minimal benchmark on my admittedly underpowered system compares Moose, Moo, and slot. The test includes multiple setters using a mix of inherited, typed and untyped, attributes, which ammortizes the benefit of Class::XSAccessor to Moo and slot.

|           Rate   moo moose  slot
| moo   355872/s    --  -51%  -63%
| moose 719424/s  102%    --  -25%
| slot  961538/s  170%   34%    --

Oddly, Moo seemed to perform better running the same test without Class::XSAccessor installed.

|           Rate   moo moose  slot
| moo   377358/s    --  -50%  -56%
| moose 757576/s  101%    --  -12%
| slot  862069/s  128%   14%    --

AUTHOR

Jeff Ober <sysread@fastmail.fm>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Jeff Ober.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.