·  PERL – HTTP Request – Get Parameters

 

- Info:

  - This tutorial shows how to get HTTP GET & POST Request Parameters.

 

  - CGI module can:

    - get the value of specific parameter

    - load all parameter names into Array

    - load all parameter names nad values into Hash.

 

- Procedure:

  - Create "PERL Web Application" C:\Inetpub\wwwroot\Test.pl and Form.html.

  - http://localhost/Test.pl?name=John&age=30 (This creates HTTP GET Request)

  - http://localhost/Form.html − Type: John − Register (This creates HTTP POST Request)

 

    Form.html

    <FORM METHOD=POST ACTION=test.pl> 

      <INPUT TYPE=Text    NAME=name                 >

      <INPUT TYPE=SUBMIT  NAME=action VALUE=Register>

    </FORM>

 

- Get value of specific parameter:

  - This example shows how to get value of specific HTTP Request parameter.

 

    Test.pl

    print "Content-Type: text/plain\n\n";

    

    use CGI;                       

    $cgi    = new CGI;            

    

    #Load value of parameter 'name': "John".     

    $name   = $cgi->param("name");

    

    #True if:      /Test.pl?age=30  

    if(!defined($name)) { print("Parameter 'name' is not defined.\n"         ); }

    

    #Also true if: /Test.pl?name=, /Test.pl?name

    if($name eq ""    ) { print("Parameter 'name' is not defined or has no value.\n"); }

    

    #Display value of parameter 'name'.

    print($name);

 

- Load parameter names into array:

  - This example shows how to load all HTTP Request parameter names into Array.

 

    Test.pl

    print "Content-Type: text/plain\n\n";

        

    use CGI;                      

    $cgi   = new CGI;            

    

    #Load parameter names into array: [name age].

    @array = $cgi->param();                

    

    #Check if there are no parameters: "/Test.pl" or "/Test.pl?".

    if(!defined(@array)) { print("There are no parameters in HTTP request!" ); }

    

    #Display parameter names from array: "name age".

    for( $i=0; $i<=$#array; $i++ ) {

      print("$array[$i]\n");

    }

 

- Load parameter names and values into hash:

  - This example shows how to load all HTTP Request parameter names and their values into Hash.

 

    Test.pl

    print "Content-Type: text/plain\n\n";

    

    use CGI;                  

    $cgi  = new CGI;        

    

    #Load parameter names & their values into Hash:    (name=>John,age=>30).   

    %hash = $cgi->Vars();   

    

    #True if:      /Test.pl, /Test.pl?

    if(!defined(%hash))         { print("There are no parameters in HTTP request!\n" ); }

    

    #Also true if: /Test.pl?age=30   

    if(!defined($hash{"name"})) { print("Parameter 'name' is not defined.\n"); }

    

    #Also true if: /Test.pl?name=, /Test.pl?name

    if($hash{"name"} eq "")     { print("Parameter 'name' is not defined or has no value.\n"); }

    

    #Display parameter names & their values from Hash: "name=John age=30".

    while ( ($key,$value) = each(%hash) ) { #Function each returns next key-value pair.

      print("$key = $value\n");

    }