WebDeveloper.com �: Where Web Developers and Designers Learn How to Build Web Sites, Program in Java and JavaScript, and More!   
Web Developer Resource Directory WebDev Jobs
Animated GIFs
CSS
CSS Properties
Database
Design
Flash
HTML
HTML 4.01 Tags
JavaScript
.NET
PHP
Reference
Security
Site Management
Video
XML/RSS
WD Forums
 Client-Side
  Development

    HTML
    XML
    CSS
    Graphics
    JavaScript
    ASP
    Multimedia
    Web Video
    Accessibility
    Dreamweaver
    General
    Accessibility
    Dreamweaver
    Expression Web

    General

 Server-Side
  Development

    PHP
    Perl
    .NET
    Forum, Blog, Wiki & CMS
    SQL
    Java
    Others

 Site Management
    Domain Names
    Search Engines
    Website Reviews

 Web Development
  Business Issues

    Business Matters

 Etc.
    The Coffee Lounge
    Computer Issues
    Feedback




Beginners Guide to CGI in Perl

by Nathan Poole

A few lessons in the finer arts of Perl.

Printing

Of course our first program will have you print "Hello World!" on the screen. So you'll need to know how to print (even though the example will be shown later).

print "Whatever you want to print here";

As simple as that. Like many HTML tags you need to show Perl when to start printing, and when to stop. We accomplish this by using quotations, and then we end the statement in a semi-colon.

Defining Variables

When programming, you may need to store some information somewhere, and unfortunately Perl just can't keep it in it's nooks and crannies until you need it. So what we do is input that information into what's called a variable. A variable is assigned with a number sign ($) and holds that information until you need it (if at all). For example...

$myname = "Nathan Poole";

And now, until someone deletes my program, the characters $myname means "Nathan Poole" and nothing more. You want to name your variable names something related to what information it will hold. So $myname seems justified.

Comparing with if statements

This will be the hardest thing we'll discuss here, so you may want to pay a little closer attention. Sometimes you'll need to check if a variable equals something in particular. For example, if I had the following...

$myname = "Nathan Poole";

And perhaps I needed to check that information. I would use an if statement. It looks a little something like this.

if ($myname eq "Nathan Poole") {
print "Hello Nathan, yes, I know you quite well";
}

Think of it like this -- the comparison portion of our program is in between brackets. And the ACTION part (the section that our program is to do something after it decides if something is true or not) is in between braces. Now that example would check if $myname equals Nathan Poole. And we know it does so after Perl realizes that, it will print out "ello Nathan, I know you quite well". In fact, let's give that a try.

Now.. Your First Program!

Let's open MS-DOS and type the following

cd C:\Perl.in.

We do all our Perl work in C:\Perl.in so we can keep track of our files. Next, type Edit 1.pl which will open MS-DOS's text editor. Type the following on your screen.

#!/usr/bin/perl
print "Hello World!";

Next, save that file and exit (FILE->SAVE -- FILE->EXIT) and type perl 1.pl.

No prizes for guessing what this does. By now you should have the text "Hello World!" on your screen. Not too difficult yet is it?

Now that we've used your perl path, and we've used your print statement, let's try using a variable ($) with an if statement. Type edit 2.pl at your MS-DOS prompt and type the following in.

#!/usr/bin/perl
$myname = "Nathan Poole";

if ($myname eq "Nathan Poole") {
print "Yes, it's Nathan alright";
}

Go to File, then Save, then Exit. You should now be back at the DOS prompt. Type perl 2.pl. WOW! It worked! You just made 2 programs in about 10 minutes. Not bad for a beginner. Let me know if you had any problems with this tutorial, did everything work out for you? Too easy? Too hard? My mailbox is always open.



HTML5 Development Center


Recent Articles