NAME

Class::XSReader - reader functions in XS

SYNOPSIS

package Address {
  use Class::XSConstructor qw( number street city region postcode );
  use Class::XSReader      qw( number street city region postcode );
  use Class::XSDestructor;
}

DESCRIPTION

Class::XSAccessor should usually be preferred over this module.

However, this module adds support for lazy defaults/builders (with optional type constraints and coercions).

use Types::Common qw( Str );
use Class::XSReader
  name => {
    reader  => "get_name",
    default => "Anonymous",
    isa     => Str,
    coerce  => sub { "$_" },
  };

Note that because this is a reader method only (not a writer/setter), the type constraints and coercions are only used on the lazy default. This makes them not especially useful, unless you suspect your default will sometimes return invalid data.

You can set:

use Class::XSReader
  name => {
    reader  => "get_name",
    default => "Anonymous",
    lazy    => false,
  };

In which case the default is taken to be eager and the responsibility of a constructor. It basically means that the default will be ignored, and therefore any type constraint or coercion will be too. lazy => true is assumed when the lazy option isn't specified.

Builders can be used:

use Class::XSConstructor qw( first_name! last_name! full_name );
use Class::XSReader
  qw( first_name last_name ),
  full_name => { builder => '_build_full_name' };

sub _build_full_name ( $self ) {
  return sprintf( '%s %s', $self->first_name, $self->last_name );
}

SEE ALSO

Class::XSAccessor.

Class::XSConstructor, Class::XSDestructor, Class::XSAccessor.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2026 by Toby Inkster.

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

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.