Home Guides Advanced Article

Using Ajax from Perl

1.0/5.0 (2 votes total)

Dominic Mitchell
May 31, 2006

Dominic Mitchell
Dominic Mitchell has written 1 articles for CGIDir.
View all articles by Dominic Mitchell...

You could spend a lot of time figuring out all the pieces of JavaScript on the client side and Perl on the server side in order to work out how to use Ajax in your code. However, this is Perl; we like to be a bit lazy. Thankfully, there's already a module on CPAN to take the pain out of it: CGI::Ajax (

CGI::Ajax provides a small bit of infrastructure for your CGI programs. You tell it about some of your functions and it sets up JavaScript to call them and return the results to your page. You don't need to worry about writing the JavaScript to do this, because CGI::Ajax takes care of it. All you have to do is add some JavaScript calls to the functions defined in your script and let CGI::Ajax deal with the plumbing.

The functions that CGI::Ajax creates in JavaScript for you all follow more or less the same pattern. They take two parameters: a list of HTML IDs to get input from, and a second list of HTML IDs to insert the results into. Having ID attributes in your HTML is a prerequisite for enabling this behavior. CGI::Ajax handles querying your web page for the input values and inserting the results when the answer comes back from the server.

By making functions in your CGI script available to the browser, you have the ability to do things that you can't ordinarily do. For instance, you can look up values in a database, or query the system load average. Anything you can do in Perl that you couldn't in JavaScript now becomes possible.

Read more... 

Add commentAdd comment (Comments: 0)  



Related Resources

Other Resources

image arrow