Cougar Mountain Software Support Productivity Tools
For Cougar Mountain Software Support's
Professional Version (V2012, V2011, V2010, V2009, V12-V6)

 


Editor's Note - 04/27/01
Hi DevWebPro Readers,

The Top Selling Web Domains Of All Time (in US Dollars):

Business.com          in 1999 for 7.5 million
AsSeenOnTV.com    in 2000 for 5.0 million
Korea.com               in 2000 for 5.0 million
Wine.com                in 1999 for 3.5 million
AltaVista.com          in 1998 for 3.3 million
eShow.com             in 1999 for 3.0 million
Loans.com              in 1999 for 3.0 million
Dotnology.com        in 1999 for 2.5 million
Tom.com                 in 1999 for 2.5 million
Autos.com               in 1999 for 2.2 million
Coupons.com          in 1999 for 2.2 million

Source:
http://www.submerged-ideas.com

As this is Friday, the issue will be a bit more technical. This issue is an introduction to the very powerful and important web language: Perl.

I hope that you enjoy the article,

Best Wishes,
Pete


» Programming Perl 101
1.) Background Information

If you run a website, you've probably heard of either Perl or CGI at some point. Whether you've merely heard of it, installed a few scripts on your own, or taken one look at the code and turned pale, this tutorial is for you.

Perl is a programming language. Many people think CGI is a language, as in, "I'm going to learn CGI," but this is erroneous. Think of CGI as the method in which you use Perl. You can write CGI scripts using other programming languages, but Perl is by far the most common due to its relative simplicity and power.

Even as more efficient programming languages are starting to take away Perl's popularity, Perl programming remains very popular and learning it is not a waste of time. For example, PHP is similar to Perl in most respects. The syntax used is similar and a lot of the basics are somewhat the same. If you know Perl, you've got a huge head start at learning PHP, and a few other languages as well.

Although Perl may be less efficient than other programming languages like PHP or ASP, there are still infinitely more freeware and shareware scripts out there written in Perl than in any other language. In other words, Perl isn't going away anytime soon.

2.) A Few Small Rules

Now that you have a little background on Perl, let's get down to some of the basic commands.

Like any programming language, Perl has rules. You will need to use certain symbols in certain places to tell the script something. One of the most basic rules in Perl is that every line must end with a semi-colon: ";" . As with every rule there are a few exceptions which we'll go into later, but for now just realize that almost every line of Perl code that you write will end with a semi colon. The semi-colon, in case you're wondering, tells Perl that it has reached the end of a command.

Let's write a quick script to introduce you to some of the basics.

In any Perl-based script, a line like this goes at the very top:

#!/usr/local/bin/perl

This has been nicknamed the "shebang" line, and it is another requirement of the Perl language. The shebang line points the browser to the path to the Perl interpreter on your web host's server. Think of this line as a way of telling your browser, "Here's where you can find a translator to turn my code into a functional script."

Virtually every web host has Perl installed, although it will not always be in the location specified above. If the above line doesn't work, contact your system administrator and ask them for the path to Perl.

Note that you'll need to point to the Perl interpreter in each and every file that you write. (Note: files that end in .cgi, .pl, and .perl can all be considered Perl files.)

This tutorial from now on assumes that you have a basic knowledge of HTML and form fields. If you don't, head on over to http://www.HtmlGoodies.com and read through some of the tutorials offered there.

Let's create our first script!

3.) Starting Off

The script we are about to create will take the information your user submits in a standard feedback form and then load a page showing him what he just entered. Not terribly useful, but a great example to get us started.

In this example, we'll have someone enter their name, city, and favorite food (after all, that's the basic vital information for all of us, right?).

First, we create the HTML page with the form:

<html>
<head>
<title>Submit Your Information</title>
</head>

<body>

<h1>Submit Your Information</h1>
<p>
<form action="display.cgi" method="post" >
First Name: <input type="text" size="30" name="first"><br>
City: <input type="text" size="30" name="city"><br>
Favorite Food: <input type="text" size="30" name="food"><br>
<P><input type="submit" name="submit" value="Submit!">
<input type=reset name=reset value="Reset">
</form>

</body>
</html>

Do you see the names of those form fields? Those are the identifying values assigned to each form field the user will be entering his information into. In this case they are "first," "city," and "food." Think of the names given to form fields as containers that will hold the data entered by a user.

Capture Images!
TechSmith's capture tools let you grab, edit, and
transform image, text, video and voice. Getting
started is as easy as pressing a hotkey.

http://www.devwebpro.com/rd/techsmith2.html


The way this works is that when a user enters their name into the appropriate form field, the name tag will "attach" itself to the data entered in order to identify it for later use. If a user were to enter the word "Marvin" into the "first" field, the word "Marvin" would be assigned the "first" tag. As you'll soon see, we can use Perl to display whatever text has been given the "first" tag. Starting to make some sense?

Now, as you'll notice, we have the form action set to "display.cgi." This line in the form tells us that this information (with name tags attached, of course) will be sent to a certain file. In this case, display.cgi. Since, display.cgi doesn't exist yet, let's create it!

4.) Display.cgi

Now that we've got the form up (call the file whatever you like, just make sure that it has a .html or .htm extension) we'll move onto creating the Perl script to work with the data. The way this entire thing will work is:

1. A user will view a form and fill out the data

2. Each field on the form and the data entered will have a name
slapped to it

3. The data entered, together with the name of the form field, will
be sent to display.cgi for processing

Let's create display.cgi!

First, the shebang line:

#!/usr/local/bin/perl

Next, insert this:

require "subparseform.lib";
&Parse_Form;

Subparseform.lib is a widely used file that will aid us in the creation of this script. Do a search for it on the web and you should be able to find it. If your search fails, contact me and I'll give you some help: chris@mycoding.com.

Now, remember: we want this script to take the information from the form and display it to the user in such a way as to say "This is the information you entered." To do this we need to tell the script to "grab" the form information. But how? We simply have to match up those tags!

Enter this next:

$first = $formdata{'first};
$city = $formdata{'city'};
$food = $formdata{'food'};

Look familiar?

Take notice of a few things: first, the ";" sign is at the end of every line, as it should be. Second, the tag names: They match exactly with the tags on the form information.

Take note of the equals sign ("="). That tells the script to assign the data entered into the form to a variable or container which we can use inside the Perl script. The first line is saying "make $first equal the information entered in the form field with the name 'first'".

As you've probably noticed, first, city and food have a dollar sign in front of them. Perl requires that all variables or containers used in a script begin with the $ sign.

That first line, as I said before, is telling the browser that whatever form field had the name of "first" in the HTML file, now has the name of "$first".

What this allows us to do is use "$first" variable elsewhere in the Perl. What will happen is that $first will be replaced by whatever the user entered into the "first" field on the feedback form!

Starting to see where this is going?

Now, copy and paste this into the display.cgi file:

print "Content-type: text/html\n\n";
print "Thank you, here is the information you entered:<br>";
print "<ul><li>$first</li><li>$city</li><li>$food</li></ul>";

That first line tells the script that you'll be using HTML in one or more of the "print" statements. Now that the information from the form has been assigned new variables beginning with the $ sign, all we have to do is type those variable names out and the script will display what the user entered.

The print statement that you see above simply tells Perl to spit out the HTML code out to the browser after parsing it. Here's what Perl "thinks" when reading the above three lines:

Line One: Hmm... we'll be working with HTML statements in one or more of the following print statements.

Line Two: Ok, this one is simple. I'll just spit out the code between the quotation marks back out to the browser.

Line Three: This line contains three variables named $first, $city and $food that I need to find and replace before sending out this line of code to the browser. I'll take the value of $first from the form field named "first" in the form that was just submitted to me, then I'll do the same for $city and $food. Now that I know what $first, $city and $food stand for, I can send this line of code to the browser with the replacements made.

Here's the entire display.cgi file:

#!/usr/local/bin/perl

require "subparseform.lib";
&Parse_Form;

$name = $formdata{'name'};
$city = $formdata{'city'};
$food = $formdata{'food'};

print "Content-type: text/html\n\n";
print "Thank you, here is the information you entered:<br>";
print " - First Name: $first<br> - City: $city<br>";
print " - Favorite Food: $food";

Let's assume the user entered the name "John," the city "New York," and the favorite food as "Pizza." Here's what John would see displayed in front of him after filling out the form:

Thank you, here is the information you entered:
- First Name: John
- City: New York
- Favorite Food: Pizza

You should now have a basic grasp of some of the rules of Perl and how slapping identifying "tags" on data can be useful. As you'll also learn later on, you can tell Perl to send out emails using these same variables (tags). Imagine having someone enter his email address and name, and getting an email personalized with his name in the subject line!

In Part 2 we'll try to drill some syntax laws into your head, explore "if," "elsif," and "else" commands, and even explore using sendmail to send out automatic confirmation/thank you emails!

[This second issue will be sent out next Friday. -Pete]

ABOUT THE AUTHOR
by Chris Bowyer
chris@mycoding.com
Article courtesy of SitePoint. Discover how-to build, promote and profit from your website from the experts that have done it! SitePoint's two FREE email newsletters are a must-have, subscribe now at:
http://www.sitepoint.com/newsletter/?ientry



We at the Editorial Team would like to thank all our subscribers for
reading DevWebPro. We hope you find this focus on developer issues
useful. Check out http://DevWebPro.com for the DevWebPro site.

Peter Thiruselvam
Editor

The DevWebPro Team




Expert in your field? We're looking for ARTICLES on web site design and development, promotion, positioning, and e-commerce. Articles should be in the 700 to 800 word range and include a 2 line promotion of the author's site. Send submissions to: Pete@DevWebPro.com
» Corporate Info
Advertising
Have your ad reach thousands of webmasters and website owners. contact: Susan@DevWebPro.com
Business Development
Do you have a visitor data-base or a newsletter? iEntry  is interested in exploring partnership opportunities: Business@iEntry.com


» For Our Readers
DevWebPro.com
Visit DevWebPro.com for free software and more free web development tips!
FeedBack
Send us email with comments/suggestions.
Click here
Print This Issue
Printer friendly version of DevWebPro.
click here.


» Webmaster Links
- DevNewz

- DesignNewz

- WirelessProNews

- SearchNewz

- PromoteNewz


» Subscribe For Free!

Text HTML


»WMF Search
Search the WEBMASTERFREE database of free software: