Anyone interested: please comment on this code
- From: "bjjnova" <bjjnova@xxxxxxxxx>
- Date: 30 Apr 2006 09:13:29 -0700
I wrote the following code to manage information from a webform. I
would like to use it to continue learning about Perl and am hoping that
anyone who cares to might comment on this code's form and content. I
am not looking for it to be re-written (even where it may need to be)
but rather just for pointers and indications about where I might be
going wrong. (Just for information purposes, the overall aim of the
script is to create a string of code and it to the same directory this
script is in where it will call and be run by another application.)
The first part of the code in intended to read each of the form
elements' data and create a hash out of the re-formatted info.
The second part is intended to create a temporary, unique, txt file to
store the data of one of the fields. The last line has me a little
confused. I am not sure when this temporary file gets deleted (I want
it to be deleted when the entire script finishes running, after the
output is written).
The third part of the script is intended to write one of the values
from the hash (corresponding to a particular form field datum) to the
temporary text file just created.
The last part of the script takes two other values from the hash (again
corresponding to form field data), concatenate them with a reference to
the text file and then output that string to the same directory (where
it will be picked up and run by another application).
The code:
#!/usr/bin/perl
#===============================================================================
# modules utilized
#===============================================================================
use IO::File;
use POSIX qw(tmpnam);
my $method = $ENV{'REQUEST_METHOD'};
if ($method eq "GET") {
$text = $ENV{'QUERY_STRING'};
}
else { # default to POST
read(STDIN, $text, $ENV{'CONTENT_LENGTH'});
}
#perform first separation into individual form element:value pairs
my @value_pairs = split (/&/,$text);
my %form_results = ();
#five value pairs are sent --
#hash:hashid; pswd:password;lastDMCLI:after description;
#detDescrip:detailed description; firstDMCLI:first part of DMCLI
foreach $pair (@value_pairs) {
#separate each form element name and its corresponding value
($key, $value) = split (/=/,$pair);
#convert plus signs to spaces
$value =~ tr/+/ /;
#decode hex characters
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$form_results{$key} = $value; # store the key in the results hash
}
#store the data in a hash called %FORM
$FORM{$name} = $value;
#===============================================================================
#create a txt file to write detailed Description
#===============================================================================
# try new temporary filenames until we get one that didn't already
exist
do { $descripTmpFilename = tmpnam() }
until $fh = IO::File->new($descripTmpFilename,
O_RDWR|O_CREAT|O_EXCL);
# install atexit-style handler so that when we exit or die,
# we automatically delete this temporary file --
END { unlink($descripTmpFilename) or die "Couldn't unlink
$descripTmpFilename : $!" }
#===============================================================================
#write detailed Description to txt file
#===============================================================================
open ($fh, ">$descripFiles")
print $descripTmpFilename $FORM{chdocDescription}
#===============================================================================
#create DMCLI with description file reference
#===============================================================================
# next line sets the MIME type for the output
print "Content-type: text/plain\n\n";
$dmcli =
$FORM{firstDMCLI}.'/cgi-bin/tempDescrip/'.$descripTmpFilename.$FORM{lastDMCLI}
print OUTPUT $dmcli
.
- Prev by Date: Re: match nested tags
- Next by Date: Re: 'medium' reg exp greediness?
- Previous by thread: match nested tags
- Index(es):
Relevant Pages
|