Create a Free Website

PERL CGI Tutorial
for Writing Interactive Form Scripts


Send an HTML Page to a Browser

The following block of code will send the paragraph; Hello World. This is my first CGI script. to a browser as a web page.

You can use this same setup to send any HTML page to a browser.

helloworld.pl

#!/usr/bin/perl
print "Content-type:text/html\n\n";
print "<html>\n";
print "<head><title></title></head>\n";
print "<body>\n";
print "<p>Hello World. This is my first CGI script.</p>\n";
print "</body>\n";
print "</html>\n";

Before we go on, we should mention that PERL is case sensitive. Mail and MAIL are not the same in PERL.

Common errors in code are: missing semi colons where required and extra spaces.

TRY This Website Builder FREE!

No Credit Card Needed!
Create beautiful,professional websites
WITHOUT Learning to Code!!

Use this Next Generation Builder
to make blogs, portfolios or
full blown websites.
No Experience Needed!

SquareSpaceSquareSpace

Start Your Website Now!SquareSpace

Professional websites
Without the HASSLE!



Adding Style

You can add style settings to the paragraph text as embedded, linked or inline code.
print "<head><title></title>\n";
print "<link rel=\"stylesheet\" href=\"yourstyle.css\" type=\"text/css\">\n";
print "</head>\n";
This code would be added within the head section to add a linked style sheet. If you place quotation marks within the print statement, they must be preceded by a backslash. You can just leave them out.

See Style Sheets in our HTML tutorial.



Important Stuff

The code shown below appears at the top of every Perl script. It is called the shebang line. It tells the browser the path to the Perl interpreter on your server. The path may not be the same on all servers.
#!/usr/bin/perl

If testing scripts on the IndigoAMPP web server, change the shebang line to:
#!C:/indigoampp/perl-5.10.0/bin/perl.exe

Before you upload the script to your server, check the FAQ section on your server for information on the correct path to your Perl interpreter.

When you upload the script to your web host's cgi-bin folder, be sure to use the Chmod function on your ftp client tool or file manager to change the file permissions from 644 to 755. This can usually be accomplished by right clicking on the uploaded file and choosing Chmod.