Use Perl::Tidy module in your application
Perl::Tidy is usually used as a stand-alone program to beautify your Perl code. Sometimes, for example when you'd like to build a GUI for it, you need to be able to use it as part of your application.
This is a simple example showing how to do it.
Just some random Perl code
examples/tidy/code.pl
use strict; use warnings; my $x = "a"; my $y="b"; if($x){ print $y } if($y) { print "Hello World"; } if($y) { print "Hello World"; }
Using the Perl::Tidy module
examples/tidy/tidy.pl
use strict; use warnings; use 5.010; use Perl::Tidy; use Path::Tiny qw(path); my $filename = 'code.pl'; my $code = path($filename)->slurp_utf8; my %config = ( '--indent-columns' => 4, '--maximum-line-length' => 80, '--variable-maximum-line-length' => undef, '--whitespace-cycle' => 0, ); my $clean; my $stderr; my $rc = ''; for my $field (sort keys %config) { if (defined $config{$field}) { $rc .= "$field=$config{$field}\n"; } else { $rc .= "$field\n"; } } say $rc; my $error = Perl::Tidy::perltidy( source => \$code, destination => \$clean, stderr => \$stderr, perltidyrc => \$rc, ); say $code; say '--------'; if ($error) { say 'ERROR'; say $stderr; } else { say $clean; }
Published on 2020-11-27
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post