1Eval::TypeTiny::CodeAccUusmeurlaCtoonrt(r3i)buted Perl DEovcaulm:e:nTtyapteiToinny::CodeAccumulator(3)
2
3
4
6 Eval::TypeTiny::CodeAccumulator - alternative API for Eval::TypeTiny
7
9 my $make_adder = 'Eval::TypeTiny::CodeAccumulator'->new(
10 description => 'adder',
11 );
12
13 my $n = 40;
14 my $varname = $make_adder->add_variable( '$addend' => \$n );
15
16 $make_adder->add_line( 'sub {' );
17 $make_adder->increase_indent;
18 $make_adder->add_line( 'my $other_addend = shift;' );
19 $make_adder->add_gap;
20 $make_adder->add_line( 'return ' . $varname . ' + $other_addend;' );
21 $make_adder->decrease_indent;
22 $make_adder->add_line( '}' );
23
24 my $adder = $make_adder->compile;
25
26 say $adder->( 2 ); ## ==> 42
27
29 This module is covered by the Type-Tiny stability policy.
30
32 Constructor
33 "new( %attrs )"
34 The only currently supported attribute is "description".
35
36 Methods
37 "env()"
38 Returns the current compilation environment, a hashref of variables
39 to close over.
40
41 "code()"
42 Returns the source code so far.
43
44 "description()"
45 Returns the same description given to the constructor, if any.
46
47 "add_line( @lines_of_code )"
48 Adds the next line of code.
49
50 "add_gap()"
51 Adds a blank line of code.
52
53 "increase_indent()"
54 Increases the indentation level for subsequent lines of code.
55
56 "decrease_indent()"
57 Decreases the indentation level for subsequent lines of code.
58
59 "add_variable( $varname, $reference_to_value )"
60 Adds a variable to the compilation environment so that the coderef
61 being generated can close over it.
62
63 If a variable already exists in the environment with that name,
64 will instead add a variable with a different name and return that
65 name. You should always continue to refer to the variable with that
66 returned name, just in case.
67
68 "add_placeholder( $placeholder_name )"
69 Adds a line of code which is just a comment, but remembers its line
70 number.
71
72 "fill_placeholder( $placeholder_name, @lines_of_code )"
73 Goes back to a previously inserted placeholder and replaces it with
74 code.
75
76 As an alternative, "add_placeholder" returns a coderef, which you
77 can call like "$callback->( @lines_of_code )".
78
79 "compile( %opts )"
80 Compiles the code and returns it as a coderef.
81
82 Options are passed on to "eval_closure" from Eval::TypeTiny, but
83 cannot include "code" or "environment". "alias => 1" is probably
84 the option most likely to be useful, but in general you won't need
85 to provide any options.
86
87 "finalize()"
88 This method is called by "compile" just before compiling the code.
89 All it does is remove unfilled placeholder comments. It is not
90 intended for end users to call, but is documented as it may be a
91 useful hook if you are subclassing this class.
92
94 Please report any bugs to
95 <https://github.com/tobyink/p5-type-tiny/issues>.
96
98 Eval::TypeTiny.
99
101 Toby Inkster <tobyink@cpan.org>.
102
104 This software is copyright (c) 2022-2023 by Toby Inkster.
105
106 This is free software; you can redistribute it and/or modify it under
107 the same terms as the Perl 5 programming language system itself.
108
110 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
111 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
112 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
113
114
115
116perl v5.36.0 2023-01-04Eval::TypeTiny::CodeAccumulator(3)