JSPL is a bridge between Perl and JavaScript (the SpiderMonkey engine specifically). JSPL makes it easier for JavaScript programmers to write stand-alone, out-of-the-browser, applications. For Perl programmers JSPL allows you to extend your application using JavaScript. JSPL is open source software. The core of JSPL is a reflection engine and a life cycle synchronizer allowing you to write programs using both languages at the same time.
To start using JSPL see the README file.
We want to use JavaScript as the general purpose language that it is. But, having been born on the client-side web it was lacking a library. We ended up giving it the best 'library' that we know of: CPAN. Using JSPL you can do:
#!/usr/bin/jspl say('Hello World!'); say('Are you ' + Sys.Env.USER + '?'); if(Sys.Argv.length) say('My argv: ' + Sys.Argv());
#!bin/jspl require('POSIX', 'POSIX', -1); // POSIX.read needs an override, can't pass $buf by reference, so: POSIX.read = new PerlSub( 'my $buf; my($fd, $len) = @_; POSIX::read($fd, $buf, $len); $buf' ); if(Argv.length != 1) throw new Error("Usage: "+PrgName+" <file>"); var file = Argv[0]; say(sprintf("Opening: '%s'", file)); var fd = POSIX.open(file, POSIX.O_RDONLY); if(typeof fd == 'undefined') { var err = POSIX.errno(); throw new Error(sprintf("%s (%d)", POSIX.strerror(err), err)); } else { POSIX.lseek(fd, -6, POSIX.SEEK_END) say("The last 5 chars reads: '", POSIX.read(fd, 5), "'"); }
#!/usr/bin/jspl require('Gtk2', 'Gtk2'); install('Gtk2.Window', 'Gtk2::Window'); install('Gtk2.Button', 'Gtk2::Button'); Gtk2.init(); var window = new Gtk2.Window('toplevel'); var button = new Gtk2.Button('Quit'); button.signal_connect('clicked', function() { Gtk2.main_quit() }); window.add(button); window.show_all(); Gtk2.main(); say('Thats all folks!');
#!/usr/bin/jspl require('DBI', 'DBI'); var dbh = DBI.connect('dbi:SQLite:dbname=foo', '', ''); dbh.do("create table colors(name text primary key, feeling text)"); dbh.do("insert into colors values ('green', 'happy')"); dbh.do("insert into colors values ('black', 'sad')"); var all_colors = dbh.selectall_hashref('select * from colors', 'name'); for(var color in all_colors) { say(color + ' is a ' + all_colors[color].feeling + ' color'); }
MatÃas Software Group, 2010