-/ Parsing Form Output with Perl /-
sub info
{
my $by = 's0ttle';
my $created = 'July 28th 2001';
my $email = '\c3x30x74x74x6Cx65x40@s0ttle.net';
}
We start out with the form that the user will use to input
their information.
nick/handle:
email:
webpage:
message:
---------------
-------------------
-the html
When the user hits the submit button all the information they
previously entered is sent to the script mentioned in the form tag.
In this case it's gbook.pl here's the form tag that tells
the form where to send the information.
The input that gbook.pl recieves is similar to the following:
handle=somebody&email=somebody@somewhere.com&url=http%3A
%2F%2Fwww.somewebsite.com&comments=Hey+there%3F+I+found+your+site+
while+browsing+the+net%21+%26+I+will+definitely+bookmark+it+for+later
+reference+%3A%5E%29%0D%0A';
This is assuming the user entered:
nick/handle: somebody
email: somebody@somewhere.com
webpage: http://www.somewebsite.com
message: Hey there? I found your site while browsing the net! & I will definitely bookmark it for later
reference :^)
The code that changes this form output into usable bytes is:
1: read(STDIN, $gbinfo, 7740);
2: @gb = split(/&/, $gbinfo);
3: foreach $entry (@gb) {
4: ($key, $value) = split(/=/, $entry);
5: $value =~ tr/+/ /;
6: $value =~ s/%([\w|\d][\w|\d])/pack("C*", hex($1))/ge;
7: $value =~ s/</g;
8: $GBINFO{$key} = $value;
9: }
( yeah, I just took it from my Perl guestbook )
--[ Line 1 reads input from STDIN or the information the form sends to the script
and assigns the input to $gbinfo using $ENV{'CONTENT_LENGTH'} as the length
or the value that tells read() how much of the STDIN to grab.
--[ Line 2 breaks (splits) up the contents of $gbinfo on the ampersand (&) and assigns
the resulting string to the array @gb which looks like the following:
$gb[0]= 'handle=somebody';
$gb[1]='email=somebody@somewhere.com';
$gb[2]='url=http%3A%2F%2Fwww.somewebsite.com';
$gb[3]='
comments=Hey+there%3F+I+found+your+site+while+browsing+the+net
%21+%26+I+will+definitely+bookmark+it+for+later+reference+%3A%5E%29%0D%0A
';
--[ Line 3 starts a foreach loop which takes an element from the array @gb and assigns
it to $entry each time through the loop.
--[ Line 4 is a list assignment which takes the return value of split() and assigns
one to $key and the other to $value respectively. split() is taking $entry which is really
the first element of @gb ($gb[0]) the first time through the loop and separating it into
two parts which gets assigned accordingly.
Lets take a look at what's happening $gb[0]='handle=somebody' , now we go through the
loop and $gb[0] gets assigned to $entry so now:
3: $entry=$gb[0]='handle=somebody';
4: ($key , $value ) = split (/=/ , $entry); # $key='handle'; $value='somebody';
5: $value doesn't have any +'s in it so it remains the same.
6: $value doesn't have any hexadecimal values so it still stays the same.
7: $value doesn't have any HTML tags so it is still unchanged.
8: $value gets assigned to $GBINFO{$key} or $GBINFO{handle}=somebody.
The loop starts over now assigning $entry with $gb[1] and on the next iteration of
the loop $entry gets assigned to $gb[2] and so on until we have reached the end of @gb.
For the sake of example lets take a look at what happens when $entry contains the value of $gb[3].
3: entry=$gb[3]='comments=Hey+there%3F+I+found+your+site+while+browsing+the
+net%21+%26+I+will+definitely+bookmark+it+for+later+reference+%3A%5E%29%0D%0A';
4: ($key , $value ) = split(/=/ , $entry);
$key ='comments';
$value =' Hey+there%3F+I+found+your+site+while+browsing+the+net%21+%26+I+will
+definitely+bookmark+it+for+later+reference+%3A%5E%29%0D%0A';
5: $value has alot of +'s in it so we transliterate the +'s to spaces resulting in
$value ='Hey there%3F I found your site while browsing the net%21 %26 I will
definitely bookmark it for later reference %3A%5E%29%0D%0A';
6: $value has hexadecimal values which are: 3F, 21, 26, 3A, 5E, 29, 0D, and 0A
which get converted back to their respective ASCII character values:
3F = ? 21 = ! 26 = & 3A = : 5E = ^ 29 = ) 0D0A = newline/carriage return
with this in mind:
$value = ' Hey there? I found your site while browsing the net! & I will
definitely bookmark it for later reference :^)
';
7: $value stays the same since there are no HTML tags in the string.
8: $value gets assigned to $GBINFO{$key} or $GBINFO{comments}='
Hey there? I found your site while browsing the net! & I will
definitely bookmark it for later reference :^)';
--[ Some side notes ]--
If you are wondering where handle and comments got in the output just take
a look at the HTML for the form
Nick/Handle:
Message:
Now do you see? Whatever you define for the form property 'NAME' will be
what shows up as the input to the script. That's it!
{
print"\n.\n";
}