Servers Make Perl Scripts More Readable With perltidy

Make Perl Scripts More Readable With perltidy




Juliet Kemp

I am of course sure that everyone reading this is well aware of scripting best practices and always writes beautifully neat and well-set-out scripts that will still have meaning when you return to them in six
months time. You might not then need perltidy for your own
scripts.

Tip of the Trade:
Even if it’s not your own, messy code is a fact of life for every programmer. Clean up messes with perltidy, an app generated from the Perl module Perl::Tidy.

Even so, from time to time you might come across a script left
over from before your time or written by a colleague in a hurry that on
examination bears a strong resemblance to alphabet spaghetti.

Recent Tips
» IPTraf
» PAM
» Cracking Passwords

Read All Tips of the Trade

Enter perltidy, a program generated from the Perl module
Perl::Tidy, which saves you time by unmangling Perl code for you. You can
download it from CPAN, or it may be available through your distro. (Debian and
Ubuntu both provide it.)

Basic operation is simply to type perltidy badscript.pl. This
will leave the initial file alone and create a new tidied-up file with a
.tdy extension.

Alternatively, you can use the -b switch to do the opposite: Make a backup of the original file and then edit it in-line. The -syn switch will run a syntax check of the original code at the same time.

I tried it out on the second
prize entry in the 5th annual Obfuscated Perl Contest
. Although it didn’t simplify the program logic any, it definitely made it more
readable and gave a better chance of figuring out what was going on. Try it
out for yourself, and see if it can save you some time and mental effort in
disentangling confusing code.

Ed Note (Oct. 13, 2008): Thanks to
Randal L. Schwartz for pointing out it’s a good idea to set up tests for your code, to check that it does the same thing after perltidy is
finished as it did before. As he discusses here, ultimately the only thing that can interpret Perl code entirely reliably is the Perl interpreter itself. So be aware that there are circumstances in which Perltidy (and similar programs) can break code!

Latest Posts

Data Center Management Training & Certifications

For System Administrators looking for ways to bolster their skills or to get ahead of the competition when job hunting, data center management training...

VMware Player Bridged Network

When you install a Workstation Player, a VMware Player bridged network is automatically set up for you. However, you may run into issues connecting...

ManageEngine OpManager Plus Pricing & Review

Server management is a critical component of maintaining a healthy and efficient IT infrastructure. This requires System administrators to continuously manage physical server hardware,...

Analyzing Web Server Logs

Modern organizations track and log data for virtually all business processes, which is why web server log analysis tools are vital for effectively using...

Harnessing the Power of PowerShell Aliases

Creating aliases for PowerShell cmdlets can be a valuable tool for saving you time and headaches over remembering the names of commands. PowerShell was...

Related Stories