Raphael has posted 1 posts at DZone. View Full User Profile

Sleep 2.1: Java Scripting Language

  • submit to reddit

After 2 years of development, Sleep 2.1 is now released. Sleep is a Java-based scripting language heavily inspired by Perl. Features include first-class functions and continuations, a built-in debugger, taint mode security, and access to the Java class library. Sleep embeds into applications, executes scripts stand-alone, and it supports the Java 6 Scripting Framework.

$ java -jar sleep.jar
>> Welcome to the Sleep scripting language
> x iff(unpack('i!', pack('i+', 1))[0] == 1, "big endian", "little endian")
big endian

Sleep can embed into applications, implement mobile agents, extract and parse binary data, and a lot more. More information is available at the Sleep homepage:

Published at DZone with permission of its author, Raphael Mudge.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Mike P(Okidoky) replied on Mon, 2008/06/16 - 11:53pm

I'm seeing some similarities with php.  What are the main advantages over that other Quercus, the Java php implementation?

I'm seeing closures for one.

What about performance, does it perform any kind of compilation to enhance performance?

At first glance, I'm liking this more than Python and Ruby, both of which I *really* do not like. 

Raphael Mudge replied on Tue, 2008/06/17 - 1:53am in response to: Mike P(Okidoky)

Your comment made me take my first look at Caucho's website. I'm very impressed with these guys. I think they are people I'd like to have a beer with. We have very similar thinking about application servers. That doesn't answer your question, does it?

As I'm not read into Quercus, I'll try to highlight some of the core competencies of Sleep and let you make your own judgements:

- Sleep embeds into applications. Sleep's extensible nature allows developers to create a seamless integration between Sleep and their app. In the announcement I link to a recent jroller post that highlights this. The IRC client jIRCii uses Sleep to allow users to define aliases, menus, keyboard shortcuts, respond to events, and redefine output.

- You mentioned closures. Yes, Sleep supports first-class functions. Scripts can pause a sleep closure with the callcc and yield keywords (continuations and coroutines). Paused (or unpaused for that matter) closures are serializable. I've used this feature to distribute tasks as mobile agents that move through a network. Sleep closures are also flexible super-data structures as you can implement your own OO functionality on top of them. I've written about this a few times. One last thing, the syntax for closures is very lightweight. The literal form of a closure is code enclosed within curly braces. Arguments passed to a closure are available as $1, $2, etc. The light weight syntax encourages their use throughout programs. A lot of times I can't stand operating in the OO paradigm when I know many of the objects I would define are better suited for a closure.

- Sleep also sports a built-in profiler and debugger. These are integrated into the language and the information they report delivers the right information about what your scripts are doing. The debugger can report all soft errors (failure to open a file, connect to a host, etc.) with no code required on your part. This is great for writing one-off scripts. The debugger can provide a trace of every single function call with arguments and return value ala strace on UNIX systems.

- Hrm, UNIX, that reminds me of another very important core philosophy. I come from the UNIX world. I'm used to thinking about problems in a UNIX way. Sleep borrows many abstractions (I/O, threading, etc.) from the UNIX world. To an extent, the core Sleep library ignores the Java way and introduces the UNIX way. This turns off some Java developers but offers something of a home for those coming from the UNIX world. Sleep does provide functionality to bridge the UNIX way with the Java way:

- Sleep excels at ripping apart data sources and putting them back together. pack and unpack manipulate binary data. parseDate and formatDate manipulate date-time data. PHP has these things as well. So no comparison there.

- Scripts can access the Java class library. Scripts can import classes from 3rd party jar files at runtime. Ignore my comments from earlier, the Java way is at home in Sleep.

- Finally you asked about performance. Sleep scripts are interpreted. However the parser compiles the scripts to a very high level form. Sleep's highly functional nature allows trivial (and sometimes transparent) implementation of performance enhancement techniques.

- I'm in the camp that performance is achieved through using the right algorithms and data structures in the right places. Sleep's building blocks for data structures are the dictionary (Sleep Hashes) and the linked list (Sleep Arrays). Scripts can mix, match, and nest these data structures to any depth. Manipulation of these data structures is accomplished with a very light weight syntax (again encouraging their use). When I want to iterate through data, I use a list with a foreach loop. When I want to iterate through lists in a non-standard way, without sacrificing performance, I use sublist and recursive function calls. At this point scripts start to look like Lisp programs, so beware. When I need constant time access to data, I use dictionaries. These building blocks let you store your data based on the problem at hand and not syntactic convienence.

One last thing. Awhile back I wrote a script to chop the first few seconds off of several MP3 files in a directory. I purchased an audiobook. It consisted of over 100 chapters ranging from 1-3 minutes in length each with a "this audio presentation is brought to you by ________" at the beginning. Not exactly road trip material. With Sleep I leveraged the javax.sound API and an external program to chop the files down to size. Sleep excels at one-off hacks. (some would say Sleep is a one-off hack)

That about covers it. Sleep isn't a very big language. At the core Sleep is just a Perl inspired language sitting on top of the Java platform. If you like Perl and if you like Java, you'll like Sleep.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.