package Beautifier; use strict; use warnings; our $VERSION = '0.04'; sub new() { my $self = shift; return bless {}, $self; } sub Beautify($$) { ### First element of @_ is a reference to the element that called this subroutine my $self = shift; ### Second is a reference to a hash with options my $options = shift; ### Third is a string that contains the code to beautify my $fileContent = shift; ### get the options from the hashref my $indent = " "; if ($$options{'Indent'}) { $indent = $$options{'Indent'}; } my $curlyString = "\n{"; if (($$options{'CurlyBraceOnNewLine'}) && ($$options{'CurlyBraceOnNewLine'} =~ m/^(?:no|0)$/i)) { $curlyString = " {"; } my $spaceBeforeParens = 1; if (($$options{'SpaceBeforeParenthesisOpen'}) && ($$options{'SpaceBeforeParenthesisOpen'} =~ m/^(?:no|0)$/i)) { $spaceBeforeParens = 0; } ### Random string used for replacing comments and string values temporarily. my $randomString = 'KJSDKfSDKFJKJsdFKKJDKJlkxcljhdfsjlxv'; ### step 0: save all stuff between "" or '' or `` or q{} or qq{} or qx{} or qw{} or m{} or qr{} or s{}{} or tr{}{} or after # or <Beautify( {'indent' => " "}, "$this = 'some perl code';\n"; ); =head1 DESCRIPTION This module pretty prints/beautifies perl code. It does 3 things: - Indenting - Spacing around () and = and so forth - Curly placing This might come in handy when working on other people's code (don't you hate that?) It uses my coding conventions, like placing the curly on the next line. Feel free to change it to your style (which, if different from mine, is wrong ;) Here is what code will look like after it's been crunched by Beautifier: if (($varName =~ m//) && (-f $fileName)) { print "Hello, world!\n"; } WARNING: A working program might no longer work after Beautifier did her thing on it. (Beautifier is definitely female) =head1 EXAMPLES #!/usr/bin/perl -w use strict; use Beautifier; undef $/; ### To read file all in one swoop open (FH, "test.pl") || die $!; my $fileContent = ; close (FH); $/ = "\n"; ### Back to default my $beautify = new Beautifier; print $beautify->Beautify( { 'indent' => " ", }, $fileContent ); =head1 AUTHOR Teun van Eijsden, Eteun@chello.nlE =head1 SEE ALSO L. =cut