Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


A Taste of Perl

Perl is by far the most popular language for CGI, because, among other things, it has powerful string matching. Perl is an interpreted language optimized for scanning text files, extracting information from those files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). It combines some of the best features of C, sed, awk, and sh.

This is a very brief introduction to Perl; to better understand it you will find it useful to consult Selena Sol's Introduction to Perl, which covers Perl 4. The new features of Perl 5 are covered in An Introducation to Perl 5.

On the first line, you need to tell your shell how to run Perl on your system. This is usually done with a line that tells the shell where Perl is and to execute it. The #! instructs the shell interpreter to run the program specified in the pathname that immediately follows it:-

	
	#!/usr/local/bin/perl
You can usually find Perl on your system by typing which perl.

Our first example is a simple "Hello World!" program.

#!/usr/bin/perl

	print "Hello World!\n";
The "\n" tells the computer to print a newline after the text.

More realistically, your program will compute something and then you want to print the result. Results will be stored in scalar variables, which are denoted in Perl by names beginning with the '$' sign:-

#!/usr/bin/perl

	$t	= "Hello World!\n";
	print	$t;
In CGI applications, your script should first send the 'MIME type' (more correctly 'Internet Media Type'), such as text/html for an HTML page; followed by a blank line and then the HTML page.
#!/usr/bin/perl

	$t	= "Hello World!";
	print	<<EOT;
Content-type: text/html

	<Title> $t </Title>
	<H1>	$t </H1>
EOT
The print statement prints everything following until the "EOT". Text printed to "standard output" goes to the server and thence to the browser. The first line printed is an HTTP header to tell the browser that an HTML file is coming. HTTP header lines must always be separated by a blank line from actual data.

Typically you will use a library of CGI functions to avoid having to handle all the low-level details of the CGI interface. Two of the most popular are cgi-lib.pl and CGI.pm. Although CGI.pm is probably the better library, we will use cgi-lib.pl for simplicity.

Before we can use the cgi-lib.pl library we have to tell Perl where to find it, with the require statement.

	
require	"/home/web/public_html/cgi-bin/cgi-lib.pl";

Now we need to get the input from the user. We only have to call the ReadParse subroutine. It will return us all the form input in an associative array, called in, which is keyed by variable name. To call subroutines in Perl, we prepend the name of the subroutine with an &, like this:

	
	&ReadParse;

Our input is stored in an associative array called in.

	
	$in{'foo'}

We have to print out a line to tell the client that what follows is html code.

	
	print	"Content-type: text/html\n\n";

Perl makes it easy to generate HTML dynamically. print <<EOT means: print everything up to (but not including) the line beginning with 'EOT' (you may use any other unique string). Variables will be 'interpolated', i.e. if the text contains $var then the value of $var will be printed there.

	
	print	<<EOT;
<HTML> <HEAD>
	<TITLE>	Search Results</TITLE>
</HEAD>
<BODY> <H1>	Search Results	</H1>
EOT

Putting these pieces together, and tidying up a little, we have:

#!/usr/local/bin/perl

	$home   = "/home/web/public_html";
	require "$home/cgi-bin/search.pl";
	require "$home/cgi-bin/cgi-lib.pl";
#
	&ReadParse;

	print	"Content-type: text/html\n\n";

	&Search	( $in{'string'}, $in{'group'} );
There you are - just a taste! If you want to learn more Perl, visit the following:-

Introduction to Web Programming

This is a four half-day free course covering the basics of Web Programming, CGI, and both basic and advanced Java. It includes a good Introduction to Perl.


Up to => Home / Authoring / Languages / Perl




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers