Re: Call Perl script from Javascript
by Anonymous Monk on Mar 18, 2014 at 22:05 UTC
|
You haven't given very much information, however:
Assuming your server supports Perl scripts the same way it does PHP, "simply" replace the PHP script with a Perl script. In your Perl script, use the CGI module to handle input from the HTML page, or a more modern framework such as Mojolicious.
For database connections, DBI and friends, or something more modern such as DBIx::Class.
For returning things back to JavaScript, you may find a module such as JSON useful.
There are also plenty of tutorials on topics such Perl CGI scripts and database access, Google is your friend.
| [reply] |
Re: Call Perl script from Javascript
by Don Coyote (Hermit) on Mar 18, 2014 at 22:16 UTC
|
Hello Abed, i am imagining a kind of search field dropdown which auto fills as you type. Essentially you just need to direct the XHTMLrequest object to the Perl script. Using a CGI script to query the database with the use of database handles and statement handles and then creating a valid XHMTL object to return, which can then be appropriated by the Javascript into the DOM on receipt.
If you want a more specific answer you will need to provide some code
| [reply] |
|
|
Hi,
This is exactly what I'm doing :)..auto fill field
Currently I have php script that does the db connection and return the data to the Javascript that post it to the html.
My question:
- I'm using the following Javacript function to call php
$.get("suggest.pl", {word: $(this).val()}, function(data){
$("#myWord").empty();
$("#myWord").html(data);
});
gets the values from php (php is doing echo for the returned values from db) then post them to html.
How can I replace this code to be valid to the perl scipt?
Thanks, | [reply] [d/l] |
|
|
This JavaScript should work with tangent's answer below, did you try it?
If you're having trouble with your Perl script feel free to post your code with any errors you are getting.
| [reply] |
|
|
|
|
|
Re: Call Perl script from Javascript
by fattahsafa (Sexton) on Mar 18, 2014 at 22:40 UTC
|
Thanks alot for your responses. Actually Javascript code is ready and it calls the php script:
$(document).ready(function(){
$("#suggest").keyup(function(){
$.get("suggest.php", {word: $(this).val()}, function(data){
$("#myWord").empty();
$("#myWord").html(data);
});
});
});
also I already have a perl script that deals with db..the missing part is that how to get the "word" value in my perl script. | [reply] [d/l] |
|
|
Even with code tags, I am a little fuzzy as to how this gets passed to the script.
The $get() function does not state whether this is a post or get request, in that it is a Javascript function which could be doing anything. Looks like a get request though.
To get a list of paramaters you can go a couple of ways see http://search.cpan.org/~markstos/CGI.pm-3.65/lib/CGI.pm#FETCHING_A_LIST_OF_KEYWORDS_FROM_THE_QUERY:
The JSON object containing the word: string once extracted can then be decoded and manipulated. You will then be able to encode back into a JSON object
However as I cannot tell how your calling function is sending - are the Data functions DOM functions or also parameters? - and will this function happily receive the return value from the Perl script?
Assuming the parameters are passed as a list of value pairs, something like this may get the value
#!/usr/bin/perl -T
use warnings;
use strict;
use CGI;
use JSON;
my $q = CGI->new;
my @pars = $q->params;
my $wordref = decode_json($pars[0]);
my $word = $wordref->{word} ;
#untaint word
die "invalid input" unless $word =~ /^([\w\s]+)$/;
$word = $1;
... # mess about with db and/or word;
$wordref->{word} = $word;
my $jsonobj = encode_json($wordref);
But how does it get back? To be honest, I am not completely sure in this case.
The second parameter passed into the script may be the empty value holder so you could extract that and send that back. But this is really dependant on what that Javascript function is doing | [reply] [d/l] [select] |
|
|
If you replace suggest.php with suggest.pl (or whatever your perl script is called) in the javascript, you should be able to get the value using the CGI module:
use CGI;
my $q = CGI->new;
my $word = $q->param('word');
Update: Looking at Don Coyote's comment below I should clarify that I am assuming you are using jQuery (it certainly looks like that to me) where the $.get() sends a conventional url encoded get request, i.e. not a JSON request.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
Re: Call Perl script from Javascript
by locked_user sundialsvc4 (Abbot) on Mar 19, 2014 at 15:55 UTC
|
If you actually can cost-justify the expense of replacing a PHP script that works with a Perl script that doesn’t exist yet, then the structure of the Perl script is basically going to be the same as whatever the PHP version now reflects. It will be invoked as a CGI or FastCGI script, handed some HTTP inputs, and it must produce an HTTP response after doing some work with the database. Instead of using features that are built-in to PHP, which is by design a fairly monolithic system, your Perl script will use components drawn from CPAN, and you would be well-advised to use the highest-level components that you can manage. (For instance, RPC::Any.) Nearly all of this requirement is “nothing new,” so do not plan to write “new” code for all of that.
If I were managing this project, though, I’d oblige you to answer me, “what’s so wrong with PHP in this case?” If that host-side system is doing the job acceptably well, then PHP, also, is a well-proven system, and “I’m not running a Perl school here.” Consider very carefully whether there will actually be any ROI = Return On Investment here, knowing that I am going to have to put a new line-item for $XX,000.00 on my project budget and to secure approval for that from my boss ... and to sell the idea that replacing a module that is in-service now is “necessary and unavoidable.” (In the rather unlikely event that I am actually persuaded of this, myself.) Consider also just what it is that is now compelling you to replace it, so that you can be extremely sure that your Perl replacement does not merely follow in its footsteps to the same end. Consider also what changes are implied in the client-side JavaScript. If de-stabilizing changes start showing up there, too, as I expect they will, then I would kick-the-can your project back to start and say, “I’m not buying this. (Literally.) Find a way to do this keep doing this in PHP.”
| |
Re: Call Perl script from Javascript
by fattahsafa (Sexton) on Mar 19, 2014 at 21:03 UTC
|
I need to replace http with perl as I need to do further processing on the "word" that I already implemented in perl for desktop app. | [reply] |
|
|
I'm not sure I understand what your set up is and what this "word" desktop app is, it'd help if you could explain some more. Also, do you have a specific question? You've already been given several answers on how to approach the general problem above. As a next step it'd probably be best to write some code, and if you have problems with it post your code with specific questions here. See also How do I post a question effectively? and How (Not) To Ask A Question
| [reply] |
Re: Call Perl script from Javascript
by fattahsafa (Sexton) on Mar 19, 2014 at 20:48 UTC
|
Hi guys,
Thanks alot for your ideas.
This is my php script:
<?php
$username = "root";
$password = "root";
$hostname = "127.0.0.1:3306";
$dbname = "AI";
$word = $_GET['word'];
$sql = "SELECT crps_word, crps_probability FROM arabic_cor
+pus WHERE crps_word like '$word%' ORDER BY crps_probability LIMIT 10
+";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
// Set Encoding Set to utf8
mysql_query("SET character_set_results=utf8", $dbhandle);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbhandle);
mysql_query("set names 'utf8'",$dbhandle);
//select a database to work with
$selected = mysql_select_db($dbname,$dbhandle)
or die("Error selecting database");
//execute the SQL query and return records
$result = mysql_query($sql, $dbhandle);
if($result)
{
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
$word = $row{'crps_word'};
$prob = $row{'crps_probability'};
$str = $prob."----".$word;
echo "<option value = '$word'></option>";
}
}
//close the connection
mysql_close($dbhandle);
?>
| [reply] [d/l] |
|
|
To see what you are getting back from your script add an alert() to your javascript:
$.get("suggest.pl", {word: $(this).val()}, function(data){
alert(data);
$("#myWord").empty();
$("#myWord").html(data);
});
You say earlier that you already have the script that deals with the db, so instead of the echo in PHP you just need print in Perl, something like:
print $q->header;
while (my $row = $sth->fetchrow_hashref) {
my $word = $row->{'crps_word'};
my $prob = $row->{'crps_probability'};
my $str = $prob."----".$word;
print qq|<option value="$word">$str</option>|;
}
| [reply] [d/l] [select] |