Sub::HandlesVia::Handler(3) User Contributed Perl Documentation Sub::HandlesVia::Handler(3)

Sub::HandlesVia::Handler - template for a method that can be delegated to

This module is part of Sub::HandlesVia's internal API. It is mostly of interest to people extending Sub::HandlesVia.

This module works in conjunction with Sub::HandlesVia::CodeGenerator and subclasses of Sub::HandlesVia::Toolkit to build a string of Perl code which can be compiled into a method to install into your class.

Standard Moose-like constructor.

Looks up a method from existing handler libraries.

my $h = Sub::HandlesVia::Handler->lookup( 'get', 'Array' );

Curried version:

my $h = Sub::HandlesVia::Handler->lookup( [ 'get', 0 ], 'Array' );

The $trait may be an arrayref of possible traits.

Nothing is exported by default.

Shortcut for the "new" constructor.

use Sub::HandlesVia::Handler 'handler';

my $h = handler( %attr );
# is the same as
my $h = Sub::HandlesVia::Handler->new( %attr );

The name of the function being delegated to.

Indicates whether this handler might mutate an attribute value. The default is to try to detect it based on analysis of the templates.

Specially formatted string (see section below) containing the Perl code to implement the method.

If defined, a shortcut for implementing it when the attribute slot value can be used as an lvalue.

The number of arguments which the method being generated expects (does not include the attibute value itself).

For methods which take a variable number of arguments. If omitted, default to "args".

A signature for said arguments.

A signature to show in documentation, like '$index, $value'. If not provided, will be generated magically from "args", "min_args", and "max_args".

An arrayref of curried arguments.

Whether to force the generated method to be chainable.

Whether to do less validation of input data.

If this handler has to "reset" an attribute value to its default, and the attribute doesn't have a default, this string of Perl code is evaluated to provide a default. An example might be "[]".

Indicates this handler would prefer the code generator to shift $self off @_.

String of pod to describe the handler.

This coderef, if called with parameters $class, $attr, and $method, will generate a code example to insert into the pod.

Coderef providing a slightly annoying API. To be described later.

Defaults to true. Rarely useful to override.

Indicate whether this handler has a defined min or max args.

The required arguments are "method_name" and "code_generator". Installs the delegated method into the target class (taken from the code generator).

Same required arguments as "install_method", but returns the Perl code for the method as a string.

Pseudo-constructor.

Creates a new Sub::HandlesVia::Handler object like this one, but with the given arguments curried.

Pseudo-constructor.

Creates a new Sub::HandlesVia::Handler object like this one, but with looser argument validation.

Pseudo-constructor.

Creates a new Sub::HandlesVia::Handler object like this one, but chainable.

The template is a string of Perl code, except if the following special things are found in it, they are substituted.

$SELF
The invocant.
$SLOT
Direct hashref access for the attribute.
$GET
The current value of the attribute.
@ARG
Any additional arguments passed to the delegated method.

$ARG[$n] will also work.

"#ARG"
The number of additional arguments passed to the delegated method.
$ARG
The first element in @ARG.
$DEFAULT
The attribute's default value, if known.
"« EXPR »"
An expression in double angled quotes sets the attribute's value to the expression.

For example, a handler to halve the value of a numeric attribute might be:

'Sub::HandlesVia::Handler'->new(
  name => 'MyNumber:halve',
  args => 0,
  template => '« $GET / 2 »',
  lvalue_template => '$GET /= 2',
);

Sub::HandlesVia::Handler::Traditional and Sub::HandlesVia::Handler::CodeRef are provided. See source code for this module for more info.

Please report any bugs to https://github.com/tobyink/p5-sub-handlesvia/issues.

Sub::HandlesVia.

Toby Inkster <tobyink@cpan.org>.

This software is copyright (c) 2020, 2022 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.

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.

2024-09-02 perl v5.40.0