HOW TO: Write a basic CGI Script for testing? Print

  • 4

Here is an example of what is probably the most basic CGI Script in perl:

#!/usr/bin/perl
# It's always a good idea to use the strict pragma while developing
# Perl code, it makes Perl warn about dangerous code
use strict;

# We're also going to include the CGI module, so that we can take
# advantage of other programmer's efforts (One of Larry Wall's basic
# tennants is that programmers are fundamentally lazy -- he's probably
# right, but I can't be bothered to prove it right now)
use CGI;

# instantiate a new CGI object
my $cgi = new CGI;

# perform a single print statement, with liberal use of the perl
# string concatenator "." and some CGI methods
print
   $cgi->header .
   $cgi->start_html('Hello World!') .
   $cgi->h1('Hello World!') .
   $cgi->end_html;

# Tell the webserver everything is fine
exit (0);

#!/usr/bin/perl
# It's always a good idea to use the strict pragma while developing
# Perl code, it makes Perl warn about dangerous code
use strict;

# We're also going to include the CGI module, so that we can take
# advantage of other programmer's efforts (One of Larry Wall's basic
# tennants is that programmers are fundamentally lazy -- he's probably
# right, but I can't be bothered to prove it right now)
use CGI;

# instantiate a new CGI object
my $cgi = new CGI;

# perform a single print statement, with liberal use of the perl
# string concatenator "." and some CGI methods
print
   $cgi->header .
   $cgi->start_html('Hello World!') .
   $cgi->h1('Hello World!') .
   $cgi->end_html;

# Tell the webserver everything is fine
exit (0);

To test that Perl and CGI Scripts are working, save the text above in a file called hello.cgi. Then upload the above file to the server.

http://yourdomainname/www/hello.pl

(Replace 'yourdomainname' with your actual domainname.)

You should be able to access the CGI Script URL:

http://yourdomainname/hello.pl

(Replace yourdomainname with your actual domain name.)

and you should see the output:

Hello World!

 


Was this answer helpful?

« Back