Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old July 26th, 2000, 01:45 AM
jgagner jgagner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Seattle, WA,USA
Posts: 84 jgagner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to jgagner
I was wondering if there was a better way of collecting variables thatn the code I'm using now. I guess I would use this command "use CGI", but I have no Idea what that is. There is probably some really simple solution, as always in perl. Here's the code I use now (kinda complicated :confused )

%postInputs = readPostInput();
... #omitted

sub readPostInput(){
my (%searchField, $buffer, $pair, @pairs);
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9]/pack("C",
hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",
hex($1))/eg;
$searchField{"$name"} = $value;
}
}
return (%searchField);
}


...#end of program

Reply With Quote
  #2  
Old July 26th, 2000, 06:02 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 9
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan

<< I guess I would use this command "use CGI", but I have no Idea what that is. >>


yea, you can use CGI.pm module in your cgi program so your form parsing would be very easy.


see this example...

test.html
----------

<html>
<head>
<title>Test</title>
</head>
<body bgcolor="#FFFFD9">
<form method="post" action="test.cgi">
Name : <input type="text" name="name">
<br>
<input type="submit" value="submit">
</form>
</body>
</html>

test.cgi
--------

#!/usr/bin/perl

use CGI;
#use cgi.pm module..

$q=new CGI;

#initialize the cgi.pm..

$name=$q->param('name');

# get the name value from the form..


print $q->header;

#print page header..

print "Hello ".$name."n";


----------------------

That is it...


------------------
SR -
webshiju.com

"The fear of the LORD is the beginning of knowledge..."

Reply With Quote
  #3  
Old July 26th, 2000, 09:50 AM
ledjon
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
I don't really like to use the CGI.pm unless I'm doing something like uploading an image. I'll explain your code so that it's not so confusing, but you really don't need to know what it does.:
##########
#Optional If stament.
if ($ENV{'REQUEST_METHOD'} eq 'POST') {

#Read's the POSTed data.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

#Put the elements into an array.
@pairs = split(/&/, $buffer);

#Loop through the array and do the following:
foreach $pair (@pairs) {

#Split into $name=$value from the $pair
($name, $value) = split(/=/, $pair);

#Convert "+"'s into spaces " ".
$value =~ tr/+/ /;
$name =~ tr/+/ /;

#Convert other things like "%20" to the appropriate values.
$value =~ s/%([a-fA-F0-9][a-fA-F0-9]/pack("C", hex($1))/eg;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9]/pack("C", hex($1))/eg;

#Finalize everything by putting them into a hash (%searchField).
$searchField{"$name"} = $value;
}
#############

The $searchField can be named anything you want, it doesn't matter. The:

my (%searchField, $buffer, $pair, @pairs);

is really a waist of processes unless you plan on using those names again for different variables. By taken them out, you don't have to keep passing elements between sub routines. Below is a clean code that will parse POST and GET data perfectly everytime and the the variables are set globaly so you don't have to pass them on to other subs:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
####
sub parse_data {
####Post Data
read(STDIN, $namevalues, $ENV{'CONTENT_LENGTH'});
@namevalues = split(/&/, $namevalues);
foreach $namevalue (@namevalues) {
($name, $value) = split(/=/, $namevalue);
$name =~ tr/+/ /;
$value =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$INPUT{$name} = $value;
}
####Get Data
$temp=$ENV{'QUERY_STRING'};
@pairs=split(/&/,$temp);
foreach $item(@pairs) {
($name,$value)=split (/=/,$item,2);
$value=~tr/+/ /;
$value=~ s/%(..)/pack("c",hex($1))/ge;
$request{$name}=$value;
}
}#End Sub
[/code]

Items sent through the POST method are $INPUT{name} and GET method elements are $request{name}

Wow, that was allot, I hope it helped

Reply With Quote
  #4  
Old July 26th, 2000, 12:13 PM
jgagner jgagner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Seattle, WA,USA
Posts: 84 jgagner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to jgagner
Thanks, you both helped a whole lot thanks!

------------------
Jerome Gagner

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Variable parsing


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway