Skip to content

Perl Best Practices : Part 1

March 25, 2012

This a serious of Perl Best Practices I wrote for my teammates. Hope you would find it useful. Before starting, I would like to point out that please don’t follow these best practices blindly. It’s very important to understand what you are doing. I will try my best to describe why we want to follow the best practices in some circumstance, but may not be able to cover everything. Please contact me if you have any questions/suggestions/objections for any of these rules.

Make your Perl code easy to read

According to perltidy website, “Perltidy is a Perl script which indents and reformats Perl scripts to make them easier to read. If you write Perl scripts, or spend much time reading them, you will probably find it useful.”. Here are some code snippets that I shamelessly copy from perltidy website because I am too lazy to type my own.

$_= <<'EOL';
$url = new URI::URL "http://www/"; die if $url eq "xXx";
LOOP:{print(" digits"),redo LOOP if/\G\d+\b[,.;]?\s*/gc;print(" lowercase"),
redo LOOP if/\G[a-z]+\b[,.;]?\s*/gc;print(" UPPERCASE"),redo LOOP
if/\G[A-Z]+\b[,.;]?\s*/gc;print(" Capitalized"),
redo LOOP if/\G[A-Z][a-z]+\b[,.;]?\s*/gc;
print(" MiXeD"),redo LOOP if/\G[A-Za-z]+\b[,.;]?\s*/gc;print(
" alphanumeric"),redo LOOP if/\G[A-Za-z0-9]+\b[,.;]?\s*/gc;print(" line-noise"
),redo LOOP if/\G[^A-Za-z0-9]+/gc;print". That's all!\n";}

$_ = <<'EOL';
$url = new URI::URL "http://www/"; die if $url eq "xXx";
print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc;
print(" lowercase"), redo LOOP if /\G[a-z]+\b[,.;]?\s*/gc;
print(" UPPERCASE"), redo LOOP if /\G[A-Z]+\b[,.;]?\s*/gc;
print(" Capitalized"), redo LOOP if /\G[A-Z][a-z]+\b[,.;]?\s*/gc;
print(" MiXeD"), redo LOOP if /\G[A-Za-z]+\b[,.;]?\s*/gc;
print(" alphanumeric"), redo LOOP if /\G[A-Za-z0-9]+\b[,.;]?\s*/gc;
print(" line-noise"), redo LOOP if /\G[^A-Za-z0-9]+/gc;
print ". That's all!\n";

This should give you a brief idea about what Perltidy does and how powerful it is. This helps us to enforce code style in and across teams as long as we all

  1. install perltidy
  2. share the same .perltidyrc
  3. run perltidy before check in

We don't actually need to do all of them by hand. Hudson component job is a best place that we can set it up to tidy our Perl code automatically if you are lazy to run it before check in.

Here is my .perltidyrc file and I would like you to copy and paste to your home directory

[jqyao@springstation:build VIM ~]$ more .perltidyrc
# file:.perltidyrc
-l=100 # max line width
-i=2 # Indent level is 2 cols
-ci=2 # Continuation indent is 2 cols
-st # Output to STDOUT
-se # Errors to STDERR
-vt=2 # vertical tightness
-cti=0 # No extra indentation for closing brackets
-pt=1 # Medium parenthesis tightness
-bt=1 # Medium brace tightness
-sbt=1 # Medium square bracket tightness
-bbt=1 # Medium block brace tightness
-nsfs # No space before semicolons
-nolq # Don't outdent long quoted strings
-bl # opening brace on newline
-nbbc # no blank lines before whole-line comments
# Break before all operators
-wbb="% + - * / x != == >= | & **= += *= &= <>= ||= .= %= ^= x="

With the power of Vim, I am using perl support to run perltidy by just click \ry and it automatically tidy my code for me, very neat.


From → perl

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: