This code below will tidy your perl code.
use File::Copy;
opendir (DIR , Win32::GetCwd());
my @DIR = readdir(DIR);
closedir DIR;
my $tabValues = ' ';
my $FileToClean ='myperlfile.pl';
#foreach(@DIR)
{
# my $FileToClean ='myperlfile.pl';
#$FileToClean =$_;
# next if ($FileToClean =~ /\.bak$/gi);
copy ($FileToClean,"$FileToClean.bak");
open(FILE ,"$FileToClean");
my $templine;
my $tab;
while(my $line =)
{
$line =&trim($line);
#print $line."\r\n";
if ($line =~ /.*TOVALIDDATE.*/gi)
{
print "test\n";
}
if ($line ne "")
{
if ($line=~ /.*\(.*\)\{.*\}/gi)
{
$templine .= $tab.$line."\r\n";
next;
}
if ($line=~ /(^(while|if|else|elsif|foreach).*\(.*\).*)(\{.*)/gi)
{
$templine .= $tab.$1."\r\n";
$templine .= $tab.$3."\r\n";
$tab .= $tabValues;
next;
}
if($line=~ /^#/gi)
{
$templine .= $tab.$line."\r\n";
next;
}
if($line=~ /^{/gi || $line=~ /^(while|if|else|elsif|foreach).*{$/gi)
{
$templine .= $tab.$line."\r\n";
$tab .= $tabValues;
next;
}
if($line=~ /^}/gi || $line=~ /^}.*(while|if|else|elsif|foreach).*/gi)
{
$tab =~ s/$tabValues//;
if($line=~ /^{/gi || $line=~ /.*(while|if|else|elsif|foreach).*{/gi)
{
$tab .= $tabValues;
}
$templine .= $tab.$line."\r\n";
next;
}
if($line=~ /}$/gi || $line=~ /.*(while|if|else|elsif|foreach).*}$/gi)
{
$tab =~ s/$tabValues//;
#$line =~ s/}/$tab}/;
$templine .= $tab.$line."\r\n";
next;
}
if($line=~ /^sub .*/gi)
{
$tab="";
$templine .= $tab.$line."\r\n";
my $aaa= trim($line);
if($aaa=~ /^sub .*{$/gi)
{
$tab=$tabValues;
}
next;
}
$templine .= $tab.$line."\r\n";
}
}
close FILE;
open(FILEOUT,">$FileToClean");
print FILEOUT $templine;
close FILEOUT;
}
exit;
sub trim
{
my $str = shift;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
return $str;
}
No comments:
Post a Comment