Monday 1 June 2009

Fast, concise and reliable code? Try Perl!

Maybe you're new to programming. Maybe you just like learning new languages. But, from so many options, which one to choose?

Sure, you probably heard a lot about about Java. PHP too, specially if you're into web programming. If you're into open-source, you might have also heard about Python and, if you're a Object-Oriented junkie, Ruby. In all cases, you probably also heard about Perl.

Unfortunately, chances are you heard a lot of FUD about Perl. This topic itself has sprung a lot of comments and replies, so I'll not go there. For whatever purpose or language, you should never pay attention to FUD. So, don't. Instead, I'm gonna talk a little about what makes Perl the language of choice for so many people and companies.

Perl is fast!

I know, I know, benchmarking is evil (you can see a broader discussion about it here and here(slides+talk)). But you don't even have to benchmark your code to see how fast it runs. Perl is not purely interpreted and has several optimizations during compile time. If even after that your code is not fast enough for your purposes, you can use XS, SWIG or even Inline to create parts of code in C (or even Assembly, for that matter) where speed is critical.

Examples include Perl's OpenGL implementation, which has no statistical differences between the C implementation and even overcomes C in some operations (impressed? see the full story). I've also seen Lorn++'s LWP::Curl fetch web pages faster than the curl binary itself (I'll probably blog about this later on, but if you're interested, he could use your help!).

Not only that, but a Perl implementation also won the WideFinder contest, where people attempted to write a program that would benefit from paralelism present in modern CPUs with slow clock rate but many cores.

Perl lets you do more, with less code.

Perl's conciseness is so amazing that it's a popular game among programmers to try and write a program that solves a particular problem with less characters as possible (this is called golf, and you can see here how Perl stands out from the crowd in it). Not only that, but one-liners can be so powerful there are a lot of system administrators who use Perl as a command line tool instead of as a programmer would.

Sure, you can write a complete wiki in 5 lines of Perl. But, of course, it doesn't mean you should. Professional programs, written by professional Perl programmers, tend to be clear and scalable, yet still concise. If you're into metrics, you know this usually means faster development and fewer bugs in your code.

I recently came across the Computer Language Benchmarks Game and a nice graph of programs benchmarked against their implementations in several different languages. The X axis is slowness, while the Y axis is code size. So, an utopic optimal language would have all their programs on (0,0). Here's the result related to this post:

As you can see, Perl is not only a heavy player, it's one of your best choices when it comes to speed, size and dependability. Of course, you can help your language (whatever it is) improve it's average - even Perl! But I digress...

Perl has a powerful and modern object system.

It's called Moose. Perl 5 provides all the material to implement object orientation (OO) as you see fit. While writing standard Perl OO is easy if you actually understand OO, it's through Moose that you'll experience the actual ease of use and power of OO. Moose proudly stole all the nice features from other object systems present in different languages such as Java, Ruby, Smalltalk, Ocaml, Perl6, and implemented some sweetness of their own, such as Perl Roles.

package Laptop;
use Moose;

extends 'Computer';

has 'weight' => (isa => 'Int', is => 'rw', required => 1);

Beautiful, simple, elegant, powerful. Moose.

Perl has an amazing and active community.

The Perl community is HUGE and vibrant. We have several Perl Mongers groups spread around the globe, so you'll probably find one nearby (if not, you can always create one!). There are monthly social and technical meetings, workshops and major Perl conferences called YAPCs. But, if people are not really your thing, mind the code they produce: The Comprehensive Perl Archive Network (CPAN) has more than 16 thousand modules ready for you to use, earning the common saying that, with CPAN, "90% of your program - whatever it is - is already written". Play with the search utility and see for yourself! Not only that, by the time of this writing there was an average of around 200 new modules/versions uploaded every month!

But, most importantly:

Perl is fun!

When you first dip your toe into Perl, you might be a little scared of how can there be so many ways of doing the same thing (in the Perl world this is called TIMTOWTDI). Perl not only has a plural and flexible syntax, it lets you extend it, bend it, and modify it to suit your needs and style. With Perl, you are not tied to the way the language developer's wanted you to write your program. It lets you write your code, your way. So, letting your imagination flow, you can actually have fun while coding, be it writing clear, elegant and professional code during work (which can even be enforced with Perl::Critic and allies) or writing many other forms of art in your free time.

Perl is freedom - try it.


  1. Hey! Is that last line a reference to Achewood[1]?

    I love your post; it's one of the best written advocacy documents of Perl I've ever read! Thanks so much for posting this.


  2. Oh, and technically, CPAN has over 16,000 distributions with over 68,900 modules, but that's probably unnecessarily nitpicky.

  3. Perl does not fail safely. That is, Perl programs will ignore I/O failures unless the programmer checks for them explicitly. This pretty much knocks it out as a reliable language.

  4. tutufan: You haven't seen the "autodie" pragma which is part of the perl core nowadays, have you?

  5. smueller: I hadn't. Thanks for the pointer.

    If this behavior were made the default, Perl would then have the failsafe property in this respect. I hope that this will be done, though I suspect that it will not.

  6. Mostly good, except Moose. Why do you have to do

    has 'weight' => (isa => 'Int', is => 'rw', required => 1);

    rather than

    required rw Int weight;

  7. tutufan: There's a general push to make the language give less rope to the user to hang himself with, while preserving backwards compatibility. For example, it's likely that the next version of perl (5.12.0) will have strictures turned on by default IF you put a "use 5.12.0" at the top of your program. This one line of cargo cult will not go away. Otherwise, perfectly legal programs which were written a while ago would stop to work. This is not Ruby, nor PHP. We don't just break stuff. We try to improve without forcing people into harakiri with every upgrade.

    Other such small improvements to making the default (given only a "use VERSION") saner are being discussed. One such potential thing is loading autodie by default. If that's going to happen, I suspect it'll take a while to convince people, though.

  8. smueller: I'm glad to hear things are going that way. The compromise you mention is not perfect, but perhaps is the best possible, all things considered.

    A warning by default if 'use 5.12.0' is not present might be nice, too. One could argue that it would represent a break with backward compatibility also, but against that I would consider that current Perl programs with unchecked I/O operations are already quite broken (and hazardous to Perl's reputation).

  9. This comment has been removed by the author.

  10. I love perl and use it for everything web related and all kinds of every day scripting.

    I've been programming professionally since 1987. I've tried every popular language along the way, but I always keep coming back to Perl as the most practical, useful and coding-time-efficient language.

    I also like the attitude of the Perl community that puts practicality and backwards compatibility first.

    My only criticisms of Perl are that because there are so many different and correct ways of doing the same thing, it can sometimes be difficult to comprehend someone else's code, even when well written.

    It's been a few years since I learned anything new about Perl. I've been doing OO Perl with Class::MethodMaker. I'm looking forward to exploring some of the new technologies mentioned in this article.

  11. > "the Computer Language Benchmarks Game and a nice graph of programs benchmarked ..."

    And since then the graphs and analysis have been corrected and updated - and the Perl graph looks very different to the one you are showing!

  12. @Lonely_Android: because that syntax would become unworkable. The example you give is an extremely simple one. You can provide many other options to define an attribute, like defaults, builders, predicates, etc. See the Moose docs for more :)

  13. Lonely_Android: you should look at MooseX::Declare and MooseX::Has::Sugar

  14. Thanks everyone for their comments! Below are some of my own:

    @ggvaidya: Thanks! It really means a lot :) I must confess though that I had no previous (conscious?) knowledge of Achewood, but Ray and I had the same thought (too bad I couldn't find a nice looking camel picture to go with the post :P)!

    @lonely_android: I don't feel that code is very perlish, but that's the beauty of it: if you prefer it this way, roll your own (if it's not already on CPAN)! I'm sure people sharing your point of view will greatly benefit from it. Check out Devel::Declare and have fun!

    @smueller++: thanks for giving tutufan the correct pointers to some of the new Perl features and proposals.

    @gmail: I hear you completely. Perl is the tool that makes programming an art (TM). As such, it leaves room for different styles, form and flow, which might prove themselves harder to read then monotone (boring) programs, specially when your own style differs from the original authors. It's like having an impressionist specialist comment on an expressionistic painting. Fortunately there are modules like Perl::Critic that can enforce style in group coding. Oh, and if you're trying Moose over Class::MM, it now has a shiny new Manual at the link I gave in the article - make sure you try it out, and blog about that too!

  15. Perl is great except when it comes to the job market. You can spend months and months looking for a Perl job, while your colleagues get cushy jobs in C#, Java, and even C++.

    I think learning/using Perl is a great idea, just please don't make Perl your only language.

    Also, most of the benchmarks out there show that Perl does not, in fact, perform as fast as C, even if you take into account native code optimizations. C's optimizing compiler is still second to none (but again, I'm a Perl fan).

  16. Isaac Gouy said, "And since then the graphs and analysis have been corrected and updated - and the Perl graph looks very different to the one you are showing!"

    Which isn't entirely accurate. The "correction" is that the more recent graph is using a known broken version of Perl, which has a serious performance bug. That bug has been fixed. Seems a little unfair to Perl, as the older graph is more accurate.

  17. @codingthewheel: With regards to the job market, I think it depends where you are. Experienced Perl developers in Australia seem to be in quite high demand, and I've been watching them get imported from overseas to satisfy that demand.

    I've also seen a lot of geologists and hydrologists learning Perl, because their jobs regularly involve massaging large amounts of data.

    With regards to Perl not being as fast as C, well, *nothing* is as fast as C. But being able to use Inline::C for the most speed-critical parts of my projects means I'm very happy that C is so fast. ;)

  18. @tutufan

    I suspect autodie will get added to Modern::Perl at some point.

    Thus u can replace all this....

    use strict;
    use warnings;
    use feature ':5.10';
    use mro 'c3';
    use autodie;

    with just this....

    use Modern::Perl


  19. codingthewheel: See for example Perl jobs in UK, number is lower than in same period last year due to crisis, but rank (% from total) is higher. And if you want Perl job, you should look at main Perl jobs site.

  20. SwellJoe > The "correction" is that the more recent graph is using a known broke version of Perl, which has a serious performance bug.

    The more recent graph uses Perl 5.10.0 - which is shown as the latest version @

    In reality, the "correction" was required because the blogger did not understand that data file included rows for error programs, timed-out programs, programs using completely different algorithms, 2nd 3rd and 4th fastest programs...

    Now he has pre-filtered data suited to his analysis:

  21. Perl is concise? concision needs clarity ( and so a better word might be condensed.

    - Paddy.

  22. @Paddy - to my knowledge Perl is concise by that exact definition. Like I said in the article, it lets you do more, with less code - but that code doesn't have to be obscure or unclear at all, quite the contrary. It's very unfair to say Perl isn't clear when one is illiterate to it.

    Sure, you can write small and obscure Perl code, just as with the next language, whatever it is (and yes, Python included). It doesn't make the language itself - or other, more professional, code in that language - unclear.

  23. Agree with @codingthewheel; I've used Perl for 16 years, but only for 3 of those years was it a required programming language for the job. In comparison, Java was required for 7 of those years... although I'd never describe a Java job as "cushy".

  24. And now you can tryperl online from the comfort of your browser :