[Perl.sig] Perltidy

Litss Coordinator litss.coord at anu.edu.au
Wed Apr 27 10:02:46 EST 2005


==== News ====


==   Early Bird Dates This Week   ==

     Please note that the Early Bird Special for the following courses
     expires Friday, this week (29th April). Book now to secure your free
     books:

     http://www.perltraining.com.au/bookings/All.html

     *   Database Programming with Perl (Melbourne)

     *   Perl Security (Melbourne)

     *   Getting Started With mod_perl (Melbourne and Sydney)

     *   mod_perl 2.0, The Next Generation (Melbourne and Sydney)


==   mod_perl courses   ==

          *Places on these courses must be booked by this Friday*
                         * 29th April 2005.*

     Stas Bekman, leading mod_perl developer, and author of *Practical
     mod_perl* will be presenting courses in Melbourne and Sydney during
June
     2005. See the end of this tip for more information.


==== Perltidy ====

     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.


==== Using Perltidy on 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 at chickens;sleep until$dawn;cluck$loudly
until
             open $coop;exit$coop;chomp$food and accept($scraps,$seed);shift
             @straw,pop at eggs and cluck$more;until($dusk){seek($many,$worms,
             $bugs);join flock($other, at 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;


==   Finding errors with Perltidy   ==

     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


==   Generating HTML files   ==

     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.


==   More information and downloads   ==

     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.


==== Upcoming Courses in Canberra ====

     http://perltraining.com.au/bookings/Canberra.html

         Introduction to Perl:               17th - 18th May
         Intermediate Perl:                  19th - 20th May
         Database Programming with Perl:            23rd June
         Perl Security:                             24th June


==== Upcoming Courses in Melbourne ====

     http://perltraining.com.au/bookings/Melbourne.html

         Database Programming with Perl:            26th May
         Perl Security:                             27th May
         Getting started with mod_perl:              6th June
         mod_perl 2.0, the next generation:          7th June
         Object Oriented Perl:                7th -  8th July

     Note that places on the mod_perl courses must be booked by Friday:
                 ``29th April 2005''!.

==== Upcoming Courses in Sydney ====

     http://perltraining.com.au/bookings/Sydney.html

     Please note the change in dates for the mod_perl courses.

         Database Programming with Perl:             9th June
         Perl Security:                             10th June
         Getting started with mod_perl:             15th June
         mod_perl 2.0, the next generation:         16th June
         Object Oriented Perl:               21st - 22nd July

     Note that places on the mod_perl courses must be booked by Friday:
                 ``29th April 2005''!.


==== Corporate Courses ====

     http://perltraining.com.au/corporate.html

     Do you have a large group, or the need for training at a particular
     time? Perl Training Australia is happy to arrange a course in the time
     and place that best suits you. For more information read our page on
     Corporate Courses at http://perltraining.com.au/corporate.html or call
     us on +61-3-9354-6001.


_______________________________________________
This Perl tooltip 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 at perltraining.com.au
   Phone: 03 9354 6001

Perl-tips mailing list
To change your subscription details visit:
http://perltraining.com.au/cgi-bin/mailman/listinfo/perl-tips




More information about the Perl.sig mailing list