Archive for May, 2010

Emacs Has QWAN

The Preamble

I enjoy programming in Perl. There, I said it. And, although a lot of the stuff I write could easily be written in Emacs Lisp, I generally reach for Perl to solve most of my programming problems. However, if I had to choose between Perl and Emacs disappearing, I’d choose Perl any day of the week and twice on Sunday.

The reason is that there isn’t any competition in the emacs space1. Vim, Visual Studio and Eclipse are not acceptable substitutes. But if I want a dynamically typed langugage with good syntactical support for arrays, hashes and regular expressions and loads of libraries I know where to find Ruby or Python.

Q. Why not switch to Ruby today?
A. I enjoy programming in Perl

What makes Emacs so great?

(or what would I need to switch from Emacs to another editor)

Many people put it as Emacs has QWAN (the quality without a name), but that doesn’t really help to quantify things. Saying that all of the features work very nicely together is true, but also not that helpful. Let me try and point out a few things.

(If the c2 link above isn’t working, try the Bing cache).

Keystrokes are bound to functions

…and it is all very discoverable. If I want to know what a given keystroke does, I just press C-h k <key> and it tells me the function that calls. For example, arrow key calls forward-char. Now, say I want to make a function that needs to move forward a number of characters, I know what the function is called.

The utility of this discoverability in making composite functions cannot be overstated. You might think that macros are equally good, but what if I want a function that persists between sessions. For my line duplication function I glued together functions found with C-h k.

Okay, new utilities, great but not that impressive.

What about fixing stuff with the editor that you just don’t like. For example, recently I had a problem where <page down> went to the beginning of the line rather than remembering the current column position. You might be thinking a proper editor wouldn’t do that, or it isn’t really a problem, but you can’t imagine how annoying that was after the first ten times.

Anyway, what I did was… I fixed it. With C-h k I found that the command was scroll-up and I wrote a wrapper function that remembered the current column (alternatively I could have used advice). Saved my blood pressure and took around 5 minutes.

To summarise:

  • Key strokes / key chords call functions (which can be user-defined)
  • New functions can be created within the editor at any time
  • any key / key chord can call any function
  • information about the function can be viewed

Okay, you’re not convinced. Vim can do that, albeit with a much less pleasant language (or with perl, ruby, python, etc. if you’ve compiled a fat vim)

Editor Tabs

Tabs are not the right mechanism to deal with hundreds of open files. Emacs (together with ido) works beautifully. Generally I’ll have a couple of hundred files open with different file types and I’ll use progressive filtering to first find the perl files and then narrow down the file I wanted. Very quick and very nice. I’ve spent hours scrolling through tabs in Visual Studio and it was not fun.


So all your keys are bound to various useful commands. M-e steps forward to the next sentence nicely. But then you’re editing lisp (or perl) and sentence doesn’t mean much. What does emacs do?

In this case it has a keymap hierarchy. The global keymap defines a bunch of keys which can be overridden by keys defined in the major mode keymap. These can be added to with keys defined in one or more minor mode keymaps.


Comint is an awesome bit of kit. Out of the box, Emacs provides fantastic support for interacting with asynchronous processes. Comint codifies a lot of the functionality you would want to make a decent REPL for an external interpreter. Because this is one of the more tricky interactions to manage, it is an excellent API for dealing with any external process.

A while ago I wrote an article about a comint based stock price subscriber which demonstrates a few comint features.

Take a look at the interaction with gdb following M-x gdb-many-windows. It looks like an IDE no?

Emacs Applications

Some people have developed some impressive applications in emacs.

  1. Org Mode
  2. Emacs Muse
  3. SLIME (A full-featured Lisp IDE)

And my own Emacs Database Mode

Which in turn is based on the excellent sql-mode.

1. Apart from XEmacs of course.

Read Full Post »

Ask and you shall receive it seems. The commenters have arrived with some great comments. And (possibly this is connected), yesterday was my first 200+ day since my 7 month hiatus. I even got some referrals from planet perl due to Gabor mentioning my post on his [aggregated blog].

Foo pointed out that I haven’t got my tone quite right for irony. Any suggestions how to do that better without being too obvious?

And in another comment, Gabor has given me an idea for my next post – which Emacs features I’d need to switch to Padre.

Overall, I’m very pleased.

Read Full Post »

Hang on, that should be Vim vs Emacs right? No.

I have been blogging for the Perl community I have been writing Perl blog posts for around 6 weeks now. Prior to that, I wrote mostly Emacs related blog posts for around 18 months. My vast experience puts me in a great position to compare them.

Emacs blogging

Back when I wrote about Emacs I averaged a couple of posts a week and around 200 visitors a day. Better still, I got several comments for each post, pointing out things I had got wrong, things I could improve or alternative techniques.

200 visitors isn’t many, but it was plenty for me. I was paid for my blogging effort in comment currency.

I suspect the reason I got the comments was that I never had much competition in the emacs hints and tips space. The main ones were:

Of these, only emacs-fu kept grinding out the handy tips, week in week out.

Interestingly, emacsblog reports 3379 readers by feedburner which indicates a decent level of interest in emacs hints and tips.

There was only one real place to pick up emacs news – Planet Emacsen. My posts would hang around for a week or more and I would pick up pretty much all the readers who were interested as well as probably quite a few who weren’t.

Perl blogging

With my Perl blog posts, I struggle to keep 100 visitors a day and I need to post every other day to get that many. Why the difference? I suspect it is a combination of things.

  • If you want to read about perl, you can read the gurus in the community – the Miyagawas, the Tim Bunces, the Curtis Jewels. And there’s hundreds more perl gurus blogging. Okay, maybe 10 more.
  • There are multiple perl news sources. I listed a few here.
  • If you want to improve your perl-fu there is IRC or Perl Monger groups. Emacs in contrast had fewer options.
  • There are more perl bloggers, so your post disappears off the Ironman aggregation pretty quickly.
  • Six weeks may not be enough to build up a following.
  • There are more areas of perl to be interested in. Maybe no-one else wants to know about writing AnyEvent TCP servers.

It could also be that the quality of my writing is poor. But one of you guys would tell me, right?

Why does this matter?

Blogging has to have some value to me, otherwise I might as well watch TV. I do get something out of it even if there are no readers – I’m able to find my fantastic code snippets as long as I have access to the internet. But the lack of input means this doesn’t really offset the effort to write a story around each post. And these days with github et al there are easier and better ways to get your code out there.

Okay, no worries, you’re thinking, if Jared stops blogging about perl (not that I’m thinking of doing so) no-one loses anything. But maybe my experiences is why the level of blogging activity is low compared to the relative size of the community.

And maybe it just doesn’t matter.

Read Full Post »


So I’m perhaps 1% of the way to a poorly thought out middleware component like CORBA1. No, it’s more light-weight, maybe a messaging layer, sorry I mean wire-level protocol specification implementation such as AMQP.

And then I think (like hundreds have probably thought before me), you know, this would be more useful if it had authentication. After all, I don’t want just anyone to be able to send kill signals to any processes. That would be like everyone being root. Which terrifies me.

Don’t invent your own authentication mechanism

And the golden rule about authentication, as far as I can work out, is don’t invent your own authentication mechanism. You’ll get it wrong and leave gaping vulnerabilities for the bad guys to have their wicked way with you. That is, if anyone besides you ever uses your code. And besides, I don’t want to waste any of my 1500 lines on coming up with Yet Another Broken Authentication System.

A quick trip to CPAN

Then I’m looking through the Authen::XXX modules on CPAN and none of them behave in exactly the way I want. But somehow I find a perl server that includes authentication and perhaps does everything I want and I should definitely put it on my list of things to look into even though I’m having a lot of fun with AnyEvent right now.

But by the time I come to look again, I can’t find it. And I’ve complained about documentation before, but Emacs really does deserve it, and I know of no system or language that is better documented than Perl. But I guess the classification problem is a bit tricky to overcome.


Anyway, long story short, I found it.

authenticated perl server -http

An Authenticating Perl Server

The first link (warning PDF) is a paper about using Authen::SASL in client/servers and it mentions ARCv2 which sounds like what I’m looking for.

The first thing to do is find out if it does what I want. The second is to check if it works on Windows.

1. Ambiguity left in deliberately

Read Full Post »

$ date
Sun May  2 09:22:09 GMTDT 2010
$ perl -MPOSIX -le 'print "Week ",
>                   POSIX::strftime("%V", gmtime(time))'
Week 17

One of the differences between summarising emacs news and perl news is that there are many sources of perl news. This makes a good summary more valuable. For emacs news you only need to visit A Better Planet Emacs. For perl, there are hundreds of sources, including:

And many others I have missed.

Eweek Slideshow (Padre)

Eweek has a slideshow with a list of 25 reasons why Perl keeps rising in the enterprise. The interesting points are:

  • it is extensible and flexible, i.e. has good libraries including Catalyst, DBIx::Class and Plack
  • the new release 5.12 has improved unicode and datetime support
  • everyone uses it "used in virtually 100% of the Fortune 500"
  • it is built into all major OSes (apart from Windows)
  • it has good IDE support including Padre – a new Perl IDE and Komodo
  • it is in the top 10 of the TIOBE index (why do people still think that TIOBE is relevant?)

Sam Crawley also talks about Padre so I thought I’d better install it. Conclusion: it is great, but obviously as an emacs fan it isn’t for me. Having said that, if I could customise Padre as easily with perl as I can customise emacs with lisp, I would consider switching.


My own posts have mainly covered AnyEvent.

Other News

Brian kelley decides to stick with Perl over Powershell on windows due to the availability of libraries and examples. (That’s where we beat everyone, no?)

xenoterracide has started a series on teaching perl. Here is the prologue and week 1a and 1b.

Pythian has another perl news roundup.

And finally, one you’ve probably seen on all the news links already. chromatic has a great post on volunteers and civility. It is advice I should often bear in mind myself.

Read Full Post »

I am a student of the design your data structures first school of programming1. My first proper language was C and in that language pretty much all you had was typedef struct and a handful of primitive types.

Despite the fact that I now spend most of my time programming with dynamic languages (emacs lisp and perl), I still believe it is important to think about your types before you start programming. Eh, but what’s that? Dynamic languages don’t have types? Au contraire mon ami. Not having static type-checking is not the same as not having types.

So, enough handwaving, what about a real example?

Extensible Vectors

In C++, one of the data structures I use most is the STL vector. But in emacs lisp, the vector is a very different animal as it doesn’t automatically extend. If I want an extensible vector type (an evector), I have to implement it myself (or find a library containing one, but that wouldn’t make a good example).

So, first of all, I need to know what pieces of information each evector will need. There will be the vector itself, plus the current end of the vector and the current size before we need to resize it. If we want to use fibonacci to get the next size of vector we need another slot for the fibonacci counter. So each evector will look like this:

['evector [...] <end position> <fib counter>]

The constructor is generally called make-<typename> so the implementation (assuming we want a vector that starts with 8 slots) is:

(defun make-evector ()
  (vector 'evector (make-vector 8 nil) 0 5))

We also need a predicate for checking if a variable is of type evector.

(defun evectorp (object)
  (eq (aref object 0) 'evector))

What does the push-back operation look like?

(defun evector-push-back (object elem)
  (let* ((vec (aref object 1))
         (pos (aref object 2))
         (len (length vec)))
    ;; increase the size of the vector if necessary
    (when (<= len pos)
      (let* ((new-size (+ len (aref object 3)))
             (new-vec (make-vector new-size nil))
             (i 0))
        ;; copy the original vector into the new one
        (while (< i len)
          (aset new-vec i (aref vec i))
          (incf i))
        (setq vec new-vec)
        (setf (aref object 1) vec)
        (setf (aref object 3) len)))
    ;; set the vector element and update end position
    (aset vec pos elem)
    (incf (aref object 2))))

Hmmm… referring to the member variables by position looks error prone and inflexible. What can we do about that?

If you’re thinking accessors, you’re on the right lines. Fortunately, because of setf, you only need a getter.

(edit: fixed, thanks Jisang)

(defsubst evector-data (object) (aref object 1))
(defsubst evector-end-position (object aref object 2))
(defsubst evector-fib-var (object aref object 3))


But even better, there is a macro that does defines your constructor, predicate and accessors for you, defstruct.

(defstruct evector
  (data (make-vector 8 nil))
  (end-pos 0)
  (fib-var 5))

Yet another advantage of using defstruct, is that the error messages are clearer than with my basic accessors.

(evector-fib-var 'x)

[basic accessor]
Debugger entered--Lisp error: (wrong-number-of-arguments (lambda (object aref object 3)) 1)

[defstruct accessor]
Debugger entered--Lisp error: (error "evector-fib-var accessing a non-evector")
  signal(error ("evector-fib-var accessing a non-evector"))
  error("evector-fib-var accessing a non-evector")

edit: apologies to those who arrived from ironman perl. This isn’t a perl related post so I hadn’t tagged it as such. I didn’t realise that posts that mention perl anywhere get picked up.

1. Although it might not come across in my examples :)

Read Full Post »

« Newer Posts


Get every new post delivered to your Inbox.