1Sub::HandlesVia::CodeGeUnseerratCoorn(t3r)ibuted Perl DoScuubm:e:nHtaantdiloensVia::CodeGenerator(3)
2
3
4
6 Sub::HandlesVia::CodeGenerator - looks at a Handler and generates a
7 string of Perl code for it
8
10 This module is part of Sub::HandlesVia's internal API. It is mostly of
11 interest to people extending Sub::HandlesVia.
12
13 Sub::HandlesVia toolkits create a code generator for each attribute
14 they're dealing with, and use the code generator to generate Perl code
15 for one or more delegated methods.
16
18 new( %attributes )
19 Standard Moose-like constructor.
20
22 "toolkit" Object
23 The toolkit which made this code generator.
24
25 "target" ClassName|RoleName
26 The target package for generated methods.
27
28 "sandboxing_package" ClassName|RoleName|Undef
29 Package name to use as a sandbox; the default is usually fine.
30
31 "attribute" Str|ArrayRef
32 The attribute delegated to.
33
34 "attribute_spec" HashRef
35 Informational only.
36
37 "is_method" Bool
38 Indicates whether the generated code should be methods rather than
39 functions. This defaults to true, and false isn't really tested or
40 well-defined.
41
42 "env" HashRef
43 Variables which need to be closed over when compiling coderefs.
44
45 "isa" Maybe[TypeTiny]
46 The type constraint for the attribute.
47
48 "coerce" Bool
49 Should the attribute coerce?
50
51 "method_installer" CodeRef
52 A coderef which can be called with $method_name and $coderef, will
53 install the method. Note that it isn't passed the package to install
54 into (which can be found in "target"), so that would need to be closed
55 over.
56
57 "generator_for_self" CodeRef
58 A coderef which if called, generates a string like '$_[0]'.
59
60 Has a sensible default.
61
62 All the "generator_for_XXX" methods are called as methods, so have the
63 code generator object as an invocant.
64
65 "generator_for_slot" CodeRef
66 A coderef which if called, generates a string like '$_[0]{attrname}'.
67
68 "generator_for_get" CodeRef
69 A coderef which if called, generates a string like '$_[0]->attrname'.
70
71 "generator_for_set" CodeRef
72 A coderef which if called with a parameter, generates a string like
73 "\$_[0]->_set_attrname( $parameter )".
74
75 "generator_for_simple_default" CodeRef
76 A coderef which if called with a parameter, generates a string like
77 'undef' or 'q[]' or '{}'.
78
79 The parameter is a handler object, which offers a "default_for_reset"
80 attribute which might be able to provide a useful fallback.
81
82 "generator_for_args" CodeRef
83 A coderef which if called, generates a string like '@_[1..$#_]'.
84
85 Has a sensible default.
86
87 "generator_for_argc" CodeRef
88 A coderef which if called, generates a string like '$#_'.
89
90 Has a sensible default.
91
92 "generator_for_argc" CodeRef
93 A coderef which if called with a parameter, generates a string like
94 "\$_[$parameter + 1]".
95
96 Has a sensible default.
97
98 "generator_for_currying" CodeRef
99 A coderef which if called with a parameter, generates a string like
100 "splice(\@_,1,0,$parameter);".
101
102 Has a sensible default.
103
104 "generator_for_usage_string" CodeRef
105 The default is this coderef:
106
107 sub {
108 @_==3 or die;
109 shift;
110 my $method_name = shift;
111 my $guts = shift;
112 return "\$instance->$method_name($guts)";
113 }
114
115 "generator_for_type_assertion" CodeRef
116 Called as a method and passed a hashref compilation environment, a type
117 constraint, and a variable name. Generates code to assert that the
118 variable value meets the type constraint, with coercion if appropriate.
119
120 "generator_for_error" CodeRef
121 Called as a method and passed a Perl string which is an expression
122 evaluating to an error message. Generates code to throw the error.
123
124 "generator_for_prelude" CodeRef
125 By default is a coderef returning the empty string. Can be used to
126 generate some additional statements which will be inserted near the top
127 of the method being generated. (Typically after parameter checks but
128 before doing anything serious.) This can be used to unlock a read-only
129 attribute, for example.
130
131 "get_is_lvalue" Bool
132 Indicates wheter the code generated by "generator_for_get" will be
133 suitable for used as an lvalue.
134
135 "set_checks_isa" Bool
136 Indicates wheter the code generated by "generator_for_set" will do type
137 checks.
138
139 "set_strictly" Bool
140 Indicates wheter we want to ensure that the setter is always called,
141 and we should not try to bypass it, even if we have an lvalue getter.
142
144 For each "generator_for_XXX" attribute, there's a corresponding
145 "generate_XXX" method to actually call the coderef, possibly including
146 additional processing.
147
148 "generate_and_install_method( $method_name, $handler )"
149 Given a handler and a method name, will generate a coderef for the
150 handler and install it into the target package.
151
152 "generate_coderef_for_handler( $method_name, $handler )"
153 As above, but just returns the coderef rather than installs it.
154
155 "install_method( $method_name, $coderef )"
156 Installs a coderef into the target package with the given name.
157
159 Please report any bugs to
160 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
161
163 Sub::HandlesVia.
164
166 Toby Inkster <tobyink@cpan.org>.
167
169 This software is copyright (c) 2020, 2022 by Toby Inkster.
170
171 This is free software; you can redistribute it and/or modify it under
172 the same terms as the Perl 5 programming language system itself.
173
175 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
176 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
177 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
178
179
180
181perl v5.36.0 2023-04-06 Sub::HandlesVia::CodeGenerator(3)