Adding style to perl code
Perl has always been my favourite command line and web programming language. There is nothing Perl can’t do. The Perl language combined with the large quantities of modules on CPAN (developed by some of the best programmers on the planet) make it the kitchen sink of programming languages.
I like to make my code as easy to read as possible. Nothing irks me more than a jumble of spaghetti code with indents and curly braces everywhere. I admit that there are times when a solution to a difficult problem comes to me and I become more concerned with getting my ideas down than on following style rules. But after I have my thoughts properly coded I want to go back and clean things up a bit. That’s where perltidy comes in.
What is Perl::Tidy?
Perltidy is a Perl script which indents and re-formats Perl scripts to make them easier to read.
The CPAN module, written by Steve Hancock, makes the functionality of the perltidy utility available to other Perl scripts. That means you can write your own script for your own Perl style needs. I prefer to use the utility.
Installation
Installing Perl::Tidy on OS X is quite easy. From the Terminal use the sudo command to run the cpan command line utility. You need to be logged into an account with administrator privileges. This will pull down and install from CPAN, all the necessary code for using perltidy.
Using perltidy
Once the module has been installed (the utility is placed in /usr/bin ) you can use the command line utility just like any command in Terminal. Perltidy has a number of command line switches to modify it’s behavior. I have found that the default styling works well enough for me. To use the command line utility open up a Terminal window and type
:~ perltidy –b /path/to/your/code.pl
I used the –b switch to tell perltidy to create a backup of my script before doing its work. I want a copy just in case something goes wrong. Perltidy does its thing and then exits. If errors are found, e.g. a missing curly brace, perltidy will let you know and create a error file that contains more information about the problem.
Useful perltidy command: : ~ perltidy –html –pre code.pl This will create an html snippet with only the PRE section to code.pl.html. This is useful when code snippets are being formatted for inclusion in a larger web page (such as a blog).
Perltidy will take this code:
#!/usr/bin/perl my %var = { name => ‘khurt’, last => ‘williams’, location => ‘Princeton’, height => ‘164cm’, weight => ’64 kg’ }; foreach my $key ( sort keys %var ) { printif( “%s:%s\n”, $key, $var{$key} ); } #this prints the values in the hash
and turn it into this code:
#!/usr/bin/perl
my %var = {
name => 'khurt',
last => 'williams',
location => 'Princeton',
height => '164cm',
weight => '64 kg'
};
foreach my $key ( sort keys %var ) {
printif( "%s:%s\n", $key, $var{$key} );
} #this prints the values in the hash
To find out more about perltidy type “man perltidy” into Terminal to see the manual page. Prepare to be overwhelmed. Perltidy is feature rich.
I have found the eclipse IDE to be slow and cumbersome (and sometimes unstable). On my Mac I prefer to use something much simpler like vi or <a href="http://macrabbit.com/espresso/" target="_blank">Espresso</a>. On Windows I use <a href="http://www.activestate.com/komodo_edit/" target="_blank">Komodo Edit from ActiveState</a>. Thanks for dropping by. I appreciate the feedback.
btw, perltidy can also be used from eclipse with the very useful epic plugin which provides support for perl (incl. "visual" debugging) from eclipse -- http://www.epic-ide.org.
I have found the eclipse IDE to be slow and cumbersome. For my Mac I prefer to use something much simpler like vi or Espresso. On Windows I used Komodo Edit from ActiveState.
Thanks for dropping by. I appreciate the feedback.
I have found the eclipse IDE to be slow and cumbersome (and sometimes unstable). On my Mac I prefer to use something much simpler like vi or Espresso. On Windows I use Komodo Edit from ActiveState.
Thanks for dropping by. I appreciate the feedback.
btw, perltidy can also be used from eclipse with the very useful epic plugin which provides support for perl (incl. "visual" debugging) from eclipse -- <a href="http://www.epic-ide.org." target="_blank"><a href="http://www.epic-ide.org.</a>" target="_blank">http://www.epic-ide.org.</a></a>
btw, perltidy can also be used from eclipse with the very useful epic plugin which provides support for perl (incl. "visual" debugging) from eclipse -- http://www.epic-ide.org.
I have found the eclipse IDE to be slow and cumbersome (and sometimes unstable). On my Mac I prefer to use something much simpler like vi or <a href="http://macrabbit.com/espresso/" target="_blank">Espresso</a>. On Windows I use <a href="http://www.activestate.com/komodo_edit/" target="_blank">Komodo Edit from ActiveState</a>. Thanks for dropping by. I appreciate the feedback.
- spam
- offensive
- disagree
- off topic
Like