It is always worth repeating the fact that AJAX (Asynchronous Javascript and XML) is not a new programming language, it is a technique. It is a technique that allows a web page designer to use a Javascript to communicate with a program held on the web server, and then use the response from the server within their web page. This, of course, is a major advantage for any Perl programmers. It means that the Perl programmer can:
- build a Perl application on a web server
- incorporate the Perl application into a web page (or even many web pages)
And the general technique is always the same:
- the Javascript web page sends a request to the web server
- the web server processes the request and then replies to the web page
- the web page processes or displays the response from the web server
The starting point for the programmer is, therefore, to create a Perl CGI application.
Creating a Simple Perl CGI Web Page
The first line of a CGI application must always be the shebang line. This tells the web server where to locate the Perl interpreter:
The second line of a CGI application should then output the page's HTML header to the web browser:
Once those two compulsory lines are in place the programmer can start building the main functionality of the application. In this example the application will calculate the times for sunrise and sunset for the current day. Therefore the correct module must be used. In this case it's the Astro Sunrise module:
This requires an input of the latitude and longitude of a location on the surface of the Earth:
From this the times for sunrise and sunset can be produced:
And then returned:
If this is saved into a file in the server's CGI directory then it can be tested (as seen in figure 1 at the bottom of this article). And then it is ready to be turned into an AJAX application.
Turning a Perl Script into an AJAX Application
Every AJAX application starts in the same way - it creates an XML HTML request object:
And every AJAX application requires a function that will:
- send a request to the server
- wait until the server has completed its processing of the request
- display the results
The function will call the simple Perl CGI application already saved on the server:
Then a little HTML is required. In this example it defines a button (which will call the request function) and it also defines an area that will be used to display the result:
The end result of this simple application can be seen in figure 2.
Adding Input Variables to the Perl CGI Web Page
At the moment longitude and latitude have been hard coded into the script. However, it is much more useful allow the users to set these. Fortunately user inputs are easy to handle in Perl (as discussed in How to Use a Form in a Perl CGI Web Page) and so it is just a matter of loading another module:
And then handling the inputs (if there are any):
Again this should be test before moving on (as shown in figure 3).
The Completed AJAX Application
The AJAX application will now need a small amount of modification. It will require input boxes for the user to enter their latitude and longitude:
And the function for sending the request will have to be altered so that it adds the longitude and latitude as part of the query string:
This complete AJAX application will (as shown in figure 4) be able to:
- accept inputs from a user
- format the inputs and send them to a web server
- process the input on the server
- return a result from the server to the web page
- display the results on the web page
And all this done through the power of Perl programming (with a little help from Javascript).