pozorvlak ([info]pozorvlak) wrote,
@ 2008-04-28 00:18:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Entry tags:beware the geek, computers, perl, programming

Why I like Perl
I wrote this in an email to a colleague of [info]totherme's a while back, and have occasionally felt the need to refer to it since. Fortunately, I have a place where I can post my ill-informed ramblings for all the world to see, so here it is :-) It's an attempt to convince a computer scientist, who I believe comes from the Haskell/static typing camp, why Perl is worth learning.

[If you're not a computer scientist, the two chief reasons to learn Perl are (1) it's incredibly useful, (2) it's great fun. But I digress.]

In many ways, Perl occupies the opposite corner of design-space to Haskell. To a first approximation, Haskell proceeds from the question "what if everything we think we know about language design is right?", whereas Perl proceeds from the question "what if everything we think we know about language design is wrong?" Consequently, learning Perl is essential for any student of programming language design. It's not so obviously revolutionary now, because some of Perl's design decisions have since become accepted into the conventional wisdom, but there are still some surprises.

The original author, Larry Wall, was trained as a linguist; consequently, Perl takes inspiration from human languages more than it takes inspiration from mathematics (it's one of the few programming languages with pronouns, for instance). It also has no hesitation about borrowing good features from other languages: built-in hashes from awk, regular expressions from SNOBOL, Python's OO features, Lisp's closures, C's syntax, Ada's module system. This leads to a lack of consistency, but consistency is not rated very highly in the Perl community (There's More Than One Way To Do It). The default assumption is that the programmer (a) is competent, (b) uses the language regularly. Consequently, if there's a choice between maintaining consistency and providing some useful special case (particularly if it's a common special case), they'll often go for the latter. If you need a Perl-like language infrequently, Python occupies roughly the same niche but aims at consistency and simplicity. Python, though, lacks one of Perl's most interesting and useful features, which might be called implicit type coercion. From a Haskell perspective, if the Python interpreter encounters a type error involving basic types in your code, it will (safely) bug out at runtime with a helpful error message, but the Perl interpreter will attempt to recover by silently applying type coercion functions and continuing. If that sounds dangerous, it's because I've described it from the wrong viewpoint: from a Perl perspective, it means that you can largely ignore variable types when writing code, freeing up your mind to concentrate on your actual algorithm. I've written more about this point at http://pozorvlak.livejournal.com/47565.html .

The assumption that the programmer is competent manifests itself in other ways. One of Perl's mottos is "easy things should be easy, hard things should be possible", and so there are few limits on what you can do if you really try (though occasionally the mechanisms involved are a bit grubby and ad-hoc). From a Haskell perspective, this means that Perl code provides few guarantees. It's therefore essential that you read the documentation, and conversely that you provide good documentation for code that you write (this is considered part of the definition of competence). I read something by Cale Gibbard a while ago about how he'd made a big change to some Haskell module without ever reading the docs, simply by checking that everything compiled and the tests passed. In the Perl world, this would be somewhere between impossible, inconceivable and criminally negligent :-)

The downside of providing freedom for good coders to do wizardly things is that it makes it possible for bad coders to do stupid things. This is unfortunate. The solution is held to be self-discipline and education, and not hiring bad coders in the first place.

Perl is the Practical Extraction and Report Language, and a lot of its features are there to help with everyday scripting, data-munging and administration tasks, particularly in a Unix environment. In particular, there are a lot of features there to help with writing "one-liners". A lot of people say that this makes it unsuitable for writing large programs; this was true for versions 1-4, but version 5 improves the situation here a lot. People can and do write maintainable Perl 5 programs that are hundreds of thousands of lines long. It's also worth mentioning that you can do a lot more in 1000 lines of Perl than you can in 1000 lines of, say, Java. If people say negative things about Perl, it's always worth asking which version they used: if they say 4 or less, you can safely ignore them. Perl 6, a major redesign, has been on the cards for quite a few years now, and it should iron out some of the language's more irritating quirks while incorporating some nifty things from Haskell and Lisp. Unfortunately, they're learning the Mozilla Lesson that ground-up rewrites take a lot longer than you'd think. They seem to be making good progress now, though, and some useful Perl 6 features have been incorporated in version 5.10.

[ykokese from Reddit makes an excellent point: "I believe learning that you can shoot a problem with a one-liner is important too. I'm not trying to be witty, I truly think that Perl marked the beginning of an era by making it possible to solve problems an order of magnitude faster than before, and indeed introduced the stop-messing-around paradigm."]

I should certainly mention CPAN, the Comprehensive Perl Archive Network, which is without doubt the world's largest collection of open-source user-contributed add-on modules, widely mirrored and with a nifty tool for recursively downloading and installing modules. There is almost always a CPAN module to help with whatever task you are attempting. It might not be good, mind, but it's almost certainly going to be easier to start with some CPAN module than roll your own solution. And at their best, CPAN modules are stunningly good (the parser generator Parse::RecDescent springs to mind, here, but there are so many good ones - have a look at Mark Fowler's Perl Advent Calendars for some more recommendations). Perl's paradigm-agnosticism helps here, too: if it makes sense for a module to be OO, it can be OO; if it makes sense for it to be functional, it can be functional; if it makes sense for it to be a bunch of imperative procedures, it can be. All in all, it's the world's premier laboratory for add-on module design and implementation :-)

It's not all roses: Perl definitely has its irritating quirks and defaults-that-shouldn't-be. A while ago, I tried to think of five things I hated about the language and came up with ten. See http://pozorvlak.livejournal.com/49333.html for the list.

If you're interested in learning it, I've posted some advice on resources at http://pozorvlak.livejournal.com/58387.html . I'd also be happy to answer any questions. But since you're in London, you should seriously consider subscribing to the London Perl Mongers mailing list (london.pm.org), or going along to some of their meetings - there are some awesomely good programmers in that group, and they're a pretty friendly bunch. London.pm tech meetings are a great way of opening your eyes to how much is possible.

[Note: I specify London.pm mainly because I used to be a member and can personally vouch for their excellence. Your local Perl M[ou]ngers group may well be just as good, and is certainly worth looking at.]

Some of the stories in Memories of 20 years of Perl may also help convince you to take a look. I especially like the story in the second comment. Larry Wall's speech Perl, the first postmodern computer language is also compulsory reading.

I hope that helps!



(Post a new comment)


[info]thesz
2008-04-28 01:38 am UTC (link)
There is very little class of jobs where perl could be used regularly.

The above oneliner says it all. ;)

(Reply to this)(Thread)


[info]ryani
2008-04-28 03:23 am UTC (link)
Actually, I've seen very few programming jobs where you -couldn't- use perl regularly. Sure, it isn't going to make up the core of whatever product you are working on, but every project needs little administrative tools & scripts, not to mention the "I need to figure something out from a logfile RIGHT NOW" problem that perl so excels at.

That said, the team I'm on seems to use python for these purposes. Perl is getting a bit deprecated because python does basically everything we want from perl and doesn't require a language wizard to make it not be write-once.

(Reply to this)(Parent)(Thread)


[info]thesz
2008-04-28 04:05 am UTC (link)
I think your comment is applicable for very small amount of "regular use." ;)

Those tools & scripts are done once and for long time.

And I agree that more regularly structured Tcl and, say, Python are just more convenient to use. And more productive after all.

PS
I once wrote two small Tcl programs to solve our small problem, just two little different algorithms. I tried them, found nothing interesting in results and deleted them. Then my colleague came up and said "Here, my Perl oneliner for that problem! Can you beat it in size?" ;)

(Reply to this)(Parent)


[info]pozorvlak
2008-04-28 08:41 am UTC (link)
Au contraire.

(Reply to this)(Parent)(Thread)


[info]thesz
2008-04-28 09:05 am UTC (link)
Good argument. ;)

I change my statement to "For me, there is..." And generalize to "For smart people, for very large values of smart, there is..."

Because I can. ;)

(Reply to this)(Parent)(Thread)


[info]pozorvlak
2008-04-28 09:37 am UTC (link)
I'm sorry, but you have to generalize to very large values of "smart" to exclude, say, Damian Conway :-)

(Reply to this)(Parent)(Thread)


[info]thesz
2008-04-28 09:59 am UTC (link)
Measurement error. I'll live with that. ;)

BTW, only two of his papers are about Perl.

(Reply to this)(Parent)(Thread)


[info]pozorvlak
2008-04-28 10:17 am UTC (link)
Sure, but he's also written rather a lot of CPAN modules, many of them insanely brilliant. Or possibly brilliantly insane.

Plenty of other smart people use Perl, but perhaps that's not such a recommendation - plenty of smart people use Fortran :-) But I will say that one of the things that surprised me about London.pm was how many of them knew Haskell. This was in 2004, before the boom had really started.

(Reply to this)(Parent)(Thread)


[info]thesz
2008-04-28 11:08 am UTC (link)
I started to use Haskell in 1999. ;)

In 1998 I made an implementation of interesting idea (like one proposed recently by John Carmack - using octree for ray-tracing). I implemented it in C, and thought that C would be not a good language to experiment on my idea. So I started to search through lists of programming languages and, on second or third iteration was struck by Haskell elegance.

Now I use Haskell almost everywhere. ;)

(Reply to this)(Parent)(Thread)


[info]pozorvlak
2008-04-28 11:33 am UTC (link)
Then you have a few years on me :-) I first started using Haskell in 2001 or 2002, then hit a problem I couldn't solve in about 2003 and abandoned it for a while. I've tried to pick it up again a couple of times since then.

But still, Haskell was a lot less well-known in 2004 than it is now. I remember that I usually had to preface any discussion of it with "have you heard of Haskell?"

(Reply to this)(Parent)


[info]necaris
2008-04-28 06:13 am UTC (link)
If you need a Perl-like language infrequently, Python occupies roughly the same niche but aims at consistency and simplicity

True. I don't know about you but I think Python tries harder, somehow, to be a *proper* language ("No! It's not just a scripting language! Look, we have built-in OO!") and consequently I find it rather harder to throw something quick together than I do in Perl.

What [info]ryani says is what I end up doing too -- if I want to reuse and expand the script, I often rewrite it in Python. But then, if it does the job, why does it need to be more than write-once ;-)?

(Reply to this)(Thread)

why does it need to be more than write-once ;-)?
(Anonymous)
2008-04-28 09:58 am UTC (link)
This is the biggest problem that imprecates all the "admin" scripts!

Why cant we treat those sidekick scripts as 1st class citizens? you will be surprised to find that the world has suddenly become a better place once you do it. just try it!

Every "script" starts its life as a one liner - does not end as a one liner!
it ends its life as a gigantic ball of turd that will elude even the bravest and smartest on your team.
Then it will die a slow and painful death as it gets rewritten from scratch the right way.

Treat every script as though someone else will have to maintain (someone could be you 24 hours from now)

clean_scripting:
Thats where Python shines

If its a true one liner - don't ever save it to a file - thats been my strategy. Let it die a premature death - right after its born :)

(Reply to this)(Parent)(Thread)

Re: why does it need to be more than write-once ;-)?
[info]pozorvlak
2008-04-28 10:19 am UTC (link)
If I need to save it to a file (rather than specifying it as an argument to perl -e), the first thing I do is tidy it up. If I need to save it, then I've already demonstrated that it's too hard to write it in one-liner style!

(Reply to this)(Parent)(Thread)

Re: why does it need to be more than write-once ;-)?
[info]necaris
2008-04-28 11:03 am UTC (link)
Well, that's exactly it -- if I'm doing anything complex enough to require Perl over a shell script, and I can be bothered to save it properly, then I'll make sure I can grok it at a glance.

There's some horrendously ugly Python scripts out there as well (I've written a couple!) :-)

(Reply to this)(Parent)

Re: why does it need to be more than write-once ;-)?
[info]pozorvlak
2008-04-28 10:21 am UTC (link)
BTW, not dissing Python at all. Python's a great language, and I'd very probably be using it if Perl didn't exist (though I might go for Ruby instead). On the other hand, I think Python's less assumption-challenging than Perl, and thus less interesting from a language-design perspective.

(Reply to this)(Parent)(Thread)

Re: why does it need to be more than write-once ;-)?
(Anonymous)
2008-04-28 02:35 pm UTC (link)
I was using perl first when i switched to Unix. Back then i thought it was a great language.

I think perl is _still_ a good language BUT it has weaknesses which I think were never addressed. People may laugh but actually I found PHP a lot more practical than Perl (!) soon later for some years. But PHP actually, from a language's point of view, is a horrible language. I stopped writing any php code.

After maybe 2 years of PHP i started playing with ruby. The decision back then was between python and ruby, and the real difference was a talk matz did back then. He emphasized the thinking aspect, inside an OOP "world". (I already knew how beautiful pure OOP can be as I coded in a LPC mud for a few years too).

What shall i say? I am using ruby since 5 years now. I think I know what kind of language would replace ruby _for me_ (a pure prototyped language that is conceptually easier than ruby, but still very powerful, slightly faster and would include features that are rare to see, like behavioural "code snatching" from other live prototype objects, and cell-oriented prototyping, but i stop now before dreaming)

The thing with Ruby is - I love the elegance. If I compare my ruby code to my perl code, I cant stand the perl code anymore. I dont have that problem with python, and I think it is BAD that perl has a "write once, throw away" attitude as far as maintaining source code is concerned.
This is why I recommend python over perl too, also because python seems to have a lot more momentum than perl. (But of course, I would recommend ruby over python even though python is a tad faster.)

I think new people should choose between python or ruby instead of perl and php. PHP is not a serious contender because outside the www it was never successful, but perl was around FOR SO LONG and the cool ideas these days simply arent done in perl. Perl 6? That reminds me of the HURD jokes.


Hurd will be ready next year ........

(Reply to this)(Parent)(Thread)

Re: why does it need to be more than write-once ;-)?
[info]necaris
2008-04-28 03:47 pm UTC (link)
I think new people should choose between python or ruby instead of perl and php

Couldn't agree more -- as general-purpose languages those two have a huge amount going for them. But as you say, Perl and PHP are good languages in their own right and have their place -- be it tight integration into webpage generation or effortless text-processing -- and I hope you're not saying they're not worth learning?

(Reply to this)(Parent)

Re: why does it need to be more than write-once ;-)?
[info]pozorvlak
2008-04-28 06:51 pm UTC (link)
the cool ideas these days simply arent done in perl.

Are you serious? Have you looked at CPAN recently?

(Reply to this)(Parent)

Re: why does it need to be more than write-once ;-)?
(Anonymous)
2008-04-28 10:29 pm UTC (link)
"how beautiful pure OOP can be"

Jesus, is that a joke? Pure OOP is horrible. Pure anything is horrible because the world isn't pure anything. Programming is about modeling, and you need to be able to pick a modeling style that suits the problem. Adapting the problem to the modeling style is just back asswards.

(Reply to this)(Parent)

Re: why does it need to be more than write-once ;-)?
(Anonymous)
2008-04-28 10:37 pm UTC (link)
The cool things these days simply arent done in perl.

Not all cool things are famous or spectacular.

I do all my scientific data reduction in perl, including heavy duty image processing work (well, OK, I use the PDL language extension in CPAN). A few days ago, I was sitting in a panel discussion and, in five minutes of idleness, wrote a little perl script to retrieve, process, and plot all of the published magnetometer data from the Voyager spacecraft -- just by inspecting a few lines of one of the data files. Cool? Very. Famous? nope.

I've written 100,000 line programs in many languages over the years -- C, FORTRAN, Pascal (yuck!), a monstrosity called "Objective C", and Perl. I don't find Perl to be any harder than anything else to scale big -- it just takes discipline to keep from using bad constructs or breaking object boundaries. C is just as bad, but without all the nifty stuff to help out. C++ is an evilness, I don't know how anyone uses it. Ruby and Python are both quite elegant and fast to learn, but they don't seem to be as expressive as Perl in the long run -- like the difference between French and English. (French is a much smaller, tightly regulated language, so you must work harder to express nuance -- English is a huge, brawling bar-fight of a language, so you must work harder to learn it, but there are bits and pieces to help express whatever subtlety you want...)

If I compare my ruby code to my perl code, I cant stand the perl code anymore.
It sounds like you may have simply grown as a programmer between your perl and ruby experiences... it's possible to write sucky code in any language, yes?

(Reply to this)(Parent)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…