Sub::HandlesVia::CodeGenerator - looks at a Handler and generates a string of Perl code for it
This module is part of Sub::HandlesVia's internal API. It is mostly of interest to people extending Sub::HandlesVia.
Sub::HandlesVia toolkits create a code generator for each attribute they're dealing with, and use the code generator to generate Perl code for one or more delegated methods.
new( %attributes )
Standard Moose-like constructor.
toolkit
The toolkit which made this code generator.
target
The target package for generated methods.
sandboxing_package
Package name to use as a sandbox; the default is usually fine.
attribute
The attribute delegated to.
attribute_spec
Informational only.
is_method
Indicates whether the generated code should be methods rather than functions. This defaults to true, and false isn't really tested or well-defined.
env
Variables which need to be closed over when compiling coderefs.
isa
The type constraint for the attribute.
coerce
Should the attribute coerce?
method_installer
A coderef which can be called with $method_name and $coderef, will install the method. Note that it isn't passed the package to install into (which can be found in target), so that would need to be closed over.
$method_name
$coderef
generator_for_self
A coderef which if called, generates a string like '$_[0]'.
'$_[0]'
Has a sensible default.
All the generator_for_XXX methods are called as methods, so have the code generator object as an invocant.
generator_for_XXX
generator_for_slot
A coderef which if called, generates a string like '$_[0]{attrname}'.
'$_[0]{attrname}'
generator_for_get
A coderef which if called, generates a string like '$_[0]->attrname'.
'$_[0]->attrname'
generator_for_set
A coderef which if called with a parameter, generates a string like "\$_[0]->_set_attrname( $parameter )".
"\$_[0]->_set_attrname( $parameter )"
generator_for_simple_default
A coderef which if called with a parameter, generates a string like 'undef' or 'q[]' or '{}'.
'undef'
'q[]'
'{}'
The parameter is a handler object, which offers a default_for_reset attribute which might be able to provide a useful fallback.
default_for_reset
generator_for_args
A coderef which if called, generates a string like '@_[1..$#_]'.
'@_[1..$#_]'
generator_for_argc
A coderef which if called, generates a string like '$#_'.
'$#_'
A coderef which if called with a parameter, generates a string like "\$_[$parameter + 1]".
"\$_[$parameter + 1]"
generator_for_currying
A coderef which if called with a parameter, generates a string like "splice(\@_,1,0,$parameter);".
"splice(\@_,1,0,$parameter);"
generator_for_usage_string
The default is this coderef:
sub { @_==3 or die; shift; my $method_name = shift; my $guts = shift; return "\$instance->$method_name($guts)"; }
generator_for_type_assertion
Called as a method and passed a hashref compilation environment, a type constraint, and a variable name. Generates code to assert that the variable value meets the type constraint, with coercion if appropriate.
generator_for_error
Called as a method and passed a Perl string which is an expression evaluating to an error message. Generates code to throw the error.
generator_for_prelude
By default is a coderef returning the empty string. Can be used to generate some additional statements which will be inserted near the top of the method being generated. (Typically after parameter checks but before doing anything serious.) This can be used to unlock a read-only attribute, for example.
get_is_lvalue
Indicates wheter the code generated by generator_for_get will be suitable for used as an lvalue.
set_checks_isa
Indicates wheter the code generated by generator_for_set will do type checks.
set_strictly
Indicates wheter we want to ensure that the setter is always called, and we should not try to bypass it, even if we have an lvalue getter.
For each generator_for_XXX attribute, there's a corresponding generate_XXX method to actually call the coderef, possibly including additional processing.
generate_XXX
generate_and_install_method( $method_name, $handler )
Given a handler and a method name, will generate a coderef for the handler and install it into the target package.
generate_coderef_for_handler( $method_name, $handler )
As above, but just returns the coderef rather than installs it.
install_method( $method_name, $coderef )
Installs a coderef into the target package with the given name.
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.