[ Perl tips index ]
[ Subscribe to Perl tips ]
Code layout is often a contentious issue in many programming teams. Programmers learn a particular coding style and rarely want to change it just to suit someone else. Programmers may have good reasons for using their particular style, and it can certainly slow them down or increase the rate of errors during any period of changing styles. However, having code which has been written with a hodgepodge of different code layouts is equally horrible for everyone to maintain.
While coding standards or guidelines are a good idea its often difficult to develop them or enforce them without buy-in from most of the programmers. It's even harder to ensure that they cover all the important aspects of writing Perl code.
Good programming practices, including style guidelines, are covered in great depth in our Perl Best Practices course which we're running November 14-15th in Melbourne.
In this perl tip we'll cover Perltidy, which can be used to reformat your code to adhere to your specified requirements. In addition Perltidy can detect errors in your code, and generate syntax-highlighted html copies of your code.
Perltidy never overwrites your original code, instead it provides a new
file of the same name as your original file with a .tdy extension. Thus
make sure you view that file if you want to see your changes.
Let's assume we have the following ugly code in a program called
test.pl.
use Carp qw/cluck/;my@chickens;sleep until$dawn;cluck$loudly until
open $coop;exit$coop;chomp$food and accept($scraps,$seed);shift
@straw,pop@eggs and cluck$more;until($dusk){seek($many,$worms,
$bugs);join flock($other,@chickens)if split/from others/;tell
WORLD,"sky is falling"if$airplane;}return 2,$coop and sleep
until$dawn;
We then run Perltidy over our test file.
perltidy test.pl
Now test.pl.tdy will contain:
use Carp qw/cluck/;
my @chickens;
sleep until $dawn;
cluck $loudly until open $coop;
exit $coop;
chomp $food and accept( $scraps, $seed );
shift @straw, pop @eggs and cluck $more;
until ($dusk) {
seek( $many, $worms, $bugs );
join flock( $other, @chickens ) if split /from others/;
tell WORLD, "sky is falling" if $airplane;
}
return 2, $coop and sleep until $dawn;
which is much easier to read.
If we don't like Perltidy's defaults we can easily select our own. Perltidy allows us to specify many options including: line length, number of indentation spaces, continuation indentation, tabs or spaces, the location of the open brace, cuddled elses, closing token placement, vertical and horizontal tightness. If we prefer the bsd style of bracket placement, and indent levels of 8 spaces, we can write:
perltidy -bl -i=8 test.pl
to get:
use Carp qw/cluck/;
my @chickens;
sleep until $dawn;
cluck $loudly until open $coop;
exit $coop;
chomp $food and accept( $scraps, $seed );
shift @straw, pop @eggs and cluck $more;
until ($dusk)
{
seek( $many, $worms, $bugs );
join flock( $other, @chickens ) if split /from others/;
tell WORLD, "sky is falling" if $airplane;
}
return 2, $coop and sleep until $dawn;
If Perltidy spots a syntax error in your code it will write out a file
called perltidy.ERR. For example, if we omit our opening brace in the
until loop, with the above code, we get the following error:
There is no previous '{' to match a '}' on line 13
13: }
^
13: Starting negative indentation
15: final indentation level: -1
15: To save a full .LOG file rerun with -g
To generate a syntax high-lighted html copy of your code run Perltidy with
the -html option:
perltidy -html test.pl.tdy
Note that this does not create a tidied version of your program at the same time, it only performs html mark-up. This means that you can use Perltidy to convert your code to into HTML without changing its layout.
If Perltidy is not yet installed on your system you can download it from Sourceforge:
http://perltidy.sourceforge.net/
This site also gives many more examples of how Perltidy can clean up your code and a Style Key ( http://perltidy.sourceforge.net/stylekey.html ) to best decide which options are required to suit your coding standard.
Using Perltidy allows everyone to program in whichever style best suits them and then tidy their Perl script so that it's readable for everyone. Perltidy writes better Perl code than most of us and is thus an indispensable tool in your toolkit.
[ Perl tips index ]
[ Subscribe to Perl tips ]
| Location | Course | Course Date | Duration | Early Bird Date |
|---|---|---|---|---|
| Sydney | Programming Perl | Mon 22 Mar 2010 | 5 days | — |
| Brisbane | Programming Perl | Mon 12 Apr 2010 | 5 days | — |
| Canberra | Programming Perl | Mon 19 Apr 2010 | 5 days | — |
| Melbourne | Programming Perl | Mon 16 Aug 2010 | 5 days | Mon 12 Jul 2010 |
| Sydney | Programming Perl | Mon 6 Sep 2010 | 5 days | Tue 3 Aug 2010 |
| Canberra | Programming Perl | Mon 20 Sep 2010 | 5 days | Tue 24 Aug 2010 |
For future dates, please see our training calendar.
This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.
If you have any questions please don't hesitate to contact us:
| Email: | contact@perltraining.com.au |
| Phone: | 03 9354 6001 (Australia) |
| International: | +61 3 9354 6001 |
Copyright 2001-2009 Perl Training Australia. Contact us at contact@perltraining.com.au