PPIx::...ariable(3pm)
π PPIx::EditorTools::IntroduceTemporaryVariable π
okay bestie, sit down, hydrate, because we're about to talk about a lil Perl module from the User Contributed Perl Documentation collection. It's called PPIx::EditorTools::IntroduceTemporaryVariable, and yes that name is a mouthful, and yes it absolutely slays at exactly one job. π
β¨ NAME β¨
PPIx::EditorTools::IntroduceTemporaryVariable β Introduces a temporary variable using PPI
Like. That's it. That's the whole mission statement. It finds a chunk of code you picked and goes "mm, this would be cuter as a variable." π
πΈ VERSION πΈ
version 0.21
π¦ SYNOPSIS π¦
Here's the vibe, verbatim, no notes:
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;
See how it hands you the new code and the location of the fresh little declaration? Main character energy fr fr. π
π DESCRIPTION π
So here's the tea, bestie. You give this thing a region of code inside a statement, and it replaces all the occurrences of that code with a temporary variable. Not some of them. All of them. Iconic behavior.
Then β and this is the part I'm obsessed with β it declares and initializes that temporary variable right above the statement that included the selected expression. So it doesn't just yeet a variable into the void; it puts the declaration exactly where it needs to live. π«Ά
π METHODS π
new()
Constructor. Generally shouldn't be called with any arguments. Like, it can hear you, it just doesn't want to. π
find( ppi => PPI::Document, start_location => Int, end_location => Int, varname => Str )
find( code => Str, start_location => Int, end_location => Int, varname => Str )
Two signatures, one queen. β¨ It accepts either a PPI::Document to process, or a string containing the code (which will be converted into a PPI::Document) to process.
Given the region of code specified by start_location and end_location, it replaces that code with a temporary variable with the name given in varname (which defaults to "tmp", no cap). And again, because it's polite: it declares and initializes the temporary variable right above the statement that included the selected expression.
What does it hand back? A PPIx::EditorTools::ReturnObject, bestie, with:
- the modified code as a string, available via the
codeaccessor - or as a
PPI::Documentvia theppiaccessor - and the
PPI::Tokenwhere the new variable is declared, available via theelementaccessor
β οΈ It croaks with a"no token"exception if no token is found at the location. It also croaks with a"no statement"exception if it's unable to find the statement. So don't come at it with nonsense coordinates, it WILL scream. π
π SEE ALSO π
This class inherits from PPIx::EditorTools. Also see App::EditorTools, Padre, and PPI. The whole friend group, basically. π¦
π AUTHORS π
Look at this lineup. Legends only:
- Steffen Mueller
smueller@cpan.org - Mark Grimes
mgrimes@cpan.org - Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
- Gabor Szabo <gabor@szabgab.com>
- Yanick Champoux <yanick@cpan.org>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Periodt. π
and that's the whole page, bestie!! no crumbs left. this was PPIx::...ariable(3pm) from the User Contributed Perl Documentation, built on perl v5.34.0, dated 2022-06-17. go introduce some temporary variables, you got this πβ¨π
π¦ where this page comes from
- debian bookworm: libppix-editortools-perl 0.21-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libppix-editortools-perl 0.21-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libppix-editortools-perl 0.21-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libppix-editortools-perl 0.21-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms