Posts Tagged ‘code’

Code formating script

No Comments »
#!/usr/bin/perl -w
 
my $ifile = shift;
my $blank = 0;
my $indent = 2;
open(IFILE, "$ifile") or die "Cannot open $ifile\n";
while(my $line = ) {
  if($line =~ /^\s*else/i
     or $line =~ /^\s*end/i
    ) {  #reduce indent before xxx
    $blank = $blank - 2;
  }
  if($blank > 0) {
    print " " x $blank;  #print several blanks
    print "$line";
  } else {
    print "$line";
    $blank = 0;
  }
  if($line =~ /^\s*if/i
     or $line =~ /^\s*else/i
     or $line =~ /^\s*loop/i
    ) {  #increase indent after xxx
    $blank = $blank + 2;
  }
}