I spent some time yesterday trying to learn Perl. I'd looked at it some a few years back but never had a use for it. I now have a need to write a tool for our build environment and so, based on what is available, I am required to use Perl. The first thing I noticed about Perl is that it is very powerful. The second thing I noticed is that it is really ugly. There are too many ways to accomplish the same result. Many new languages suffer from this same fate, only to a lesser extent. What I want to talk about is keeping a language small and compact. If there is a way to accomplish a task, please don't invent another one with slightly different syntax just to save a few keystrokes. C++ is pretty good about this. Other than the multiple ways to cast, there is not much redundancy in the language. The namespace is fairly unencumbered by keywords. Modern languages like C#, Perl, Ruby, and Python, however, seem to burn namespace like it is going out of style. They invent new keywords and operators that add nothing to the language. One of my favorite examples comes from C#. The operator 'as' strikes me as wholly unnecessary. The following code accomplishes the same result:
CFoo cf = myObj as CFoo;
- and -
CFoo cf;
if (myObj is CFoo) {
cf = (CFoo) myObj;
}
In both cases, we are checking if myObj is of type CFoo and if so, setting the variable cf to point to it. Why the need for as? What does it add to the language?
Perl is much, much worse. A glaring example is the 'unless' operator. Instead of typing if(!foo), you can type unless(foo). Much better, right? No. There are many other redundancies. && and 'and' do the same thing. I can choose whether or not to use parentheses around my subroutine calls. You can put if before or after the code you want executed. The list could go on almost indefinitely. The worst part about Perl is that the advocates are proud of all of this redundancy. Even newer, more compact languages such as Ruby have redundant operators.
Here is my rule for adding a new operator or keyword to a language: Does this operator give me the ability to do something I couldn't do before? If yes, consider adding it. If no, reject it.
I'm not here to bash Perl or any of the other languages. They all have their place and are powerful. They also all have a large following. However, it seems like they could be even better if they were a bit more careful. Making something already possible merely syntactically easier has the effect of making the language more complex. While making something a bit simpler to express, you have made the language harder to learn and to retain in memory. If only language authors would think a second time before making an addition to their language. Having hundreds of keywords and multiple ways to do everything makes the language harder, not easier to use.