#!/usr/bin/perl -w use strict; use CGI qw/:standard :cgi-lib/; use XML::Simple; # the web server needs create and write access to that file # it should probably not be in the web tree (under DocumentRoot # or any accessible directory like cgi-bin and al. under Apache) my $OUT= "/web/data/out.xml"; # dispatch table => my %process= ( display_form => \&display_form, process_form => \&process_form, ); my $function= param( 'function') || 'display_form'; # display_form is the default Delete( 'function'); # we don't need to save function in the XML, so let's remove it if( my $process= $process{$function}) { $process->(); } else { display( p( h2( "Error")), p( "wrong function '$function'") ); } exit; # the interesting part, gets the params and saves them as XML sub process_form { # from CGI get all the params my %params = Vars; # only useful if you have checkbox_group (that can take several values # for a single param). There might be a better way, but this shoud work foreach my $param (%params) { next unless( defined $params{$param}); # to avoid warnings when the param is not filled my @values= split("\0", $params{$param}); # multiple values are separated by a null char if( @values > 1) { $params{$param}= [ @values ] } } # from XML::Simple turn them into xml read the docs! my $xml= XMLout( \%params, noattr => 1, rootname => 'data', xmldecl => 1 ); # I am being extra-paranoid here, but you never know # notethat this sends an Internal Error to the client open( OUT, '>', $OUT) or die "cannot open $OUT: $!"; print( OUT $xml) or die "could not print in $OUT: $!"; close OUT or die "could not close $OUT: $!"; # send back an acknowledgement and the data (escaped, in pre tags) display( p( "OK, XML data saved in $OUT"), pre( escapeHTML( $xml))); } # this is just an example, the generated form can be in a separate file, # or generated using a templating module (see Text::Template or HTML::Template) # the form here is straight out of the CGI docs sub display_form { display( h1('A Simple Example'), start_form, hidden( function => 'process_form'), "What's your name? ",textfield('name'),p, "What's the combination?", p, checkbox_group(-name=>'words', -values=>['eenie','meenie','minie','moe'], -defaults=>['eenie','minie']), p, "What's your favorite color? ", popup_menu(-name=>'color', -values=>['red','green','blue','chartreuse']),p, submit, end_form, hr ) } # a helper sub that wraps the text to be displayed sub display { print header, # HTTP header start_html('A Simple Example'), # title @_, # what we want to display end_html; }