Skip to main content

XForms tip: Accepting XForms data in Perl

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.

Summary:  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.

View more content in this series

Date:  03 Oct 2006
Level:  Intermediate
Activity:  2158 views

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.


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.


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.



Download

DescriptionNameSizeDownload method
Perl XForms samplesxforms_perl_source.zip4KB HTTP

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.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=164725
ArticleTitle=XForms tip: Accepting XForms data in Perl
publish-date=10032006
author1-email=tyleranderson5@yahoo.com
author1-email-cc=troy@backstopmedia.com

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers