Skip to main content

skip to main content

developerWorks  >  XML  >

XForms tip: Accepting XForms data in Perl

developerWorks
Document options

Document options requiring JavaScript are not displayed

Discuss

Sample code


Rate this page

Help us improve this content


Level: Intermediate

Tyler Anderson (tyleranderson5@yahoo.com), Freelance writer

03 Oct 2006

The Perl programming language is widely used on the Internet, and it will continue to be popular for quite some time. It's considered an easy language to program in because it handles strings very well. Also, what you can do in a few lines in Perl takes many more lines of code in other programming languages. In this tip, you'll learn how to submit an XForms form using POST to a Perl script, and capture the data for later use.

The instance data

The instance data you'll use in this tip is shown in Listing 1.


Listing 1. The instance document

<auctionItems  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <auctionItem itemID="2">
    <purchaseDate>2006-09-18</purchaseDate>
    <auctionLength>600</auctionLength>
    <purchasePrice>10</purchasePrice>
    <description>Vintage hair dryer</description>
    <estimatedValue>N/A</estimatedValue>
  </auctionItem>
  <auctionItem itemID="5">
    <purchaseDate>2006-09-18</purchaseDate>
    <auctionLength>300</auctionLength>
    <purchasePrice>4</purchasePrice>
    <description>Box Lot</description>
    <estimatedValue>N/A</estimatedValue>
  </auctionItem>
</auctionItems>

Browsers tested on
The XForms samples described in this tip have been tested and work with Firefox 1.5 (with the XForms extension installed) and Microsoft® Internet Explorer 6 with the Formsplayer control installed. The download contains the XHTML file for Firefox and the HTML file for IE.

Next you'll take a look at the form needed to call your Perl script.

Sending a request to a Perl script

Making the POST request to a Perl script is similar to how you do it using HTML. Take a look at the form in Listing 2.


Listing 2. Submitting a request to a Perl script

<?xml version="1.0" encoding="ASCII"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ev="http://www.w3.org/2001/xml-events" 
      xmlns:xforms="http://www.w3.org/2002/xforms">
  <head>
    <title>Instance Data-To-XHTML/XForms Example</title>

    <xforms:model id="auctionItems">
      <xforms:instance src="auctions.xml"/>
      <xforms:submission id="submit_model_auctionItems" 
          action=http://localhost/cgi-bin/xformsPerl.pl
                         method="post"/>
    </xforms:model>
    <link href="gen_default.css" rel="stylesheet"/>
  </head>
  <body>
    
    <h1 align="center">Auction Item Inventory</h1>
    
    <xforms:repeat nodeset="auctionItem" id="repeatAuctionItems">
      <h2 align="center">Auction Item</h2>
      <xforms:input ref="@itemID">
        <xforms:label>Item ID</xforms:label>
      </xforms:input>
      <xforms:input ref="purchaseDate">
        <xforms:label>Purchase Date</xforms:label>
      </xforms:input>
      <xforms:input ref="auctionLength">
        <xforms:label>Auction Length</xforms:label>
      </xforms:input>
      <xforms:input ref="purchasePrice">
        <xforms:label>Purchase Price</xforms:label>
      </xforms:input>
      <xforms:input ref="description">
        <xforms:label>Description</xforms:label>
      </xforms:input>
      <xforms:input ref="estimatedValue">
        <xforms:label>Estimated Value</xforms:label>
      </xforms:input>

    </xforms:repeat>

    <xforms:group>
      <xforms:trigger>
        <xforms:label>Insert Auction Item</xforms:label>
        <xforms:insert nodeset="auctionItem" 
                       at="index('repeatAuctionItems')"
                       position="after" ev:event="DOMActivate" />
      </xforms:trigger>
      <xforms:trigger>
        <xforms:label>Delete Auction Item</xforms:label>
        <xforms:delete nodeset="auctionItem" 
                       at="index('repeatAuctionItems')"
                       ev:event="DOMActivate" />
      </xforms:trigger>
    </xforms:group>

    <xforms:submit submission="submit_model_auctionItems">
      <xforms:label>Submit</xforms:label>
    </xforms:submit>

  </body>
</html>

Note the line in bold. The submit button is linked to that submission tag, and to make it submit to your Perl script, simply set the URL of the action attribute to the URL of your Perl script, which in this case is http://localhost/cgi-bin/xformsPerl.pl. Next you'll use Perl to process the request.



Back to top


Processing the request using Perl

Listing 3 contains the Perl script that receives the XForms POST request.


Listing 3. Capturing and displaying POST requests using Perl

#!perl
use warnings;
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/xml\n\n";
my $data = param('POSTDATA');
print $data;

The first two use statements tell Perl what warning reporting and syntanx checking you want. The second two tell Perl that you're using a CGI style script. Next, the header is displayed with the type of the contents being text/xml. The POSTed data is then retrieved from the param('POSTDATA') variable. The last print statement displays the XML to the browser, as shown in Figure 1.


Figure 1. Displaying the XForms XML instance data using Perl
Displaying the XForms XML instance data using Perl

Great! Now you can work with the XML however you want using Perl's XML::Simple or any other Perl XML module. See the Resources for an IBM developerWorks resource to help you get started using Perl and XML.



Back to top


Summary

You're now able to create an XForm that submits POST requests to your Perl scripts, and then capture and work with the XML instance data that was sent from the form. If you're using a Web programming language as popular as Perl, then adding XForms to the mix is going to be an exciting new adventure for you, and you're already well on your way.




Back to top


Download

DescriptionNameSizeDownload method
Perl XForms samplesxforms_perl_source.zip4KBHTTP
Information about download methods


Resources



About the author

Tyler Anderson recieved both his B.S. in Computer Science in 2004 and his M.S. in Electrical and Computer Engineering in 2005 from Brigham Young University. Tyler has written and coded numerous articles and tutorials for IBM developerWorks and DevX.




Rate this page


Please take a moment to complete this form to help us better serve you.



YesNoDon't know
 


 


12345
Not
useful
Extremely
useful
 


Back to top