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

Hi

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.

Proof

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

Challenge
Possible technologies
Extra Points

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/my_tidy.pl line 29, <DATA> lin +e 59. 10 took: 0.643707036972046 at c:/tmp/pm/my_tidy.pl line 29, <DATA> lin +e 59. 100 took: 6.37499022483826 at c:/tmp/pm/my_tidy.pl 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 (Parson) 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.