Eval::TypeTiny::CodeAccumulator(3pm)

NAME
Eval::TypeTiny::CodeAccumulator - alternative API for Eval::TypeTiny

SYNOPSIS
  my $make_adder = 'Eval::TypeTiny::CodeAccumulator'->new(
    description => 'adder',
  );

  my $n = 40;
  my $varname = $make_adder->add_variable( '$addend' => \$n );

  $make_adder->add_line( 'sub {' );
  $make_adder->increase_indent;
  $make_adder->add_line( 'my $other_addend = shift;' );
  $make_adder->add_gap;
  $make_adder->add_line( 'return ' . $varname . ' + $other_addend;' );
  $make_adder->decrease_indent;
  $make_adder->add_line( '}' );

  my $adder = $make_adder->compile;

  say $adder->( 2 );  ## ==> 42

STATUS
This module is covered by the Type-Tiny stability policy.

DESCRIPTION

  Constructor

new( %attrs )
    The only currently supported attribute is "description".

  Methods

env()
    Returns the current compilation environment, a hashref of variables to close over.

code()
    Returns the source code so far.

description()
    Returns the same description given to the constructor, if any.

add_line( @lines_of_code )
    Adds the next line of code.

"addf( $fmt, @args )"
    Shortcut for "add_line( sprintf $fmt, @args )".

add_gap()
    Adds a blank line of code.

increase_indent()
    Increases the indentation level for subsequent lines of code.

decrease_indent()
    Decreases the indentation level for subsequent lines of code.

"add_variable( $varname, $reference_to_value )"
    Adds a variable to the compilation environment so that the coderef being generated can close over it.

    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.

add_placeholder( $placeholder_name )
    Adds a line of code which is just a comment, but remembers its line number.

"fill_placeholder( $placeholder_name, @lines_of_code )"
    Goes back to a previously inserted placeholder and replaces it with code.

    As an alternative, "add_placeholder" returns a coderef, which you can call like $callback->( @lines_of_code ).

compile( %opts )
    Compiles the code and returns it as a coderef.

    Options are passed on to "eval_closure" from Eval::TypeTiny, but cannot include "code" or "environment". "alias => 1" is probably the option most likely to be useful, but in general you won't need to provide any options.

finalize()
    This method is called by "compile" 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.

BUGS
Please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>.

SEE ALSO
Eval::TypeTiny.

AUTHOR
Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE
This software is copyright (c) 2022-2025 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.

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.