The perltidy script allows you to convert your Perl source code to some unified layout. The Perl::Tidy module, behind the scenes allows us to build tools like perltidy.
Let's experiment with it so we can try to build a GUI for it.
Code to be beautified
use strict;
use warnings;
my $x = "a";
my $y="b";
if($x){ print $y }
if($y) {
print "Hello World";
}
if($y)
{
print "Hello World";
}
Current experiment
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;
}
Output
--indent-columns=4
--maximum-line-length=80
--variable-maximum-line-length
--whitespace-cycle=0
use strict;
use warnings;
my $x = "a";
my $y="b";
if($x){ print $y }
if($y) {
print "Hello World";
}
if($y)
{
print "Hello World";
}
--------
use strict;
use warnings;
my $x = "a";
my $y = "b";
if ($x) { print $y }
if ($y) {
print "Hello World";
}
if ($y) {
print "Hello World";
}