开发者

node.js call a perl script and get stdout

开发者 https://www.devze.com 2023-03-19 15:14 出处:网络
Is it possible to use node.js to call a perl script as a process and read back stdout line by line? 开发者_JS百科

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

开发者_JS百科

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.


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);
})


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消