MOCKSTACKS
EN
Questions And Answers

More Tutorials









Common Gateway Interfaces



Perl is the most commonly used language for CGI programming on the World Wide Web. The Common Gateway Interface (CGI) is a essential tool for creating and managing comprehensive websites. With CGI, you can write scripts that create interactive, user-driven applications.

Simple CGI Programs

CGI programs are invoked by accessing a URL (uniform resource locator) describing their coordinates:
http://www.mycompany.com/cgi-bin/program.plx
even though the actual location of the script on the hard drive of the server might be something like:
c:\webserver\bin\program.plx

The simplest CGI programs merely write HTML data to STDOUT (which is then displayed by your browser):

print << ENDOFTEXT;
Content-type: text/html
<HTML>
<HEAD><TITLE>Hello, World!</TITLE></HEAD>
<BODY>
<H1>Hello, World!</H1>
</BODY>
</HTML>
ENDOFTEXT


CGI.pm

CGI.pm is a Perl module written to facilitate CGI programming. It contains within itself the wherewithal to generate HTML, parse arguments and environment variables, and maintain state over multiple transactions. Another feature which is not to be underestimated is the ability to reliably run CGI programs from the command line for the purposes of debugging. A simple example of the CGI.pm module in action is shown below:
use CGI;
$query = CGI::new(); # create CGI object
$bday = $query->param(“birthday”); # get parameters
print $query->header(); # generate Content-type line
print $query->p(“Your birthday is $bday”);


Passing Parameters

CGI programs really shine, however, when they take arguments and format their output depending on those arguments. To pass arguments to a CGI script, ampersand-delimited key-value pairs to the URL:

Everything after the question mark is an argument to the script.

Environment variables provide the script with data about the server, client, and the arguments to the script. The latter are available as the “QUERY_STRING” environment variable. The following example prints all of the environment variables:
# Print all of the necessary header info
print <<ENDOFTEXT;
Content-type: text/html


<HTML>
<HEAD><TITLE>Environment Variables</TITLE></HEAD>
<BODY>
<CENTER><H1>Environment Variables</H1></CENTER>
<TABLE>
<TR><TH>Variable</TH><TH>Value</TH>
ENDOFTEXT


# Now loop over and format environment variables
foreach $key (sort keys %ENV) {
print "<TR><TD>$key</TD><TD>$ENV{$key}</TD></TR>\n";
}
print "</TABLE></BODY></HTML>\n";


CGI.pm is particularly good at extracting parameters in a platform-independent way:
use CGI;


$query = CGI::new();
print $query->header();
print $query->start_html(-title=>'Form Parameters');
print $query->h1('Form Parameters');
foreach $name ( $query->param() ) {
$value = $query->param($name);
print "$name => $value\n";
print $query->br(); # Insert a line break
}


print $query->end_html();




Conclusion

In this page (written and validated by ) you learned about Perl Common Gateway Interfaces . What's Next? If you are interested in completing Perl tutorial, your next topic will be learning about: Perl Database Access.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.