Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js call a perl script and get stdout

Is it possible to use node.js to call a perl script as a process and read back stdout line by line?

I pretty sure with normal javascript this is usually not possible but a server side script using node.js it would seem to make some sense.

like image 307
evolution Avatar asked Jul 11 '11 14:07

evolution


2 Answers

You could use node's built-in spawn command for child process execution, and carrier to handle line-by-line processing of stdout:

Install:

$ npm install carrier

Code:

var util    = require('util'),
    spawn   = require('child_process').spawn,
    carrier = require('carrier'),
    pl_proc = spawn('perl', ['script.pl']),
    my_carrier;

my_carrier = carrier.carry(pl_proc.stdout);

my_carrier.on('line', function(line) {
  // Do stuff...
  console.log('line: ' + line);
})
like image 88
namuol Avatar answered Nov 09 '22 19:11

namuol


Yes, look into spawn/exec.

http://nodejs.org/docs/v0.4.8/api/child_processes.html

var exec = require('child_process').exec;
exec("perl someperl.pl", function(err, stdout, stderr) {
    /* do something */
});

I am not sure why you wouldn't just do it in node.

like image 34
Justin Thomas Avatar answered Nov 09 '22 20:11

Justin Thomas