logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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

Author

       Toby Inkster <tobyink@cpan.org>.

Bugs

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

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.

       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.

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.

perl v5.40.1                                       2025-05-06               Eval::TypeTiny::CodeAccumulator(3pm)

Name

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

See Also

       Eval::TypeTiny.

Status

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

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

See Also