.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Eval::TypeTiny::CodeAccumulator 3" .TH Eval::TypeTiny::CodeAccumulator 3 2024-09-01 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Eval::TypeTiny::CodeAccumulator \- alternative API for Eval::TypeTiny .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 3 \& my $make_adder = \*(AqEval::TypeTiny::CodeAccumulator\*(Aq\->new( \& description => \*(Aqadder\*(Aq, \& ); \& \& my $n = 40; \& my $varname = $make_adder\->add_variable( \*(Aq$addend\*(Aq => \e$n ); \& \& $make_adder\->add_line( \*(Aqsub {\*(Aq ); \& $make_adder\->increase_indent; \& $make_adder\->add_line( \*(Aqmy $other_addend = shift;\*(Aq ); \& $make_adder\->add_gap; \& $make_adder\->add_line( \*(Aqreturn \*(Aq . $varname . \*(Aq + $other_addend;\*(Aq ); \& $make_adder\->decrease_indent; \& $make_adder\->add_line( \*(Aq}\*(Aq ); \& \& my $adder = $make_adder\->compile; \& \& say $adder\->( 2 ); ## ==> 42 .Ve .SH STATUS .IX Header "STATUS" This module is covered by the Type-Tiny stability policy. .SH DESCRIPTION .IX Header "DESCRIPTION" .SS Constructor .IX Subsection "Constructor" .ie n .IP "new( %attrs )" 4 .el .IP "\f(CWnew( %attrs )\fR" 4 .IX Item "new( %attrs )" The only currently supported attribute is \f(CW\*(C`description\*(C'\fR. .SS Methods .IX Subsection "Methods" .ie n .IP env() 4 .el .IP \f(CWenv()\fR 4 .IX Item "env()" Returns the current compilation environment, a hashref of variables to close over. .ie n .IP code() 4 .el .IP \f(CWcode()\fR 4 .IX Item "code()" Returns the source code so far. .ie n .IP description() 4 .el .IP \f(CWdescription()\fR 4 .IX Item "description()" Returns the same description given to the constructor, if any. .ie n .IP "add_line( @lines_of_code )" 4 .el .IP "\f(CWadd_line( @lines_of_code )\fR" 4 .IX Item "add_line( @lines_of_code )" Adds the next line of code. .ie n .IP add_gap() 4 .el .IP \f(CWadd_gap()\fR 4 .IX Item "add_gap()" Adds a blank line of code. .ie n .IP increase_indent() 4 .el .IP \f(CWincrease_indent()\fR 4 .IX Item "increase_indent()" Increases the indentation level for subsequent lines of code. .ie n .IP decrease_indent() 4 .el .IP \f(CWdecrease_indent()\fR 4 .IX Item "decrease_indent()" Decreases the indentation level for subsequent lines of code. .ie n .IP """add_variable( $varname, $reference_to_value )""" 4 .el .IP "\f(CWadd_variable( $varname, $reference_to_value )\fR" 4 .IX Item "add_variable( $varname, $reference_to_value )" Adds a variable to the compilation environment so that the coderef being generated can close over it. .Sp If a variable already exists in the environment with that name, will instead add a variable with a different name and return that name. You should always continue to refer to the variable with that returned name, just in case. .ie n .IP "add_placeholder( $placeholder_name )" 4 .el .IP "\f(CWadd_placeholder( $placeholder_name )\fR" 4 .IX Item "add_placeholder( $placeholder_name )" Adds a line of code which is just a comment, but remembers its line number. .ie n .IP """fill_placeholder( $placeholder_name, @lines_of_code )""" 4 .el .IP "\f(CWfill_placeholder( $placeholder_name, @lines_of_code )\fR" 4 .IX Item "fill_placeholder( $placeholder_name, @lines_of_code )" Goes back to a previously inserted placeholder and replaces it with code. .Sp As an alternative, \f(CW\*(C`add_placeholder\*(C'\fR returns a coderef, which you can call like \f(CW$callback\->( @lines_of_code )\fR. .ie n .IP "compile( %opts )" 4 .el .IP "\f(CWcompile( %opts )\fR" 4 .IX Item "compile( %opts )" Compiles the code and returns it as a coderef. .Sp Options are passed on to \f(CW\*(C`eval_closure\*(C'\fR from Eval::TypeTiny, but cannot include \f(CW\*(C`code\*(C'\fR or \f(CW\*(C`environment\*(C'\fR. \f(CW\*(C`alias => 1\*(C'\fR is probably the option most likely to be useful, but in general you won't need to provide any options. .ie n .IP finalize() 4 .el .IP \f(CWfinalize()\fR 4 .IX Item "finalize()" This method is called by \f(CW\*(C`compile\*(C'\fR just before compiling the code. All it does is remove unfilled placeholder comments. It is not intended for end users to call, but is documented as it may be a useful hook if you are subclassing this class. .SH BUGS .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" Eval::TypeTiny. .SH AUTHOR .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2022\-2023 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "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.