Here is a simple script which takes a Perl script as input, parses and beautifies the source
using the module Perl::Tidy and writes the formated source to a new file.
For example:
input: test.pl
output: test.pl.tdy
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use English;
use Perl::Tidy;
#-------------------------------------------------------------------------------
my $script = 'test.pl';
my $argv = '-ci=4 -nolq';
# -nolq : No Outdenting Long Quotes
# -ci=n, --continuation-indentation=n
# input parameters as described in the perltidy(1) man page
#-------------------------------------------------------------------------------
open my $SCRIPT, '<', $script
or croak "Can't open '$script': $OS_ERROR";
my $source = do { local $/; <$SCRIPT> };
close $SCRIPT;
my @destination = undef;
perltidy(
source => \$source,
destination => \@destination,
argv => $argv,
);
open my $TDYSCRIPT, '>', $script . '.tdy'
or croak "Can't open '$script.tdy': $OS_ERROR";
foreach my $line (@destination) {
print {$TDYSCRIPT} $line;
}
close $TDYSCRIPT;
exit;