The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 02:47:41 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Perl Tidy Script  (Read 5522 times)
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« on: May 13, 2007, 11:30:29 AM »

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

Code:
#!/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;
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: May 13, 2007, 08:21:19 PM »

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
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #2 on: May 14, 2007, 04:40:31 AM »

Shame you didn't copyright the name.......  ROFLMAO
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: May 14, 2007, 09:02:32 AM »

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

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 macro or something?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #4 on: May 14, 2007, 09:19:43 AM »

Ed, would not call it a macro.

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:
>perltidy -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.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: May 14, 2007, 09:23:38 AM »

Thanks Dirk -

Yikes that syntax throws me off.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #6 on: May 14, 2007, 11:16:13 AM »

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.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: May 14, 2007, 11:27:11 AM »

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.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!