#!/usr/bin/perl use warnings; use strict; my ($lines, $indents); while (defined ($_ = )) { chomp; push @$lines, $_; my ($tab_chars) = m/ \A (\t*) /x; push @$indents, length($tab_chars); } my $i = 0; my $n = @$lines; for ($i = 0; $i < $n; ++$i) { my $line = $lines->[$i]; my $indent = $indents->[$i]; my $line_next = $lines->[$i+1]; my $indent_next = $indents->[$i+1] || 0; if (!defined $line_next) { $line_next = ""; $indent_next = 0; } print $line; if ($indent_next == $indent) { print ";\n"; } elsif ($indent_next > $indent) { print " {\n"; while (++$indent < $indent_next) { print "\t" x $indent, "{\n"; } } elsif ($indent_next < $indent) { while ($indent-- > $indent_next) { print ";\n", "\t" x $indent, "}"; } unless ($line_next =~ m/ \A \t* \s* (else|elsif) \b /x) { print ";"; } print "\n"; } }