In Perl, Common Gateway Interface (CGI) is nothing more than a protocol which defines the interaction of web servers with some executable programs in order to produce dynamic web pages. Basically, it shows how the web server sends information to the program and the program sends the information back to the web server which in turn can be sent back to the browser.
GET
and POST
are not interchangeable and both the types are different. Proxy servers may cache the output of GET
requests. GET
method is the default method for all web request to pass the information from browser to the web server and it also creates a long string that shows up in the browser’s URL box. It sends the encrypted user information attached to the page request. The page and the encrypted information is separated by ? character:
Example:
http://servername.com/cgi-bin/script_name.cgi or.pl?key1=value1&key2=value2…….
This information is passed through QUERY_STRING
header and by using QUERY_STRING
environment variable it can be easily accessed in your CGI program. Only 1024 characters can be there in a request string as the GET
method has the size limitation. Information can be passed by simply concatenating key-value pairs along with any URL.
NOTE: If you are dealing with passwords or any other sensitive information in order to pass it to the server, then using the GET
method is not a good choice.
Example:
< html >
< head ></ head >
< body >
< b > Search Your Query:</ b >< br >
< FORM action = "Gfg_get.pl" method = "GET" >
< input type = "text" name = "q" size = "20" maxlength = "120" >
< input type = "submit" value = "Search" >< br >
< input type = "radio" name = "l" value = "Web" checked>Web
< input type = "radio" name = "l" value = "India" >IND
</ FORM >
</ body >
</ html >
|
Output:

Perl-CGI script for the above GET method form:
#!"c:\xampp\perl\bin\perl.exe"
$buffer = $ENV{'QUERY_STRING'};
#split information into key/value pairs
@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;
$value =~ s/~!/ ~!/g;
$FORM{$name} = $value;
}
$SearchTerm = $FORM{'q'};
$Location = $FORM{'l'};
print "Content-type:text/html\r\n\r\n";
print "< html >";
print "< head >";
print "< title >GeeksForGeeks - Get Method</ title >";
print "</ head >";
print "< body >";
print "< h3 >Hello You searched '$Location' for '$SearchTerm'< br >
Few Matches Found!< br >
< br >
Match 1< br >
Match 2< br >
Match 3< br >
Match 4< br >
etc.....</ h3 >";
print "</ body >";
print "</ html >";
1;
|
Output:

As shown above, in the output image, the information is passed along with the URL:
http://localhost/xampp/cgi-bin/Gfg_get.pl?q=music&l=Web
In contrast, the POST
method is the most reliable method to pass information to a CGI program. Generally, the POST
method is used when the information is required to be uploaded on the server. Aiming of uploading larger amount of data, POST
method is considered more suitable for it instead of GET
method as none of the data appears in the URL box. Similar to the GET
method, the information is packed in this too but instead of sending it as a text string after ? in the URL box, it sends it as a separate message to the server through a different route which is accessible by your Perl/CGI program.
Example:
< head ></ head >
< body >
< b >Please Fill in the Information:</ b >< br >
< form action = "GfG_post.pl" method = "post" >
First Name:< br >
< input type = "text" name = "first_name" size = "25" maxlength = "100" >< br >
Last Name:< br >
< input type = "text" name = "last_name" size = "25" maxlength = "100" >< br >
< br >
Languages:< br >
< input type = "checkbox" name = "python" value = "yes" >Python
< input type = "checkbox" name = "java" value = "yes" >Java
< input type = "checkbox" name = "kotlin" value = "yes" >Kotlin
< input type = "checkbox" name = "perl" value = "yes" >Perl
< input type = "checkbox" name = "swift" value = "yes" >Swift
< br >
Payment: < select name = payment >
< option >---Select---</ option >
< Option value = "Paypal" > Paypal </ option >
< Option value = "Internet Banking" > Internet Banking </ option >
< Option value = "Credit Card" > Credict Card </ option >
< Option value = "Paytm" > Paytm </ option >
</ select >< br >
< br >
First Time Customer?< br >
< input type = "radio" name = "first_time" value = "Yes" >Yes
< input type = "radio" name = "first_time" value = "No" >No< br >
< br >
Feedback:< br >
< textarea wrap = "virtual" name = "feedback" cols = "25" rows = "3" ></ textarea >< br >
< br >
< input type = "submit" value = "Place Order" >
</ form >
</ body >
</ html >
|
Output:

Perl-CGI script for the above POST method:
#!"c:\xampp\perl\bin\perl.exe"
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;
$value =~ s/~!/ ~!/g;
$FORM{$name} = $value;
}
if($FORM{python})
{
$python_flag ="YES";
}
else
{
$python_flag ="NO";
}
if($FORM{java})
{
$java_flag ="YES";
}
else
{
$java_flag ="NO";
}
if($FORM{kotlin})
{
$kotlin_flag ="YES";
}
else
{
$kotlin_flag ="NO";
}
if($FORM{perl})
{
$perl_flag ="YES";
}
else
{
$perl_flag ="NO";
}
if($FORM{swift})
{
$swift_flag ="YES";
}
else
{
$swift_flag ="NO";
}
$first_name= $FORM{'first_name'};
$last_name= $FORM{'last_name'};
$payment_method= $FORM{'payment'};
$first_time= $FORM{'first_time'};
$feed_back= $FORM{'feedback'};
print "Content-type:text/html\r\n\r\n";
print "< html >";
print "< head >";
print "< title >GeeksForGeeks - Post Method</ title >";
print "</ head >";
print "< body >";
print "< h3 >Hello $first_name $last_name</ h3 >";
print "< h3 >Here is your Purchased Order!</ h3 >";
print "< h3 >Python: $python_flag</ h3 >";
print "< h3 >Java: $java_flag</ h3 >";
print "< h3 >Kotlin: $kotlin_flag</ h3 >";
print "< h3 >Perl: $perl_flag</ h3 >";
print "< h3 >Swift: $swift_flag</ h3 >";
print "< h3 >Payment Method: $payment_method</ h3 >";
print "< h3 >First Time Customer: $first_time</ h3 >";
print "< h3 >Feedback: $feed_back</ h3 >";
print "</ body >";
print "</ html >";
1;
|
Output:

As it can be seen in the image above, that after using POST
method the information is uploaded to the server without appearing into the URL box. This makes data sent over the internet, more secure in comparison to the GET
method.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Jul, 2019
Like Article
Save Article