Node.js - Electron - NW.js package for asynchronous handling of Perl scripts
npm install camel-harness
const camelHarness = require("camel-harness");
let perlTest = {};
perlTest.script = "/test/test.pl";
perlTest.stdoutFunction = function (stdout) {
console.log(stdout);
};
camelHarness.startScript(perlTest);
child_process
Perl interpreter identified by filename on PATH or full pathname
camel-harness npm package test will fail if no perl
binary is available on PATH.
All settings of a Perl script executed by camel-harness are stored in a JavaScript object with an arbitrary name and the following object properties:
-
script
String
for Perl script full path or Perl code executed as an one-liner
This object property is mandatory.perlTest.script = "/full/path/to/test.pl";
The
-e
interpreter switch must be set when Perl code is executed in one-liner mode.
Perl code must not be surrounded in single quotes and all double quotes must be escaped.One-liner example:
let oneLiner = {}; oneLiner.interpreterSwitches = []; oneLiner.interpreterSwitches.push("-e"); oneLiner.script = "use English; print \"Perl $PERL_VERSION\";" oneLiner.stdoutFunction = function (stdout) { console.log(`${stdout}`); }; camelHarness.startScript(oneLiner);
-
stdoutFunction
will be executed every time data is available on STDOUT
The only parameter passed to thestdoutFunction
is the STDOUTString
.perlTest.stdoutFunction = function (stdout) { document.getElementById("DOM-element-id").textContent = stdout; };
-
stderrFunction
will be executed every time data is available on STDERR
The only parameter passed to thestderrFunction
is the STDERRString
.perlTest.stderrFunction = function (stderr) { console.log("Perl script STDERR:\n"); console.log(stderr); };
-
errorFunction
will be executed on Perl script error
The only parameter passed to theerrorFunction
is the errorObject
.The
errorFunction
can generate a message when Perl interpreter is not found:perlTest.errorFunction = function (error) { if (error.code === "ENOENT") { console.log("Perl interpreter was not found."); } };
-
exitFunction
will be executed when Perl script has ended
The only parameter passed to theexitFunction
is the exit codeString
.The
exitFunction
can generate a message when Perl script is not found:perlTest.exitFunction = function (exitCode) { if (exitCode === 2) { console.log("Perl script was not found."); } };
-
perlInterpreter
String
for a Perl interpreter: either filename on PATH or full pathname
If noperlInterpreter
is defined,perl
binary on PATH is used, if available.perlTest.interpreter = "/full/path/to/perl";
-
interpreterSwitches
Array
for Perl interpreter switchesperlTest.interpreterSwitches = []; perlTest.interpreterSwitches.push("-W");
-
scriptArguments
Array
for Perl script argumentsperlTest.scriptArguments = []; perlTest.scriptArguments.push("argument-one"); perlTest.scriptArguments.push("argument-two");
-
options
Object
for Perl script options passed to thechild_process
core module.
Click here for a full list of all availablechild_process
options. -
options.cwd
String
for a new Perl script current working directoryperlTest.options = {}; perlTest.options.cwd = "/full/path/to/current-working-directory";;
-
options.env
Object
for a new Perl script environmentScript environment with an inherited PATH and a new variable:
perlTest.options = {}; perlTest.options.env = {}; perlTest.options.env.PATH = process.env.PATH; perlTest.options.env.TEST = "test";
-
options.detached
Boolean
option for starting detached Perl processes like serversoptions.detached
must be set totrue
and
options.stdio
must be set to"ignore"
to
start a detached process without receiving anything from it.
A process detached with the above options can run even after its parent has ended.Example settings for a Perl server application:
let perlServer = {}; perlServer.script = "/path/to/perl-server-application"; perlServer.options = {}; perlServer.options.detached = true; perlServer.options.stdio = "ignore"; const camelHarness = require("camel-harness"); camelHarness.startScript(perlServer); perlServer.scriptHandler.unref();
-
inputData
String
orFunction
supplying user data as its return value.
inputData
is written on script STDIN.inputData
function with no dependencies:perlTest.inputData = function () { let data = document.getElementById("input-box-id").value; return data; }
camel-harness can also start and communicate with interactive scripts having their own event loops and capable of repeatedly receiving STDIN input. Use the following code to send data to an interactive script waiting for input on STDIN:
let data = document.getElementById("interactive-script-input").value;
perlTest.scriptHandler.stdin.write(data);
camel-harness demo packages for Electron and NW.js include a Perl script that can be constantly fed with data from an HTML interface. Perl with the AnyEvent
CPAN module has to be available on PATH.
MIT 2016 - 2018
Dimitar D. Mitov