Article

Programming Perl 101

Page: 1 2 3 4 5 6 7 8 Next

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.

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!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Follow SitePoint on...