Thread: Perl Tidy Script
dirk

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

perl

 tidy(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;

perl

 tidy(
    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;

perkiset

Nice - funny, I wrote a "tidy" script for Clipper (Summer '87) that I called... wait for it... "

Ajax

  - Code Cleaner" a long time ago. ALWAYS nice to have these about...

/p

thedarkness

Shame you didn't copyright the name.......  Applause

perkiset

Seems to me it was already taken back then as well  Applause

Dirk - looking back over the code and now beeing .00001% smarter in

PERL

  because of you, I am not clear how this accomplishes anything... is TDYSCRIPT a

mac

 ro or something?

dirk

Ed, would not call it a

mac

 ro.

It's simply a script written to use the standard

Perl

  module

Perl

 ::Tidy.

And it handles the input and output.

You can execute the module directly on the command line, like:
>

perl

 tidy -D filename

But using a script, I think, is a little bit more comfortable. I run such scripts
directly in my IDE, so I don't have to switch to the command line.

perkiset

Thanks Dirk -

Yikes that syntax throws me off.

nutballs

interesting. i have never once used a code formatter. my code is usually pretty well formatted, but other people's code drives me nuts. random indents, no indents, no line breaks between "concepts" etc. I might write one for

ASP

 . possibly a good exercise anyway.

perkiset

Abalootly nuts - even if you write it in another language to format a new one you're working with... gives you a feel for the rhythm of a language really quickly.


Perkiset's Place Home   Politics @ Perkiset's