Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

AJAX and Perl-based web applications

by srdst13 (Pilgrim)
on Jun 25, 2005 at 01:06 UTC ( #469868=perlquestion: print w/replies, xml ) Need Help??

srdst13 has asked for the wisdom of the Perl Monks concerning the following question:

I just stumbled onto this site using Catalyst to create an AJAX-based Wiki. I have been aware of AJAX as a technology, but know very little about it. This question may not even make sense given AJAX reliance on JavaScript, but I'll ask away. How does one go about building an AJAX-based application using standard Perl web tools (such as CGI::Application)? Are folks developing modules to generate such sites (ie., the JavaScript "widgets" and functions to run them)? What goes into incorporating AJAX into a CGI application?

Thanks,
Sean

Replies are listed 'Best First'.
Re: AJAX and Perl-based web applications
by redhotpenguin (Deacon) on Jun 25, 2005 at 02:13 UTC
    Here's how I built my first AJAX widget using Template, Javascript, and CGI::Application. It removes usernames from a list when the user clicks the remove link, without refreshing the page. The hardest part was figuring out how to properly POST using the XmlHttpRequest object. Note that my CGI handler does not return anything in this particular case.

    In the template code:

    <script type="text/javascript"> var xmlhttp=false; /*@cc_on @*/ if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } function myfunction(username) { var params = "rm=remove&username="+username; var objectlist = document.getElementById(objectlist); // rm is the node we want to remove from the DOM var rm = document.getElementById(username); // The true attribute means the script continues after // the post has been executed xmlhttp.open("POST", '/my/runmode/', true); xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-u +rlencoded"); xmlhttp.setRequestHeader("Connection","close"); // Connection is to be closed after transfer xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { // Delete the html node when request is complete rm.parentNode.removeChild(rm); } } xmlhttp.send(params); } </script> <ul id="objectlist"> [% FOREACH object = objects %] <p id="[% object.username %]">[% object.username %] - <a href="#" onclick="myfunction('[% object.username %]');"> remove</a> </p> [% END %] </ul>

    Meanwhile in my CGI::App handler...

    sub setup { my $self = shift; $self->start_mode('default'); $self->run_modes( [ qw( default remove ) ] ); } sub remove { my $self = shift; my $user = My::User->retrieve( username => $self->query->param('username') ); $user->delete; }
Re: AJAX and Perl-based web applications
by etcshadow (Priest) on Jun 25, 2005 at 01:44 UTC
    I've been writing "AJAX" applications in perl since well before it had the name AJAX. i haven't really used any special tools.

    There exist some modules on CPAN for JSON conversion into and out of perl, but a) JSON doesn't fully work on older browsers (like IE's older than 6), and b) we'd written a version (which produces much uglier javascript that works on older versions of IE) well before JSON became a buzzword, itself.

    I don't know what to tell you, exactly... some folks might be working on some clever tools, somewhere, that present a useful abstraction on AJAX, but I know that I (and my team) just deal with the complexities of it. Granted we've built some tools (you know, some simple javascript libraries to manage the synchronous and the asynchronous http requests and such). But these are just tools to make the task easier, nothing that really abstracts away the fact that there are these requests coming and going, triggered by DHTML events and so on. I don't know if anyone else has.

    ------------ :Wq Not an editor command: Wq
Re: AJAX and Perl-based web applications
by cees (Curate) on Jun 25, 2005 at 13:29 UTC

    There really isn't much difference to building a script that handles an AJAX request compared to a regular request. The main difference is that the AJAX request usually returns XML, or possibly just a small snippet of HTML.

    The hard part around AJAX is the JavaScript side (since I am a perl developer, JavaScript is hard ;). There are already some JavaScript libraries out there that can really simplify this. I've used one called Sarissa that works pretty well, but I haven't done enough in this area to recommend the "best" JavaScript library out there. Just do some searching.

    If you want an example using CGI::Application, then you can have a look at my attempt at building an Upload Meter (something like Apache::UploadMeter). It is just a prototype, so there isn't much in the way of documentation, but it does show how you can do AJAX with CGI::Application (as you will notice, it is no different from a regular CGI::Application module). Here is a tarball of the entire prototype.

    I would like to wrap this up in a simple to use module at some point, but time is my enemy right now :)

Re: AJAX and Perl-based web applications
by dragonchild (Archbishop) on Jun 27, 2005 at 02:55 UTC
    I'm doing R&D for my employer using Catalyst for AJAX applications right now. The key is the Javascript library you use. Remember - Ajax is just a way for the browser to make HTTP requests without having to refresh the page. Nothing less, nothing more. On the serverside, you're doing standard HTTP request/responses. You can use Catalyst, CGI::Application, Maypole, OpenInteract, or whatever else you want to use.

    The JS side is where things get interesting. I personally use HTML::Prototype and the Prototype JavaScript library for my AJAX work. (I have documentation for this forthcoming.)


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: AJAX and Perl-based web applications
by sri (Vicar) on Jun 25, 2005 at 05:01 UTC
    I hope Catalyst "is" a standard Perl web tool.
    Note that you can run Catalyst apps as cgi too. (next to mod_perl1/1.99/2,fcgi...yadayada )
      Yep, I would say it is. I didn't phrase the question quite right--I just meant that I was looking for a general answer to what people were doing to apply AJAX-type thinking to their perl-based CGI applications.
Re: AJAX and Perl-based web applications
by Anonymous Monk on Jun 25, 2005 at 04:42 UTC
Re: AJAX and Perl-based web applications
by pinguxx (Initiate) on May 26, 2006 at 19:30 UTC
    have u tried Perl-Ajax?? it works ok for me
      I have tried the perl AJAX Module but I am not getting successful in this and finding it very complex.
      I have tried the example given on link :
      http://www.perl.com/pub/a/2006/03/02/ajax_and_perl.html?page=2
      But it is not working completely.
      It would be a great help if you could provide some working easy example to understand the basics of CGI::AJAX.
      Many thanks
      -Vivek

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://469868]
Approved by itub
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2020-05-28 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (165 votes). Check out past polls.

    Notices?