Perl, Python, Ruby
Wednesday, October 8, 2008
Perl
Perl has precisely one thing going for it -- legacy. Because it came first, Perl has the largest installed user base, and therefore the most libraries written for it. You'll also be more likely to see Perl preinstalled on any 'nix machine than the others, although these days all three languages are likely to be preinstalled. Macs and Linux machines come with all three out of the box.
Having said this, Perl is an abysmal language. It's not Larry's fault, he did the best he could at the time. It was certainly a big improvement over shell scripts, which is why it became so popular when it was the only alternative. But today there are better alternatives -- Python and Ruby. The only advantage Perl still retains is that it has more libraries written for it, so if for example you're writing a program to control some obscure device or to create documents in some proprietary format, you might be more likely to find a library to help you in Perl. But if you don't need Perl's libraries, or you're able to find (or write) the library you need in Python or Ruby, then do yourself a favor and steer clear of the miserable swamp called Perl.
I'm sure someone out there will try to defend Perl as a language, but truly it's hardly worth debating... you'd have to be Karl Rove to spin Perl as anything but vastly inferior to Python or Ruby. Very briefly: Perl's tacked on "object orientation" is a joke... totally piecemeal and cumbersome to use. Having to manually dereference all your references is another joke... a very unfunny, painful joke, especially when there is any sort of nested dereferencing involved, which, combined with the context-sensitive behavior of variable assignments rapidly gets nightmarish. And then there are the endless special symbols and functions that each have some particular specialized use, instead of building more generalized tools and frameworks. Then finally there's the punchline of all the similar but slightly conflicting syntaxes and symbols.... does everyone understand the differences between the () versus the [] array constructors, or even the hash {} constructor that's assigned to an array @ variable? So many pointless pitfalls designed only to give you a headache. And how about the backtick ("``") versus "system()" versus "do" versus "pipe()" versus "open |" versus "exec()" ways of invoking another process? Next language, please?
Python
Python is smack dab in the middle between Perl and Ruby I feel (even though Ruby obviously borrows more syntax shortcuts from Perl than Python does). Python is moving in the right direction away from Perl and is in most ways a big improvement, with better (but still flawed) object orientation and automatic referencing/dereferencing, but it's still in many ways just a half-measure, and I feel as long as you're abandoning the legacy (Perl) why not go all the way and jump to the best (Ruby). Some gripes I have with Python:
- There is no way to privately scope your variables and methods! What on earth was Guido thinking? Instead there is a name-mangling convention with underscores that relies on luck and obscurity to hope for the best. It's a violation of a bedrock principle of object oriention -- encapsulation -- and it's absurd.
- You must manually pass the "self" reference into every method call to be able to reference the self instance. What a needless annoyance! Why on earth doesn't the language do this for me?
- It is somewhat object oriented, but still relies on numerous magically floating "built-in" methods that do not participate in the object oriented hierarchy, and thus cannot be overridden, and it's luck of the draw whether a "special" function exists to perform the given operation on the given class you're working with. For example, to get a hash value for an object, there is the hash() function. But now what if I need to use a different hashing algorithm? Then I need to write one and put it somewhere else and invoke it differently. Or I can call dict() to get a new dictionary, but what if I want to initialize my dictionary in some way other than the predefined overloaded constructors provided with this method? Then I have to write something totally different. It's just too arbitrary and inconsistent. Object-orientation was developed for a reason! Even worse, there are full-blown "built-in" classes that also don't participate in object orientation, although this was finally improved (but not exactly fixed) after Python 2.2.
- Space-based scope is a cheap gimmick that raises more problems than it solves. What happens when you copy some code from a webpage or integrate with another code base that follows a different spacing convention? What if someone accidentally uses a tab instead of 4 spaces?
- Python pointlessly broke the C/Java/Perl string syntax conventions for quoting, concatenating, etc. that most programmers are familiar with. CORRECTION: A reader has pointed out that Python uses the + for string concatenation just like the other langauges, so that is good, and my complaint on this was wrong. And quoting is done "normally" too... only the triple-quote for quoting multi-line passages is unique, but that's not an overly burdensome alteration in my opinion, even if I do still prefer the greater flexibility of being able to choose your own delimiting token in Perl/Ruby style "here" document quoting.
Ruby
Ruby is the cream of the crop. Ruby is the gem, the most exquisitely perfect language known to me, and as such I assume that in time it will become the most widely used.
It is true that currently Ruby is the slowest of these three scripting languages, but I hope you're not using a scripting language for speed! Anyway, for any ordinary administrative task this speed difference is not noticeable or relevant. And for more heavy-duty programming, computers are getting faster every day while humans are not, so I'll trade processor time for human time any day, because Ruby is still fast enough for most needs. And if you're really programming something heavy like a trading engine or a video game, then I'd suggest that you not use any of these languages and go back to C/C++ or Java!
Ruby is elegant, consistent, and expressive as hell. Ruby has so many great ideas rolled into one, like blocks, which eliminate the need for contorted callback patterns in other languages. [ADDED 11/21/2008: Or automatic attribute accessors, or using the bang (exclamation) in method names to denote methods which modify their receivers, or having any variable's scope be immediately obvious by its name, or making singletons as quick and easy to implement as simply importing the singleton mixin -- at every turn Ruby takes away the drudgery and reminds me what a refreshing delight it is to program in.] Ruby makes advanced metaprogramming techniques a breeze, it makes everything a breeze, and Ruby is the only one of these three languages that's totally, completely, 100% object oriented, no exceptions! And Ruby is so flexible and modular that you can really get into the internals of how Ruby itself operates and change it if you need to.
At the same time Ruby still provides little syntax shortcuts for lazy programmers like me, but the critical difference is that every shortcut is backed up by a true method that is fully accessible and customizable like any other... so no special "magic" floating functions. Ruby is so well thought-out, I marvel at it every time I use it.
By the way, it still lets you write a quick 3-line script if you want without consciously dealing with objects because Ruby will in this case automatically wrap your script into a default object for you.
Ruby has also been criticized for its handling of threads, but this to me is an implementation detail that will improve with time. My interface to the language is through its syntax and programming idioms, and these are clearly best with Ruby. I'd also again suggest that if massive multithreading is truly your aim, then you might try Java instead of any of these scripting languages.
And for pure programming pleasure, which is to say writing elegant, maintainable code, expressing yourself succinctly while still having an unbounded range of possible expressions, Ruby is just the finest damn programming language I ever came across. Ruby just plain gets it right.
So in conclusion, if you're looking at these three languages for the first time, I'd strongly recommend that unless you have a specific need not addressed by Ruby, that you should make every effort to use Ruby instead of the other two. In my opinion it's an open-and-shut case, Ruby, currently the least popular of these three languages, is an obviously superior language that I assume will over time win out over the other two, and as more libraries are written for it there will simply be no excuse left to use the other languages except to maintain legacy code. I'm so happy that David Heinemeier Hansson chose to build the Rails framework around Ruby, as now Ruby finally has the "killer app" catalyst to launch it to the stardom it deserves.
Recent comments
- Or....
48 weeks 2 days ago - reinforcing the point
50 weeks 1 day ago - greed isn't THAT good
50 weeks 6 days ago











Comments