Friday, September 7, 2007

Installing JavaScript.pm on OS X

While researching an idea for an HTML form building module, I wanted a way to embed a JavaScript interpreter inside a Perl program. The plan is to execute the same JavaScript code on the client and on the server. The only difference would be the context provided by the browser versus the context provided by the CGI script, respectively.

There are two Perl modules on the CPAN that embed a JavaScript interpreter in Perl: JavaScript and JavaScript::SpiderMonkey. JavaScript::SpiderMonkey looked promising but it depends on the behemoth Log::Log4perl and the build arrangement didn't seem as upgradeable as JavaScript's.

Installation was pretty straightforward. Initially I tried a simple cpan JavaScript which didn't work since I had no installation of SpiderMonkey. This incantation did the trick:

sudo port install spidermonkey
export JS_LIB=/opt/local/lib
export JS_INC=/opt/local/include/js:/opt/local/include/nspr
cpan JavaScript
   * answer Y to threadsafe
   * answer N to unicode
   * answer N to E4X

2 comments:

Matt said...

Awesome. I had previously tried to install JavaScript on OS X but didn't have DarwinPorts installed at the time, and couldn't find the spidermonkey dep. Thanks!

mndrix said...

Matt, glad I could help.