Archive for September, 2006

How to run a command and save the output, in Perl

Wednesday, September 20th, 2006

Assuming we are in a directory that contains a file called

myfile.

>perl -e “open(FOO,’cat myfile |’)|| die ‘cat failed’; while (){ print }”

Now I can treat FOO just like I would any other filehandle.

Neat little example of a useful JavaScript closure

Monday, September 11th, 2006

I just picked up the 5th edition of David Flanagan’s JavaScript, the Definitive Guide. I find it amusing that that book is the only JavaScript book officially recommended at comp.lang.javascript.

Closures are a powerful feature of JavaScript. However it’s often hard to explain in a few words, just what is useful about closures. Well, here on page 131 of the Rhino book, is the recipe for a unique ID generator that doesn’t require a global counter.

One thing that I personally enjoy about the Flanagan book is that he says things like “don’t pollute the global namespace.” :) This technique does not pollute the global namespace.

var uid = (
  function(){
    var id=0;
    return function(){
      return id++ ;
    };
  }
)();
//then just say:
alert(uid());

var uid = (function(){var id=0; return function(){return id++ ;};})();

The Simplest Example of Recursion in JavaScript

Friday, September 8th, 2006

Here is the simplest example of recursion that I can think of:

(function (arr, count) {
alert[count];
if(count

Why is this important?
(more…)