Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Perl >> Snippet

27 Using Perl on the web (CGI)


Perl scripts often reside on a server and may be called into action via submission of a form from a web page .

In its most common usage, a Perl script responds to the request from a form and creates a web page (text file
that is formatted to HTML standards) that is returned to the requestor - dynamic page generation .

Here 's a sample calling HTML page, which has a form with a single submit button that sends a
request to the server to run the Perl script called decide . pl .

< html >< head ></ head >< body >
< h2 > Decide </ h2 >
< form action = "/cgi-bin/decide.pl" method = "get" >< input type = "submit" value = "decide" ></ form >
< /body</html >

Here 's the script that would be run:

#!/usr/bin/perl
print "Content-type: text/html \n\n " ;
print "<html><head></head><body>" ;
print "Your answer is " + $answer ;
print "</body></html>

In general, the output of the Perl program (via the print statement) is fed to the HTTP server, which
in turns sends it back to the requestor .  The first line of the script provides the only HTTP header
information needed for the browser to correctl interpret the output of the script as an HTML page .