#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Seattle, WA,USA
    Posts
    84
    Rep Power
    14
    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
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    << 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..."
  4. #3
  5. No Profile Picture
    ledjon
    Guest
    Devshed Newbie (0 - 499 posts)
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Seattle, WA,USA
    Posts
    84
    Rep Power
    14
    Thanks, you both helped a whole lot thanks!

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

IMN logo majestic logo threadwatch logo seochat tools logo