[英]How can I pass information into perl script from web?
我想使用Java腳本調用Perl 腳本 ,並傳遞Java腳本變量? 該腳本已經由其他人編寫。 我只希望他們的腳本顯示某些文本,然后將結果輸出到我的網站上。 問題在於腳本需要文件作為輸入。
perl腳本具有以下用法
latexindent.pl [options] [file][.tex]
我想在調用此腳本時傳遞一個文本框,然后將打印到控制台的命令返回給我的javascript函數。
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服務器進行訪問。
基本上,您將需要:包括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或配置您的Web服務器以處理請求並執行您現有的腳本。
使用類似的代碼,但使用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));
}
$.post("path/to/php/Wraper.php", {text: textToSend},
function (data) {
$('#id').html(data.result);
}, "json");
$uniqueid = getGUID();
$file = fopen("$uniqueid.tex", "w") or die("Unable to open file!");
fwrite($file, $value);
fclose($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);
$.post("path/to/php/Wraper.php", {text: textToSend},
function (data) {
$('#id').html(data.result); //Do something with result.
}, "json");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.