Whitecat
Whitecat

Reputation: 4020

How can I pass information into perl script from web?

I want to call a perl script using java script, and pass a java script variable? This script is already written by someone else. I just want their script on some text then output the results on my website. The problem is the script requires a file as input.

The perl script has the following usage

latexindent.pl [options] [file][.tex]

I want to pass in a text box when I call this script, and return the commands printed to console to my javascript function.

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);
}

Upvotes: 1

Views: 1535

Answers (2)

Whitecat
Whitecat

Reputation: 4020

The comment by Dinesh Patra helped me arrive at the answer: Make a wrapper method to create a file them execute the script with the created file. No changes necessary to be made for the perl script. Here is what I did in the most generic way possible. Explanation beneath.

The Javascript:

    function sendValueOne(textToSend) {
        $.post("path/to/php/Wraper.php", {text: textToSend},
        function (data) {
            $('#id').html(data.result);
        }, "json");
    }

The 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));
}
  1. You want to send text that will be fed into the script to php using javascript.
 $.post("path/to/php/Wraper.php", {text: textToSend},
        function (data) {
            $('#id').html(data.result);
        }, "json");
  1. Create a GUID, a unique identifier for a file so we can delete the file afterwards. I used a function I found online.
$uniqueid  = getGUID(); 
  1. Create a file using the guid.
$file = fopen("$uniqueid.tex", "w") or die("Unable to open file!");
fwrite($file, $value);
fclose($file);
  1. Execute the perl script and save the output into a variable
$result = shell_exec("perl perl path/to/perl/script.pl $uniqueid.tex");
  1. Now delete the file and json encode the output and return it to the java script.
//Delete the file
unlink("$uniqueid.tex");

//Return result
$response = new STDclass();
$response->result = "$result";
echo json_encode($response);
  1. Finally we have the result back to our website and can do what ever we want with it.
  $.post("path/to/php/Wraper.php", {text: textToSend},
      function (data) {
          $('#id').html(data.result); //Do something with result.
      }, "json");

Upvotes: 0

Peter Scott
Peter Scott

Reputation: 1316

You mentioned CGI as a tag so I will assume that you aren't using node.js in which case you would look at somthing along the lines of

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
});

So assuming that you wish to run perl on the server as a CGI executed by the web server then as Dinesh pointed out you have 2 options.

Options 1:

edit the file to act as a CGI and make it accessible through the web server by configuring your CGI handlers etc.

basically you will need to: include CGI.pm, extract the posted parameters and use these in place of the file contents. Something along these lines should get you started.

#!/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.

Open 2: create a perl CGI or configure your web server to handle to request and execute your existing script.

use similar code but execute using system(), exec or `` approaches. Be sure to read up on security issues and differences between the approaches of these calls with regard to capturing output and possibility of injection security issues.

Upvotes: 1

Related Questions