Hey monks,
I am trying to write a simple script to clean the indents in my code. However, it does not seem to work (I believe there is something wrong with my sprintf statement). Basically, the code will indent each block 4 spaces. The code is below:
#!/usr/bin/perl
use strict;
use warnings;
my $spaces=0;
while (chomp(my $line=<>)) {
$line =~ s/^\s+|\s+$//g;
if ($line =~ /}/) {
$spaces-=4;
}
my $padded = sprintf("%${spaces}s",$line);
print "$spaces:$padded\n";
if ($line =~ /{/) {
$spaces+=4;
}
}
Here's the input:
#!/usr/bin/perl
my $blah=1;
my $blah2=3;
if ($blah1 =~ blah2) {
then do this;
and if not {
then do this as well;
and then this {
blah;
}
return it;
}
talk
}
And here's the output:
0:#!/usr/bin/perl
0:
0:my $blah=1;
0:my $blah2=3;
0:
0:if ($blah1 =~ blah2) {
4:then do this;
4:and if not {
8:then do this as well;
8:and then this {
12: blah;
8: }
8:return it;
4: }
4:talk
0:}