SYNOPSIS

    my $munged = PPIx::EditorTools::IntroduceTemporaryVariable->new->introduce(
        code           => "use strict; BEGIN {
        $^W = 1;
}\n\tmy $x = ( 1 + 10 / 12 ) * 2;\n\tmy $y = ( 3 + 10 / 12 ) * 2;\n",
        start_location => [ 2, 19 ],
        end_location   => [ 2, 25 ],
        varname        => '$foo',
    );
    my $modified_code_as_string = $munged->code;
    my $location_of_new_var_declaration = $munged->element->location;

DESCRIPTION

Given a region of code within a statement, replaces all occurrences of that code with a temporary variable. Declares and initializes the temporary variable right above the statement that included the selected expression.

METHODS

new()

Constructor. Generally shouldn't be called with any arguments.

find( ppi => PPI::Document, start_location => Int, end_location => Int, varname => Str ) =item find( code => Str, start_location => Int, end_location => Int, varname => Str )

Accepts either a \*(C`PPI::Document\*(C' to process or a string containing the code (which will be converted into a \*(C`PPI::Document\*(C') to process. Given the region of code specified by start_location and end_location, replaces that code with a temporary variable with the name given in varname (defaults to \*(C`tmp\*(C'). Declares and initializes the temporary variable right above the statement that included the selected expression. Returns a \*(C`PPIx::EditorTools::ReturnObject\*(C' with the modified code as a string available via the \*(C`code\*(C' accessor (or as a \*(C`PPI::Document\*(C' via the \*(C`ppi\*(C' accessor), and the \*(C`PPI::Token\*(C' where the new variable is declared available via the \*(C`element\*(C' accessor. Croaks with a \*(L"no token\*(R" exception if no token is found at the location. Croaks with a \*(L"no statement\*(R" exception if unable to find the statement.

RELATED TO PPIx::EditorTools::IntroduceTemporaryVariable…

This class inherits from \*(C`PPIx::EditorTools\*(C'. Also see App::EditorTools, Padre, and \s-1PPI\s0.