It was alarming to read in a recent article- The Rise and Fall of Languages in 2012- in which the Editor-in-Chief of the Dr. Dobbs Journal, Mr. Andrew Binstock describes Perl as “continuing its long decline” and that “Perl is in an irretrievable tailspin” based on statistics of the number of Google searches for Perl. Nothing in the article discussed what was lacking feature-wise in the language that may be behind this “tailspin”. While I am no authority on programming languages, I thought it was only appropriate to reflect on my 14 year affair with Perl.
I stumbled upon Perl back in 1999. I was an FPGA Logic Designer at Lucent Technologies proficient and programming in C, C++ and VHDL. Web Programming seemed like an exciting world and Perl was advertised as the “duct tape” that glued the World Wide Web together. After a 1-day workshop at Lucent, I quickly realized that Perl was a godsend to hardware developers. It combined the power of C and AWK making it a great language for developing hardware automation tools. As a logic designer, Perl augmented my abilities by providing me with a powerful language that was easier to create tools with. I created tools for “stitching modules” for chip design and testing, and designing 2-pass assembly languages customized for test benching VHDL/Verilog chip design.
In fact, quite a few of my peers purchased Perl books thinking the language does magic on its own only to quickly find out that one must first learn how to program. But it is an easy language to learn.
Over the years, my fondness of Perl has only grown stronger. I write anywhere between 5,000 to 15,000 lines of NCL Perl code a year from throwaway scripts to large-scale and robust factory test applications. Rather than compare Perl to other peer languages, I am going to list the top Ten Reasons behind the Power of Perl as relates to my particular domain of work. This can serve as a benchmark and a guide for folks looking to adopt a general purpose “scripting” language and whether Perl is the right choice for their needs. Other languages in this domain may include Python, Ruby, TCL, and PHP to name a few.
Regular Expressions is by far the most compelling gravitational attraction of Perl.
It is no secret that Perl regular expressions are the standard by which most languages compare themselves to. Data continues to have an ever-growing importance in today’s world. We live in a Data Jungle! Regular Expressions provides us with the power to slice and dice data so that we can measure, learn and make intelligent decisions.
Hashes or content associative-arrays provide us with a quick way of creating Lookup Tables. And in the world of data and automation, lookup tables are indispensable when it comes to creating tools. Listing all the great ways of using hashes is beyond the scope of this article. The vast majority of my applications would not be without the power of Perl Hashes.
A programmer’s productivity is greatly enhanced by not having to manage memory allocation and de-allocation. Arrays and lookup tables can grow and shrink as need dictates. Complex Structures can add and remove fields on the fly. “What a relief it is” not to have to worry about memory management
Serious programming requires the ability to create complex structures. Perl supports references/pointers which is the key to being able to create complex structures. Again, there is no need to pre-declare fields. Keys can be added and removed on the fly.
Large-scale programming is not possible without support for modules. Perl’s modular programming uses “Packages”. Many of my applications consist of 20 or more modules with each module housing a single subroutine call. Packages are also used in Perl’s Object Oriented Programming.
The ability to manipulate arrays in Perl is just beautiful. One can dynamically add/remove elements from the end of the array using PUSH/POP dynamically move elements into the front of the array using UNSHIFT/SHIFT. Arrays can be indexed from positively from the beginning of the array, or with negative indices from the end of the array. Building FIFOs, LIFOs, and Stacks is so easy. Split and Join are two built-in functions that allow the creation of creation and collapsing of arrays. Splice gives us the power to remove and add elements within the array. Sort and “reverse sort” allow for sorting in both directions.
Beginning programmers can easily be turned off by Perl’s lack of support for multi-dimensional arrays. But the better way to create complex structures including multi-dimensional arrays is to use references. And one cannot fully exploit the power of any language without the use of references.
Perl is a higher level language. It is a smart language. It simplifies its variable types to scalar, array and hash.
We never have to worry about variable types. Perl knows when to interpret a value as an integer or a string. The right-hand side knows whether the left-hand operand is asking for an array or single-value. And using “wantarray”, one can write his/her own subroutine modules with similar smartness. And arrays, hashes, structures can grow and shrink dynamically-Perl takes care of it for us!
It also supports a large number of great built-in functions like Map, Grep, Join, Split, Splice, Sort and Reverse Sort just to mention a few. Take a look at the Schwartzian Transformation to see the power of sorting complex structures using the Map function.
I love the ability to look at a variable and determine if it is of type $scalar, @array or %hash by the prefix. When it comes to references, I usually need to do extra work and tag a “_ptr” to the end of the declaration to make my code readable.
Perl is more than a scripting language. It is a Class A general purpose language. It supports threads and recursion. In one application, I have 64 threads running diagnostics in parallel on a chassis in factory. Time-savings from using threads in a production factory test amount to big savings in dollars.
Recursion is also a standard feature of Perl. I have written a number of specialized recursive scanners used in data collection. The CPAN library “Find” also uses recursion in searching directory trees.
Perl supports Object-Oriented programming and the new Moose library automates and simplifies much of the work.
Graphical support is made possible by using Perl/Tk. One of my fun projects was to design and program an analog watch with Perl/Tk. I thought that would excite my 8-yr old to dive into programming. It had the opposite effect! But she did like my Math program for doing timed addition, subtraction and multiplication. She found use in that.
Perl is an open-source language with a strong community of support. The Perl Comprehensive Perl Archive Network is a repository of over 25,000 modules. My use of Perl today would be very limited without the ability to use modules like DBI, Excel, Telnet, FTP, Ping, and Win32:Serial Communications to name a few of the modules critical to my field of work.
Learning Perl is made easy by the many free-tutorials on the web. Simply start with www.perl.org and start your discovery and learning process. And the Perl Community has been blessed with so many great and active authors. “Learning Perl”, “Intermediate Perl” and “Programming Perl” are three essential books to mastering Perl. In this past year, new editions of the above 3 books were released to reflect Perl language changes. Other great books worth mentioning are “Effective Perl Programming”, “Perl’s Best Practices”, and the “Perl Cookbook”. Mastering Perl/TK is another great book for learning how to design graphical interfaces. Network Programming, though slightly outdated, is a great text on how to use Perl for network programming.
ActiveState deserves special mention here. For a small fee, one can purchase a compiler that creates a pseudo executable. In deploying applications to remote sites, Active State’s PDK allows us to compile our application into an executable. Just simply drop an executable at the numerous factory test stations and we are done. You only need to maintain Perl Libraries at your development workstation. There is no need to worry about loading the latest libraries at every test station.
Perl continues to evolve over time. The few criticisms that I have had of Perl have mostly been addressed starting with release 5.10.
Beginner programmers tend to miss the need for strict mode where variables must be declared prior to usage with “use strict”. Perl by default did not require one to declare variables. So one writes “$cntr=5” and then misspells the variable 30 lines down and writes “$ctr++”. And hours are spent trying to figure out why $cntr is still displaying 5. Programming without strict mode is playing with fire.
Perl now defaults to “use strict” as the default.
It took me two years before I learned about “use strict”. And I still see folks writing code without “use strict” in their code.
Perl 5.12 now makes strict mode the default. This is great move by Perl!
Perl 5.10 resolved one of my major criticisms-the absence of a SWITCH statement. State-machine programming is at the center of most of my development and so having a switch statement makes code far easier to read and maintain vs. sifting through “elsif” statements. 5.10 gave us the “given-when” switch statement. And Perl switch statement is a powerful one.
Perl added support for state variables. This can be a handy feature eliminating the need to pass variables back and forth between subroutine calls.
Perl does graphics by loading the CPAN module TK. Unfortunately, when attempting to build a GUI interface to a threaded application, Perl/Tk would not support threads.
Block comments are really nice. These should be added.
This can be an important issue for beginning programmers and the Perl Community should look at adding this feature. But as mentioned earlier, no serious programming can be done without references- and this holds for any language.
The strengths noted above for Perl may well be the strengths of other competing languages. I am not in a position to compare Perl to Python or other similar language. So the following comments on Python and PHP will be brief.
My first turnoff to Python is the use of white space indentation over curly braces for block definition. This may be fine for many but I need my curly braces!
Would I drop Perl for Python? I see no compelling reason why. So far, I have yet to come across a task where Perl was not able to easily implement any algorithm in my domain of work. This in no way implies that Python cannot do the job as well or even better.
I use PHP in some of my Web server programming. In building my first MySQL database years ago, Head First PHP and MySQL was a great text. And so my first MySQL interface was using PHP. I also use Perl and see no advantage of PHP over Perl. Are there speed performance advantages of one over the other? I do not know.
PHP apparently does not support STRICT mode for variables and as such it is easy to misspell a variable and spend hours debugging your code. Perl finally realized in 5.12 that STRICT mode should be the default and not an option. That was a good move on the part of the Perl guys.
The Power of Perl as outlined above relates to several domains of work related to data, tools and automation. My experience is testimony backing Larry Wall’s statement that Perl is a language for “making simple things easy and difficult things possible”. Larry Wall is the creator of Perl.
But in the world of data, and post processing, Perl is the Practical Extraction and Reporting Language of choice.
Learning a first programming language with Perl can be a fun experience. With a little guidance from me, a 12-year old was able to learn his first programming language using Learning Perl 6th Edition. It just goes to show that Learning Perl is easy!