如何将信息从网络传递到 perl 脚本?
How can I pass information into perl script from web?
我想使用 java 脚本调用 perl script,并传递一个 java 脚本变量?此脚本已由其他人编写。我只是想要他们的脚本在一些文本上然后在我的网站上输出结果。问题是脚本需要一个文件作为输入。
perl脚本有如下用法
latexindent.pl [options] [file][.tex]
我想在调用此脚本时传入一个文本框,并且 return 打印命令以控制我的 java 脚本函数。
function ajax_info() {
$.ajax({
url: "latexindent.pl",
cache: false,
dataType: "text",
data: { mode: 'This is Some text!' },
success: function(result) { ajax_info_result(result); }
});
}
function ajax_info_result(result) {
var text = "The server says: <b>" + result + "</b>";
$('#info').html(text);
}
你提到 CGI 作为一个标签,所以我假设你没有使用 node.js,在这种情况下你会按照
的方式查看一些东西
var exec = require('child_process').exec;
var cmd = 'prince -v builds/pdf/book.html -o builds/pdf/book.pdf';
exec(cmd, function(error, stdout, stderr) {
// command output is in stdout
});
因此,假设您希望 运行 服务器上的 perl 作为 Web 服务器执行的 CGI,那么正如 Dinesh 指出的那样,您有 2 个选择。
选项 1:
编辑文件以作为 CGI,并通过配置 CGI 处理程序等使其可通过 Web 服务器访问。
基本上你需要:
include CGI.pm,提取发布的参数并使用它们代替文件内容。按照这些思路应该可以帮助您入门。
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use JSON;
my $cgi = CGI::Carp->new();
my $data = from_json( $cgi->param('mode') ); ## prob need to tweak
## now continue with your processing using $data instead of the file content.
打开 2:
创建一个 perl CGI 或配置您的网络服务器来处理请求并执行您现有的脚本。
使用类似的代码,但使用 system()、exec 或 `` 方法执行。请务必阅读安全问题以及这些调用方法在捕获输出和注入安全问题的可能性方面的差异。
Dinesh Patra 的评论帮我找到了答案:
创建一个包装器方法来创建一个文件,它们使用创建的文件执行脚本。无需对 perl 脚本进行任何更改。
这是我以最通用的方式所做的。下面的解释。
Javascript:
function sendValueOne(textToSend) {
$.post("path/to/php/Wraper.php", {text: textToSend},
function (data) {
$('#id').html(data.result);
}, "json");
}
PHP:
$value = $_POST['text']; //Get text from post
$uniqueid = getGUID(); //Create Guid (unique identifier)
//Write file to server.
$file = fopen("$uniqueid.tex", "w") or die("Unable to open file!");
fwrite($file, $value);
fclose($file);
//Execute the script passing the file
$result = shell_exec("perl perl path/to/perl/script.pl $uniqueid.tex");
//Delete the file
unlink("$uniqueid.tex");
//Return result
$response = new STDclass();
$response->result = "$result";
echo json_encode($response);
//Function found online to create guid
function getGUID(){
if (function_exists('com_create_guid') === true){
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
- 您想使用 javascript 将要输入脚本的文本发送到 php。
$.post("path/to/php/Wraper.php", {text: textToSend},
function (data) {
$('#id').html(data.result);
}, "json");
- 创建一个 GUID,一个文件的唯一标识符,以便我们以后可以删除该文件。我使用了一个我发现的函数 online.
$uniqueid = getGUID();
- 使用 guid 创建文件。
$file = fopen("$uniqueid.tex", "w") or die("Unable to open file!");
fwrite($file, $value);
fclose($file);
- 执行 perl 脚本并将输出保存到变量中
$result = shell_exec("perl perl path/to/perl/script.pl $uniqueid.tex");
- 现在删除文件并对输出进行 json 编码并将其 return 到 java 脚本。
//Delete the file
unlink("$uniqueid.tex");
//Return result
$response = new STDclass();
$response->result = "$result";
echo json_encode($response);
- 最后,我们将结果返回到我们的网站,并可以用它做任何我们想做的事。
$.post("path/to/php/Wraper.php", {text: textToSend},
function (data) {
$('#id').html(data.result); //Do something with result.
}, "json");
我想使用 java 脚本调用 perl script,并传递一个 java 脚本变量?此脚本已由其他人编写。我只是想要他们的脚本在一些文本上然后在我的网站上输出结果。问题是脚本需要一个文件作为输入。
perl脚本有如下用法
latexindent.pl [options] [file][.tex]
我想在调用此脚本时传入一个文本框,并且 return 打印命令以控制我的 java 脚本函数。
function ajax_info() {
$.ajax({
url: "latexindent.pl",
cache: false,
dataType: "text",
data: { mode: 'This is Some text!' },
success: function(result) { ajax_info_result(result); }
});
}
function ajax_info_result(result) {
var text = "The server says: <b>" + result + "</b>";
$('#info').html(text);
}
你提到 CGI 作为一个标签,所以我假设你没有使用 node.js,在这种情况下你会按照
的方式查看一些东西var exec = require('child_process').exec;
var cmd = 'prince -v builds/pdf/book.html -o builds/pdf/book.pdf';
exec(cmd, function(error, stdout, stderr) {
// command output is in stdout
});
因此,假设您希望 运行 服务器上的 perl 作为 Web 服务器执行的 CGI,那么正如 Dinesh 指出的那样,您有 2 个选择。
选项 1:
编辑文件以作为 CGI,并通过配置 CGI 处理程序等使其可通过 Web 服务器访问。
基本上你需要: include CGI.pm,提取发布的参数并使用它们代替文件内容。按照这些思路应该可以帮助您入门。
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use JSON;
my $cgi = CGI::Carp->new();
my $data = from_json( $cgi->param('mode') ); ## prob need to tweak
## now continue with your processing using $data instead of the file content.
打开 2: 创建一个 perl CGI 或配置您的网络服务器来处理请求并执行您现有的脚本。
使用类似的代码,但使用 system()、exec 或 `` 方法执行。请务必阅读安全问题以及这些调用方法在捕获输出和注入安全问题的可能性方面的差异。
Dinesh Patra 的评论帮我找到了答案: 创建一个包装器方法来创建一个文件,它们使用创建的文件执行脚本。无需对 perl 脚本进行任何更改。 这是我以最通用的方式所做的。下面的解释。
Javascript:
function sendValueOne(textToSend) {
$.post("path/to/php/Wraper.php", {text: textToSend},
function (data) {
$('#id').html(data.result);
}, "json");
}
PHP:
$value = $_POST['text']; //Get text from post
$uniqueid = getGUID(); //Create Guid (unique identifier)
//Write file to server.
$file = fopen("$uniqueid.tex", "w") or die("Unable to open file!");
fwrite($file, $value);
fclose($file);
//Execute the script passing the file
$result = shell_exec("perl perl path/to/perl/script.pl $uniqueid.tex");
//Delete the file
unlink("$uniqueid.tex");
//Return result
$response = new STDclass();
$response->result = "$result";
echo json_encode($response);
//Function found online to create guid
function getGUID(){
if (function_exists('com_create_guid') === true){
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
- 您想使用 javascript 将要输入脚本的文本发送到 php。
$.post("path/to/php/Wraper.php", {text: textToSend},
function (data) {
$('#id').html(data.result);
}, "json");
- 创建一个 GUID,一个文件的唯一标识符,以便我们以后可以删除该文件。我使用了一个我发现的函数 online.
$uniqueid = getGUID();
- 使用 guid 创建文件。
$file = fopen("$uniqueid.tex", "w") or die("Unable to open file!");
fwrite($file, $value);
fclose($file);
- 执行 perl 脚本并将输出保存到变量中
$result = shell_exec("perl perl path/to/perl/script.pl $uniqueid.tex");
- 现在删除文件并对输出进行 json 编码并将其 return 到 java 脚本。
//Delete the file
unlink("$uniqueid.tex");
//Return result
$response = new STDclass();
$response->result = "$result";
echo json_encode($response);
- 最后,我们将结果返回到我们的网站,并可以用它做任何我们想做的事。
$.post("path/to/php/Wraper.php", {text: textToSend},
function (data) {
$('#id').html(data.result); //Do something with result.
}, "json");