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.
new( %attributes )
Standard Moose-like constructor.
lookup( $method, $trait )
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.
$trait
Nothing is exported by default.
handler %attributes
Shortcut for the new constructor.
new
use Sub::HandlesVia::Handler 'handler'; my $h = handler( %attr ); # is the same as my $h = Sub::HandlesVia::Handler->new( %attr );
name
The name of the function being delegated to.
is_mutator
Indicates whether this handler might mutate an attribute value. The default is to try to detect it based on analysis of the templates.
template
Specially formatted string (see section below) containing the Perl code to implement the method.
lvalue_template
If defined, a shortcut for implementing it when the attribute slot value can be used as an lvalue.
args
The number of arguments which the method being generated expects (does not include the attibute value itself).
min_args
max_args
For methods which take a variable number of arguments. If omitted, default to args.
signature
A signature for said arguments.
usage
A signature to show in documentation, like '$index, $value'. If not provided, will be generated magically from args, min_args, and max_args.
'$index, $value'
curried
An arrayref of curried arguments.
is_chainable
Whether to force the generated method to be chainable.
no_validation_needed
Whether to do less validation of input data.
default_for_reset
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 "[]".
"[]"
prefer_shift_self
Indicates this handler would prefer the code generator to shift $self off @_.
$self
@_
documentation
String of pod to describe the handler.
_examples
This coderef, if called with parameters $class, $attr, and $method, will generate a code example to insert into the pod.
$class
$attr
$method
additional_validation
Coderef providing a slightly annoying API. To be described later.
allow_getter_shortcuts
Defaults to true. Rarely useful to override.
has_min_args()
has_max_args()
Indicate whether this handler has a defined min or max args.
install_method( %args )
The required arguments are method_name and code_generator. Installs the delegated method into the target class (taken from the code generator).
method_name
code_generator
code_as_string( %args )
Same required arguments as install_method, but returns the Perl code for the method as a string.
install_method
curry( @args )
Pseudo-constructor.
Creates a new Sub::HandlesVia::Handler object like this one, but with the given arguments curried.
loose
Creates a new Sub::HandlesVia::Handler object like this one, but with looser argument validation.
chainable
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[$n]
#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.
To install Sub::HandlesVia, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sub::HandlesVia
CPAN shell
perl -MCPAN -e shell install Sub::HandlesVia
For more information on module installation, please visit the detailed CPAN module installation guide.