PPIx::...ariable(3pm)

πŸ“¦
version 0.21 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute

πŸ’– 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 code accessor
  • or as a PPI::Document via the ppi accessor
  • and the PPI::Token where the new variable is declared, available via the element accessor
⚠️ 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>

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