Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 5th, 2012, 10:35 PM
Cupidvogel Cupidvogel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 62 Cupidvogel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 38 m 22 sec
Reputation Power: 2
How to return a JSON-encoded array from a Perl script to HTML page?

Hi, I am working with WAMP in my C drive, and I tested a few Perl files from the cgi-bin folder, it works. However, if I keep a HTML file in the www folder, and on clicking a button in it, I want it to invoke a Perl script through Ajax that should return a JSON-encoded array. It is very easy to do so in PHP, can anybody tell me how to do it in Perl?

Reply With Quote
  #2  
Old June 7th, 2012, 08:19 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2004
Location: Reno, NV
Posts: 4,084 keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 49 m 56 sec
Reputation Power: 1809
Not hard in perl either. Use one of the JSON modules from CPAN.

When I've needed to do this, I usually use CGI::Application to create a simple framework. That way, a single script can respond to many different types of requests. One of the nice things about using that framework, is that it is supported by a lot of plug-ins, such as one to output JSON.

CGI/Application/Plugin/JSON.pm

Sorry I don't have time for a longer example right now. One of the simple JSON modules should do what you need easily.

Reply With Quote
  #3  
Old June 7th, 2012, 08:51 AM
Cupidvogel Cupidvogel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 62 Cupidvogel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 38 m 22 sec
Reputation Power: 2
No, the main problem is how to configure the settings i.e. where to keep the Perl script, whether in the CGI-BIN folder while the HTML file is anywhere, or both together in any folder, or whether any change to be made in the Apache directives. Also do I need to install Perl in Wamp for this purpose, or will it be ok if it is installed in C:\\Perl while Wamp is in C:\\Wamp.

Reply With Quote
  #4  
Old June 7th, 2012, 09:33 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2004
Location: Reno, NV
Posts: 4,084 keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 49 m 56 sec
Reputation Power: 1809
I don't use Windows, so I can't answer any questions about Windows confuration; sorry.

I can say this much: the HTML file can be anywhere than can be reached by the web server. The javascript on the page will make a request to the perl script which must be reachable through a web server. The HTML file and perl server do not need to be in the same directory.

The easiest way to make the perl script accessible through the web server is by placing it in the cgi-bin directory, but that isn't the only way.

In the case of a CGI::Application, the perl script that lives in cgi-bin is very basic. As an example:
Code:
use MyApp;
my $app = MyApp->new();
$app->run();


The MyApp.pm file is loaded from @INC, so it can be anywhere in your filesystem that you have set up to store perl modules.

All the parameter reading, dispatching, etc; takes place from that other perl module.

In that sort of example, the javascript would be making its requests to /cgi-bin/myapp.cgi, but your perl module stored elsewhere would be processing the data and replying.

---

But a perl file stored in /cgi-bin could do all the processing itself, and reply directly.

Reply With Quote
  #5  
Old June 7th, 2012, 10:09 AM
Cupidvogel Cupidvogel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 62 Cupidvogel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 38 m 22 sec
Reputation Power: 2
Can you please provide a basic example where a Perl script is invoked via Ajax, and the script returns an integer value, which is received by Javascript, and subsequently printed out in HTML page?

Reply With Quote
  #6  
Old June 7th, 2012, 11:49 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2004
Location: Reno, NV
Posts: 4,084 keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 49 m 56 sec
Reputation Power: 1809
I can after work, if it can wait a few hours.

Reply With Quote
  #7  
Old June 7th, 2012, 12:07 PM
Cupidvogel Cupidvogel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 62 Cupidvogel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 38 m 22 sec
Reputation Power: 2
Of course it can. Take your time. Thanks.

Reply With Quote
  #8  
Old June 7th, 2012, 08:44 PM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2004
Location: Reno, NV
Posts: 4,084 keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 49 m 56 sec
Reputation Power: 1809
OK, we'll just use the simple JSON.pm module rather than CGI::Application; but I've installed JSON::XS as well, which the JSON module will use to speed things up when available.

I took my example from a JQuery website tutorial. I love JQuery. Hope the example is suitable for your purposes.

So the basic HTML page is so:

HTML Code:
Original - HTML Code
    <!DOCTYPE html> <html>     <head>         <title>AJAX Test</title>                      <script type="text/javascript" src="/js/jquery.js"></script>                  <script type="text/javascript">                                                      $(document).ready(function() {                // generate markup                $("#rating").append("Please rate: ");                for ( var i = 1; i <= 5; i++ ) {                  $("#rating").append("<a href='#'>" + i + "</a> ");                }                // add markup to container and apply click handlers to anchors                $("#rating a").click(function(e) {                  // stop normal link click                  e.preventDefault();                  // send request                  $.post("/cgi-bin/rate.cgi", {rating: $(this).html()}, function(data) {                    // format and output result                    $("#rating").html(                      "Thanks for rating, current average: " +                      data.average + ", number of votes: " + data.count                    );                  });                });              });         </script>                                                                    </head>                                                                      <body>                                                                          <h3>Test Page</h3>         <div id="rating"></div>     </body>                                                                  </html>

The page loads the base jquery library, and has the script from the tutorial embedded. The javascript alters the div to add some rating tags, and when a rating is clicked I've told it to post the rating to my perl script: '/cgi-bin/rate.cgi'

My perl script:

perl Code:
Original - perl Code
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. use CGI qw/:standard/;
  6. use JSON;
  7. use Storable;
  8. use List::Util qw(sum);
  9.  
  10. # we'll store user rating clicks in the /tmp directory
  11. my $store = '/tmp/ratings';
  12.  
  13. my $q = CGI->new;
  14. print $q->header('application/json');
  15.  
  16. # get any earlier ratings
  17. my $ratings = [];
  18. $ratings = retrieve($store) if -e $store;
  19.  
  20. # get the new user rating from the javascript
  21. my $rating = $q->param('rating');
  22. exit unless $rating;
  23.  
  24. # add it to our list
  25. push @$ratings, $rating;
  26. store($ratings, $store);
  27.  
  28. my $sum = sum(@$ratings);
  29. my $count = scalar @$ratings;
  30. my $average = sprintf("%.1f", ($sum / $count));
  31.  
  32. #put the data we want to send back into a hash reference
  33. my $json->{'average'} = $average;
  34. $json->{'count'} = $count;
  35.  
  36. # convert and output
  37. print to_json($json);


The only non-standard module is JSON, so you'll have to install that. The other modules are in perl core, so you should have them. I used Storable.pm in lieu of a database, or some other file storage. I'm just storing an array reference called $ratings in the /tmp directory (if you're on a *nix, it's a handy place to store junk that will persist until reboot).
Comments on this post
Winters agrees!

Reply With Quote
  #9  
Old June 8th, 2012, 12:20 AM
Cupidvogel Cupidvogel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 62 Cupidvogel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 38 m 22 sec
Reputation Power: 2
Thanks. Will try it and let you know the results. Two points however, will this work if Perl is not installed in the same location as Wamp? And secondly, you are appending the contents of the links inside the div after page load, so a click event won't work on those links, cause they were not present at page-load. You will have to attach the click event either through delegate or live. And yes, I love jQuery!

Reply With Quote
  #10  
Old June 8th, 2012, 07:31 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2004
Location: Reno, NV
Posts: 4,084 keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 49 m 56 sec
Reputation Power: 1809
Quote:
will this work if Perl is not installed in the same location as Wamp?


What do you mean by "same location"? Same directory? Same computer?

When Apache gets a request to the cgi-bin directory, it will execute that code through the system. The system gives the results back to Apache, and they are sent on to the client. So the computer that Apache is loaded on needs to know how to execute a perl file. Apache and perl don't have to be in the same directory, but perl has to be on the system, and the computer should know to associate the file with the perl interpreter.

Quote:
you are appending the contents of the links inside the div after page load, so a click event won't work on those links, cause they were not present at page-load. You will have to attach the click event either through delegate or live.


Gosh, why is my example working for me? Am I misunderstanding how you need it to work?

The jQuery script came straight from their tutorial page at the link I provided. I wasn't feeling very creative so I just took their code and made it work with a perl back-end, because your question was about how to get perl to provide a JSON response. So, there's one way. I don't mind working up a different example if you have a specific need.

If your question is about how to configure Apache on Windows to serve perl files through CGI, you should start a new thread, maybe in the Apache forum rather than perl. I don't use Windows, so I can't say.

Reply With Quote
  #11  
Old June 8th, 2012, 08:11 AM
Cupidvogel Cupidvogel is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 62 Cupidvogel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 38 m 22 sec
Reputation Power: 2
Yeah Wamp knows that Perl is installed in the computer, for the Perl files located there show the ActivePerl icon. And yes, I really don't know how your code is working! Will try it and let you know whether this works!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > How to return a JSON-encoded array from a Perl script to HTML page?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap