简体   繁体   中英

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.

You could use node's built-inspawn 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM