Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Challenge: Perl::Tidy subprocess for faster formatting

by LanX (Sage)
on Jul 03, 2022 at 14:47 UTC ( #11145254=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:


for many years now many IDEs offer to run code-snippets thru perltidy in a launched sub-process.

But this comes with noticeable delay, because most of the time is lost for startup of perltidy, while Perl::Tidy offers a server mode.

A faster tidying would allow formatting on-the-fly on key-triggers, like when typing return or closing a sub.


The following code is formatting itself 1,10 and 100 times thru perltidy, and an average run takes less than 0,07 secs

  • Write an IDE solution which starts a constant Perltidy server-process in the background and sends code-snippets back and forth.
Possible technologies
  • comint-mode in emacs
  • Language server protocol plugin for various IDEs (reference implementation Visual Studio Code)
  • ... whatever your favourit IDE
Extra Points
  • implement a GUI to try out perltidy configs on the fly, be it in Tk or inside an IDE.

demo code
use v5.12; use warnings; use Perl::Tidy; use Time::HiRes qw/time/; seek DATA,0,0; my $code = join "",<DATA>; #say $code; my $show; my $rc = <<'__CFG__'; --indent-columns=4 --maximum-line-length=80 --variable-maximum-line-length --whitespace-cycle=0 __CFG__ time_it(); sub time_it { for my $times (1,10,100) { my $start=time; run_it() for 1..$times; my $end =time; warn "$times took: ", ($end-$start); } } sub run_it { my $clean; my $stderr; my $error = Perl::Tidy::perltidy ( source => \$code, destination => \$clean, stderr => \$stderr, perltidyrc => \$rc, ); return unless $show; say $code; say '--------'; if ($error) { say 'ERROR'; say $stderr; } else { say $clean; } } __DATA__

1 took: 0.0720160007476807 at c:/tmp/pm/ line 29, <DATA> lin +e 59. 10 took: 0.643707036972046 at c:/tmp/pm/ line 29, <DATA> lin +e 59. 100 took: 6.37499022483826 at c:/tmp/pm/ line 29, <DATA> lin +e 59.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: Challenge: Perl::Tidy subprocess for faster formatting
by perlfan (Vicar) on Jul 04, 2022 at 14:04 UTC
    I don't know what the rage about "language servers" are recently. I tidy my code in vim with the filter command, <shift>:%!perltidy [options]. Overhead of it being a subprocess of vim seems negligable; and frankly this is way more convenient than running it outside of my "IDE" (vim + tmux).
        I saw that. The approach lost me at perl -c.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11145254]
Approved by davies
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-08-11 11:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found