Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将信息从web传递到perl脚本中?_Javascript_Perl_Shell_Cgi - Fatal编程技术网

Javascript 如何将信息从web传递到perl脚本中?

Javascript 如何将信息从web传递到perl脚本中?,javascript,perl,shell,cgi,Javascript,Perl,Shell,Cgi,我想使用java脚本调用perl,并传递java脚本变量?此脚本已由其他人编写。我只想他们的脚本上的一些文字,然后输出结果在我的网站上。问题是脚本需要一个文件作为输入 perl脚本有以下用法 latexindent.pl [options] [file][.tex] 我想在调用此脚本时传入一个文本框,并将打印到控制台的命令返回到我的javascript函数 function ajax_info() { $.ajax({ url: "latexindent.p

我想使用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);
}
函数ajax\u info(){
$.ajax({
url:“latexindent.pl”,
cache:false,
数据类型:“文本”,
数据:{mode:'这是一些文本!'},
成功:函数(结果){ajax_info_result(结果);}
});
}
函数ajax\u info\u result(结果){
var text=“服务器说:“+result+”;
$('#info').html(文本);
}

您提到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
});
因此,假设您希望在服务器上以由web服务器执行的CGI的形式运行perl,那么正如Dinesh指出的,您有两个选项

备选案文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));
}
  • 您希望使用javascript将输入脚本的文本发送到php
  • 创建一个GUID,一个文件的唯一标识符,以便我们可以在以后删除该文件。我使用了我找到的一个函数
  • $uniqueid=getGUID();
    
  • 使用guid创建文件
  • $file=fopen($uniqueid.tex”,“w”)或die(“无法打开文件!”);
    fwrite($file,$value);
    fclose($文件);
    
  • 执行perl脚本并将输出保存到变量中
  • $result=shell_exec(“perl-perl-path/to/perl/script.pl$uniqueid.tex”);
    
  • 现在删除文件并对输出进行json编码,并将其返回到java脚本
  • //删除该文件
    取消链接(“$uniqueid.tex”);
    //返回结果
    $response=新STDclass();
    $response->result=“$result”;
    echo json_编码($response);
    
  • 最后,我们将结果返回到我们的网站,并可以用它做任何我们想做的事情

  • 为什么要投反对票?还有什么我可以改进这个问题吗?首先让我澄清一下我的理解:在服务器中有一个名为“latexindent.pl”的文件,它接受一个文本文件作为输入。正确的??由于这个perl脚本需要一个文件作为输入,所以不能直接将ajax请求传递给它。现在你有两个选项,很简单,要么编辑“latexindent.pl”接受post请求,要么生成另一个脚本,将ajax请求内容存储在文本文件中,然后通过传递文本文件执行“latexindent.pl”。@dinespatra你是对的,我在服务器上有该文件。我只是不知道如何制作一个文本文件,然后执行它。那也安全吗?
     $.post("path/to/php/Wraper.php", {text: textToSend},
            function (data) {
                $('#id').html(data.result);
            }, "json");
    
      $.post("path/to/php/Wraper.php", {text: textToSend},
          function (data) {
              $('#id').html(data.result); //Do something with result.
          }, "json");