Sub::Ha...Handler(3)
π NAME π
ok so Sub::HandlesVia::Handler β it's the template for a method that can be delegated to. bestie, it's giving blueprint energy.
β¨ DESCRIPTION β¨
okay so real talk: this module is part of Sub::HandlesVia's internal API. it's mostly of interest to people extending Sub::HandlesVia, so if you're just vibing with the normal public stuff, this is very much iykyk territory π«Ά.
here's the tea: this module works in conjunction with Sub::HandlesVia::CodeGenerator and subclasses of Sub::HandlesVia::Toolkit to build a string of Perl code β and that string can then be compiled into a method to install into your class. like, it literally writes the method for you. main character energy fr π
.
π CONSTRUCTORS π
new( %attributes )
Standard Moose-like constructor. no notes, iconic, we love a classic β¨.
"lookup( $method, $trait )"
Looks up a method from existing handler libraries. it's giving shopping for a method you didn't write yourself ποΈ.
my $h = Sub::HandlesVia::Handler->lookup( 'get', 'Array' );
Curried version (yes she's flexible):
my $h = Sub::HandlesVia::Handler->lookup( [ 'get', 0 ], 'Array' );
and heads up: the $trait may be an arrayref of possible traits. options, options, we love options π.
πΈ EXPORTS πΈ
Nothing is exported by default. sorry bestie, no freebies, you gotta ask π.
"handler %attributes"
Shortcut for the new constructor. same thing, shorter vibes:
use Sub::HandlesVia::Handler 'handler';
my $h = handler( %attr );
# is the same as
my $h = Sub::HandlesVia::Handler->new( %attr );
literally identical, periodt.
π¦ ATTRIBUTES π¦
"name" Str
The name of the function being delegated to. that's her name, that's her whole identity π .
"is_mutator" Bool
Indicates whether this handler might mutate an attribute value. the default is to try to detect it based on analysis of the templates β so she sniffs it out herself unless you tell her otherwise π.
"template" Str
Specially formatted string (peep the section below π) containing the Perl code to implement the method. this is where the magic actually lives.
"lvalue_template" Maybe[Str]
If defined, a shortcut for implementing it when the attribute slot value can be used as an lvalue. fancy β¨.
"args" Maybe[PositiveOrZeroInt]
The number of arguments which the method being generated expects. note: this does not include the attibute value itself.
"min_args" and "max_args" Maybe[PositiveOrZeroInt]
For methods which take a variable number of arguments. if omitted, they default to args. so if you skip them, they just copy args's whole look, iconic π.
"signature" Maybe[ArrayRef[TypeTiny]]
A signature for said arguments.
"usage" Str
A signature to show in documentation, like '$index, $value'. if not provided, it will be generated magically from args, min_args, and max_args. β¨ magic β¨ (the good kind).
"curried" Maybe[ArrayRef[Item]]
An arrayref of curried arguments.
"is_chainable" Bool
Whether to force the generated method to be chainable.
"no_validation_needed" Bool
Whether to do less validation of input data. living dangerously, love that for you π«£.
"default_for_reset" Maybe[Str]
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 "[]". so like, a little emergency backup default, no cap.
"prefer_shift_self" Bool
Indicates this handler would prefer the code generator to shift $self off @_. tiny preference, big impact.
"documentation" Maybe[Str]
String of pod to describe the handler. give her some lore π.
"_examples" Maybe[CodeRef]
This coderef, if called with parameters $class, $attr, and $method, will generate a code example to insert into the pod. auto-generated examples, we love a self-documenting queen π.
"additional_validation" Maybe[CodeRef]
Coderef providing a slightly annoying API. To be described later. (not me calling my own API annoying π iconic honesty tho.)
"allow_getter_shortcuts" Bool
Defaults to true. Rarely useful to override. like, leave it alone bestie, she's fine as she is πΈ.
π METHODS π
has_min_args() and has_max_args()
Indicate whether this handler has a defined min or max args. simple yes/no energy.
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). it's giving move-in day π¦.
code_as_string( %args )
Same required arguments as "install_method", but returns the Perl code for the method as a string. so you can look at it, save it, frame it, whatever πΌοΈ.
curry( @args )
Pseudo-constructor.
Creates a new Sub::HandlesVia::Handler object like this one, but with the given arguments curried. same girl, new seasoning π.
"loose"
Pseudo-constructor.
Creates a new Sub::HandlesVia::Handler object like this one, but with looser argument validation. she's off duty, she's in sweatpants π.
"chainable"
Pseudo-constructor.
Creates a new Sub::HandlesVia::Handler object like this one, but chainable. and then and then and then β π
.
π TEMPLATE FORMAT π
The template is a string of Perl code β except if the following special things are found in it, they are substituted. so it's Perl code but with little sparkle placeholders β¨:
$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',
);
see? she just does the math. ate and left no crumbs π₯.
π§ SUBCLASSES π§
Sub::HandlesVia::Handler::Traditional and Sub::HandlesVia::Handler::CodeRef are provided. See source code for this module for more info. (go read the source, bestie, it's right there π.)
π BUGS π
Please report any bugs to <https://github.com/tobyink/p5-sub-handlesvia/issues>. don't suffer in silence, tell Toby π.
β SEE ALSO β
Sub::HandlesVia.
π AUTHOR π
Toby Inkster <tobyink@cpan.org>.
π COPYRIGHT AND LICENCE π
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. free as in slay π.
π 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.
(translation: no promises bestie, she's doing her best π)
okay that's the whole page!! this was Sub::HandlesVia::Handler(3), part of the User Contributed Perl Documentation, and it shipped with perl v5.42.0 dated 2026-02-11. thanks for reading my little yap session, go build something cute with it π«Άπβ¨
π¦ where this page comes from
- fedora 44: perl-Sub-HandlesVia 0.053005-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms