NAME

Class::XSDelegation - delegations in XS

SYNOPSIS

package Address {
  use Class::XSConstructor qw( number street city region postcode );
  use Class::XSDestructor;
  use Class::XSAccessor {
    accessors => [qw( number street city region postcode )],
  };
}

package Person {
  use Class::XSConstructor qw( name! age email phone address );
  use Class::XSDestructor;
  use Class::XSAccessor {
    accessors         => [qw( name age email phone address )],
    exists_predicates => [qw(      age email phone address )],
  };
  use Class::XSDelegation [ 'locality' => 'address' => 'city' ];
}

The delegation is equivalent to:

sub Person::locality {
  return shift->{address}->city( @_ );
}

DESCRIPTION

The constructor may be passed a list of arrayrefs. Each arrayref consists of:

[ $local_method, $handler, $handler_method, \%options ]

Valid options are:

{
  curry       => ARRAYREF,
  is_try      => BOOL,
  is_accessor => BOOL,
}

If an arrayref is provided to curry, then the generated method is:

sub Person::locality {
  return shift->{address}->city( @curried, @_ );
}

If is_try is true, then the method will quietly return undef if shift->{address} isn't a blessed object.

If is_accessor is true, then the generated method is:

sub Person::locality {
  return shift->address->city( @_ );
}

... so it calls the address accessor method instead of accessing the object hashref directly. This is useful if address has a lazily built default or has any more complex logic.

The package to create these methods is usually determined by caller, but you can override it using:

use Class::XSDelegation \"Some::Package", [ ... ];

SEE ALSO

Class::XSConstructor, Class::XSDestructor, Class::XSReader, 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.