#!/usr/local/bin/perl # 'pb' Perl Beautifier # Written by P. Lutus Ashland, Oregon lutusp@arachnoid.com 5/20/96 # This script processes Perl scripts, cleans up and indents, like cb does for C # Be careful with this script - it accepts wildcards and processes every text file # that meets the wildcard criteria. This could be a catastrophe in the hands of the unwary. $tabstring = " "; # You may place any tab-stop characters you want here if($ARGV[0] eq "") { print "usage: file1 file2 etc. or wildcards (replaces originals in place)\n"; } else { foreach $filename (@ARGV) { if(-T $filename) { &process($filename); } } } sub process { $fn = $_[0]; undef $/; # so we can grab the entire file at once undef @infa; # prevent left-overs print STDERR "$fn"; open (INFILE,$fn); @infa = split(/\n/,); close INFILE; $/ = "\n"; # restore default open (OUTFILE,">$fn"); $tabtotal = 0; for (@infa) { s/^\s*(.*?)\s*$/$1/; # strip leading and trailing spaces $a = $_; # copy original string $q = $a; # i plan to modify this copy for testing $q =~ s/\\\#//g; # remove escaped comment tokens $q =~ s/\#.*?$//g; # remove Perl-style comments $q =~ s{/\*.*?\*/} []gsx; # remove C-style comments $q =~ s/\\\{//g; # remove escaped left braces $q =~ s/\\\}//g; # remove escaped right braces $q =~ s/\\\(//g; # remove escaped left parentheses $q =~ s/\\\)//g; # remove escaped right parentheses $q =~ s/\'.*?\'//g; # remove single-quoted lines # now the remaining braces/parentheses should be structural $delta = -($q =~ s/\}/\}/g); # subtract closing braces $delta += ($q =~ s/\{/\{/g); # add opening braces $delta -= ($q =~ s/\)/\)/g); # subtract closing parens $delta += ($q =~ s/\(/\(/g); # add opening parens $tabtotal += ($delta < 0)?$delta:0; # subtract closing braces/parentheses $i = ($tabtotal > 0)?$tabtotal:0; # create tab index $tabtotal += ($delta>0)?$delta:0; # add opening braces/parentheses for next print if(substr($a,0,1) ne "#") { # don't tab comments print OUTFILE $tabstring x $i; # "tab" out to position } print OUTFILE "$a\n"; # print original line } # -- for (@infa) close OUTFILE; if($tabtotal != 0) { print STDERR " Indentation error: $tabtotal\n"; } else { print STDERR "\n"; } } # sub process