• Add -w to shebang line to enable warnings. This helps point out flaws in your code during runtime.
  • use strict; and declare all variables with my. This forces you to keep your syntax clean and correct and avoid use of barewords.
  • Keep your code simple – KISS (keep is simple stupid). With Perl, there is more than 1 way to handle most objectives – try and keep it simple. I’ve experienced working with Perl scripts written by others and some people just have the knack of complicating the hell out of it. For example, one fellow wanted to find a file in a directory. Instead of just if (-e /path/filename), he or she read the entire directory into an ARRAY and then applied a grep to find the file. 10 lines of code to do what if (-e /path/filename.ext) can do!
  • Always comment your code and keep others in mind when doing so. Someone else may have to troubleshoot or take over your Perl script in the future. Also useful for when revisiting after 6 months. Sometimes I go back to my old Perl scripts and ask myself, “What the hell was I thinking!”.
  • Split up functions by subroutines. I never have one subroutine handle more than 1 function and they are usual designed to stand alone. Other subroutines will utilize other subroutines, but each remains 1 function. This practice allows me to keep my code organized and easier to update when needed. Its also great practice for code reuse.
  • Create modules of common functions. If you have several scripts utilizing a common function – create a module for them to share. I create modules for exception handling and logging, notifications, and many other things such as SecureTransfer or NDM functionality for file transfer in multiple scripts. This helps to reduce the time to update those features when needed. I have seen people duplicate common features in hundreds of scripts and find themselves having to modify each one when a change was needed. I’ve made this mistake myself and learned from it.
  • Learn and master regular expressions. This magic word, regex, is godsend! I use it all the time and its POWERFUL!
  • Practice code reuse. Build up a library of code that works. Saves a ton of time when scripting for new projects.

« »