```{index} single: Class::XSAccessor ``` # Class::XSAccessor ```{pperl-module-badges} Class::XSAccessor ``` `Class::XSAccessor` - fast accessors, getters, setters, predicates and constructors for hash- and array-based objects, without compiling any Perl at runtime. ```perl package MyClass; use Class::XSAccessor constructor => 'new', accessors => { name => 'name' }, # $obj->name / $obj->name($v) getters => { get_age => 'age' }, setters => { set_age => 'age' }, predicates => { has_age => 'age' }, # defined $obj->{age} chained => 1; # setters return $self package MyArrayClass; use Class::XSAccessor::Array constructor => 'new', accessors => { name => 0 }; # $obj->[0] ``` Every generated method is a compiled subroutine that reads or writes one slot of the object directly. Moo and other object systems use it automatically when it is installed, for their plain attribute accessors. This is a native implementation of the CPAN distribution Class-XSAccessor 1.19; its `Class/XSAccessor.pm`, `Class/XSAccessor/Array.pm` and `Class/XSAccessor/Heavy.pm` are used unchanged. Like the XS original it rewrites a method call site the first time a generated accessor runs there, so later calls from that site skip the general subroutine-call machinery. ## Modules - [`Class::XSAccessor::Array`](XSAccessor/Array.md) - Generate fast native accessors without runtime compilation - [`Class::XSAccessor::Heavy`](XSAccessor/Heavy.md) - Guts you don't care about ## Functions ### Other Functions #### `getter` `$obj->name`: the value of the object's key, or undef. #### `lvalue_accessor` `$obj->name = $value`: the slot itself, assignable. #### `setter` `$obj->set_name($value)`: stores a copy, returns the new value. #### `chained_setter` `$obj->set_name($value)` under `chained => 1`: returns the object. #### `accessor` `$obj->name` / `$obj->name($value)`: get, or set and return the new value. #### `chained_accessor` The accessor under `chained => 1`: setting returns the object. #### `exists_predicate` `$obj->has_name` from `exists_predicates`: whether the key exists. #### `defined_predicate` `$obj->has_name` from `predicates` / `defined_predicates`: whether the value is defined. #### `constructor` `MyClass->new(key => value, ...)`: a hash-based object holding copies of the pairs; an odd count dies. #### `constant_false` A method from `false => [...]`: always false. #### `constant_true` A method from `true => [...]`: always true. #### `test` The `__tests__` accessor: `accessor`, narrating the entersub optimisation through warn() for t/08hash_entersub.t. #### `newxs_defined_predicate` `newxs_getter(name, key)` and its ALIASes newxs_lvalue_accessor (1), newxs_predicate (2), newxs_defined_predicate (3), newxs_exists_predicate (4): install one read-side method. Also available as `Class::XSAccessor::newxs_exists_predicate`, `Class::XSAccessor::newxs_getter`, `Class::XSAccessor::newxs_lvalue_accessor`, `Class::XSAccessor::newxs_predicate`. #### `newxs_accessor` `newxs_setter(name, key, chained)` and its ALIAS newxs_accessor (1). Also available as `Class::XSAccessor::newxs_setter`. #### `newxs_constructor` `newxs_constructor(name)`. #### `newxs_boolean` `newxs_boolean(name, truth)`. #### `newxs_test` `newxs_test(name, key)`. #### `array_setter_init` `array_setter_init`: the compat setter that optimises its call site. #### `array_setter` `array_setter`: what an optimised compat setter call site calls. #### `array_accessor_init` `array_accessor_init`: the compat accessor that optimises its call site. #### `array_accessor` `array_accessor`: what an optimised compat accessor call site calls. #### `END` `Class::XSAccessor::END`: does nothing. Freeing the key table here is commented out upstream - accessors may still run after END blocks. ```{toctree} :hidden: :maxdepth: 1 XSAccessor/Array XSAccessor/Heavy ```