0

Hey Guys. I cant actually think of a way to do this. Let me rephrase my question. I essentially want a perl script to run based on something that i am doing in javascript. for example:

<javascript>
if(x == 1)
{
then run the perl script.
}
</javascript>
i dont even want perl to return anything back to the JS or anything of that nature. I just want it to simply run because im making it do a bunch of other things. I hope my question is clear. Any help would be greatly appreciated. Thanks.

4
Contributors
17
Replies
27
Views
7 Years
Discussion Span
Last Post by Airshow
Featured Replies
  • Hey Guys. So something happened and everything just works now. I didnt touch it and it miraculously started working lol. Anyway thanks a lot for your help and support everyone. Im gonna post all the procedures i went through to make everything work(although im not sure which part actually made … Read More

0

Hey Guys. I cant actually think of a way to do this. Let me rephrase my question. I essentially want a perl script to run based on something that i am doing in javascript. for example:

<javascript>
if(x == 1)
{
then run the perl script.
}
</javascript>
i dont even want perl to return anything back to the JS or anything of that nature. I just want it to simply run because im making it do a bunch of other things. I hope my question is clear. Any help would be greatly appreciated. Thanks.

Hi,

a Perl script can be invoked using HTTP request. You need to know how to invoke it. Do you want to use a GET? a POST? are you sending more extra data?

You can use some libraries to ease your call (prototype, jquery, etc.)


i.e. Using jQuery:

<script>
$.get("yourperlscript.pl") // if it's a GET

//or

$.post("yourperlscript.pl") // if it's a post.

// if you want to send more data you can use a second parameter like this:

$.post("yourperlscript.pl", {"greet":"hello"})

</script>


HTH

.::AleX::.

0

Hi,

a Perl script can be invoked using HTTP request. You need to know how to invoke it. Do you want to use a GET? a POST? are you sending more extra data?

You can use some libraries to ease your call (prototype, jquery, etc.)


i.e. Using jQuery:

<script>
$.get("yourperlscript.pl") // if it's a GET

//or

$.post("yourperlscript.pl") // if it's a post.

// if you want to send more data you can use a second parameter like this:

$.post("yourperlscript.pl", {"greet":"hello"})

</script>


HTH

.::AleX::.

Hey Alex. I prefer post method because then the data will be hidden from the user. But I dont understand what exactly you are trying to say. how exactly would get or post invoke the perl script?

0

perl scripts are always invoked as a result of some request, until then they just sit on the server doing 0 nada nothing
traditionally perl script gets data posted or getted to it from a html form, runs on the server and returns a html page to say its done.
the code given, sends the data to the perl script by post or get and ajax similarly could/would handle any returned data

Edited by almostbob: n/a

0

thats my question. I am so confused how to "send" a request to perl to run it self.

0

I'm not a perl person but I guess the various standard ways to make an http request apply:

  1. Traditional, old-fashioed hyperlink - eg. <a href="mycode.perl">
  2. Form submission - eg. <form method="get" action="mycode.perl">
  3. Scripted http request - eg. location.href = 'mycode.perl';
  4. AJAX - eg.:
    xmlhttp = //etc.
    xmlhttp.open("GET", 'mycode.perl', true);
    xmlhttp.send(null);

Anything else would just be a variation on one or other of these.

Airshow

Edited by Airshow: n/a

0

yep. I came to the exact same conclusion. take a look at this test file.

<html>
<head>
<script type="text/javascript">
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
document.getElementById('test').innerHTML=xmlhttp.responseText;
}
</script>
</head>

<body>

<div id="test">
<h2>Click to let AJAX change this text</h2>
</div>
<button type="button" onclick="loadXMLDoc('firstscript.pl')">Click Me for perl</button>

</body>
</html>


now when i push the button, the function simply returns the data of the perl file on the browser instead of actually executing it. i am not sure how to make it execute.

0

BTW just a heads up. that example was directly from w3schools.com. Here is a link to the example page.
http://www.w3schools.com/ajax/ajax_browsers.asp

so they use text files to show their example. I simply replaced that with my perl file and was checking if that works. but it ends up just printing the content of the perl file. i want it to execute :).

0

Also, this exact script works with php. i create a test php file and replaced the firstscript.pl with test.php in the ajax code. i had a simple echo statement in the php file. and it executed it. but perl doesnt execute the code. the ajax function prints the contents of the perl file :(. i dont know what to do.

0

Two things, I expect:

The main problem appears to be the server configuration. It appears not to be set up to execute PERL and/or not returning the response correctly (mime type). This may be something you can fix yourself via a server control panel or you may need to ask for admin support.

Your "AJAX" is written to perform a synchronous request which is not universally supported in all browsers. You need to find/modify an asynchronous example (true AJAX), with a response handler xmlhttp.onreadystatechange = function() {.....} and xmlhttp.open("GET", url, true); .

Airshow

0

Hey Alex. I prefer post method because then the data will be hidden from the user. But I dont understand what exactly you are trying to say. how exactly would get or post invoke the perl script?

a perl scrip is normally exposed as a CGI. so it's "visible" from your browser like in:

http://your.server/cgi-bin/script.pl


you can try at first invoking manually the url of your perl script, if you succeed then you can invoke that using ajax.

as you want to use a post the code usoing jquery can be something like this.


function callPerlScript(){
var perlURL = "http://your.server/cgi-bin/script.pl";
$.post(perlURL)
}

now you can call the function from a button, a link, or attach it to an event.

HTH

.::AleX::.

0

Hey. The server is local. I am running everything on my machine. I tested the perl script and it runs on the browser. 2 things i must confirm about its installation. I think that could be the problem(because php runs just fine with that ajax function). First is, does the perl file it self need to be in a folder called cgi-bin inside of /var/www? . mine is in www folder where all the other files are. even if i make a cgi-bin folder inside of www and put the perl file in there, the perl script doesnt run even with the correct address. i put the following lines inside of my apache2.conf for perl.

Alias /perl/ /var/www/

PerlModule ModPerl::Registry

<Location /perl/>
allow from all
SetHandler perl-script
AddHandler cgi-script .cgi .pl
PerlHandler ModPerl::Registry
#PerlHandler ModPerl::PerlRun
Options +ExecCGI
#PerlSendHeader On
</Location>

currently, this is how i pull up my perl script on the browser. Another thing is, I need to use a ajax function to call the perl script.

localhost/firstscript.pl

if this reveals any problems, please let me know :P

0

Thing is, everything is working through command line. I figured that the cgi-bin folder is actually in usr/lib and thats where the perl file needs to be(or atleast thats where people usually put it). so its in there, and i can pull it up directly using localhost/cgi-bin/firstscript.pl-->this runs the code. the ajax function runs the php code, but not the perl code(I am just restating my problem ;). All of my TA's are a**hole racist people(seems like they dont like Indians), and refuse to help me. You guys are all I have :). I have been at this for 2 days now and I still cant figure out why the ajax function wont run the script. Thanks for being patient guys. Really appreciate it.

0

Hi Uber,

Sorry to hear your probelms extend beyond the code.

Have you read this article? It seems to shed some light. I found it by Googling "calling perl from PHP" which yelded this and several other promising results.

I'm afriad I am at the edge of my knowledge here so not a great position to help more. It appears you are in good hands with Alex.

Good luck (with code and TAs)

Airshow

0

Hi Uber,

Sorry to hear your probelms extend beyond the code.

Have you read this article? It seems to shed some light. I found it by Googling "calling perl from PHP" which yelded this and several other promising results.

I'm afriad I am at the edge of my knowledge here so not a great position to help more. It appears you are in good hands with Alex.

Good luck (with code and TAs)

Airshow

Your script works fine when you call http://localhost/firstscript.pl?

if so then you are ready to go. I'm not httpd.conf apache expert but your configuration seems fine.

Then, if your url works from your browsers then it will work from an ajax call as I mentioned before like the code below. Consider that I suppose that your script returns some value (Hello world!) that will be inserted into the <div id="response>:

<html>
<head>
</head>
<body>
<input type="button" value="Call Perl Script" onclick="callScript()"  />

<div id="response"></div>

<!-- you should download jquery and store it in the same folder  -->
<script src="jquery.js" type="text/javascript"></script> 
<script>
function callScript(){
   $.post("http://localhost/firstscript.pl", function(data){
          $("#response").html("The Perl script says: " + data); 
   })
}
</script>

</body>
</html>
1

Hey Guys. So something happened and everything just works now. I didnt touch it and it miraculously started working lol. Anyway thanks a lot for your help and support everyone. Im gonna post all the procedures i went through to make everything work(although im not sure which part actually made it work eventually) just in case anyone in the future looks at this post.

1) Install Apache2 - better from synaptic package manager because it auto configures a lot of things for you. alternatively you can run the following command - sudo apt-get install apache2

2)Install Perl - perl is actually installed on linux by default. to check version you can type perl -v. if nothing is returned then you might want to install perl from synaptic package manager as well. Even after you install it from there, run this command, just in case you forgot to install the mod.
apt-get install libapache2-mod-perl2

3)edit apache2.conf file by doing this - sudo gedit /etc/apache2/apache2.conf

put the following lines in the file

Alias /perl/ /var/www/
PerlModule ModPerl::Registry
<Location /perl/>
allow from all
SetHandler perl-script
AddHandler cgi-script .cgi .pl
PerlHandler ModPerl::Registry
#PerlHandler ModPerl::PerlRun
Options +ExecCGI
#PerlSendHeader On
</Location>

4) restart apache2 - sudo /etc/init.d/apache2 restart

5)I am assuming you have a perl script you want to run. So im not gonna go over that. once you have all this done, put your perl script in usr/lib/cgi-bin. once you put it in there you are pretty much set as far as where the file needs to be and the configuration to make it run.

6) now type this - chmod 777 /usr/lib/cgi-bin
what the above command does is sets permissions in your files. it is important you do this, otherwise the perl script will not run. you could give it a 755, which is bare minimum permissions(only read privileges), but i just gave it a 777 because i didnt wanna bother :).

once this is done then you should be all set to run the script in the browser using the ajax function(or just the script it self..whatever).

if you want to simply look at it and see how it runs, just type in the link for it, which is:

localhost/cgi-bin/YourScriptName.pl

Enjoy.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.