Web Perl   «Prev  Next»

Lesson 1

Perl Web Programming

This course is designed to teach you about Web programming and how to use Perl to write programs for the Web. The following Perl related course material will be covered.
  1. Programming techniques specific to the Web
  2. Details of the CGI interface to common Web servers
  3. Perl 5 features that make Web programming easier
  4. Using forms on your Web site
  5. Sending email from a CGI program

By the end of this course you will have created a flexible Guestbook system and an email program to send mail from a form instead of a mailto: URL. These programs were selected to use all of the techniques that you will learn in this course. You will also find them useful as templates for other applications that you will write in the course of your Web programming work.
Depending on your experience level, you can expect to spend 15 to 30 minutes on each lesson.
Before we actually start, let us cover:
SEMrush Software

Perl Course Prerequisites

This course is designed for anyone who is already familiar with Perl and wants to understand how to use it specifically for Web-based applications. You should already be able to program in Perl with basic familiarity using data types such as
  1. scalars,
  2. arrays, and
  3. associative arrays or hashes.

You should be able to use regular expressions; we will write several programs which will test your skill in pattern matching.
You should also know some basic HTML (HyperText Markup Language) since Perl programs will print to the screen in HTML.
This class will not teach you to program in Perl or to write HTML. The course will help you to sharpen your skills in both Perl and HTML, and describe ways to apply those tools to Web based CGI programming.


Developing Perl Solutions

As a rule, Perl lets you do what you want, when you want to, and how you want to. Perl is far more concerned about letting you develop a solution that works than it is about slotting your chosen solution into a set of standards and a rigid structure. The core of any program are the variables used to hold changeable information. You change the contents of those variables using operators, regular expressions, and functions. Statements help to control the flow of your program and enable you to declare certain facts about the programs you are running. If you cannot find what you want using the base Perl function set, you can make use of a number of modules, which export a list of variables and functions that provide additional information and operations. If you want to work in a structured format, modules also support objects, methods, and object classes. You can, of course, also make your own modules that use your own functions. We will have a quick look at some of the elements and components within Perl that will help when we start to look at these individual items in more detail.
  1. Perl Variables:Variables hold variable pieces of information, they are just storage containers for numbers, strings, and compound structures (lists of numbers and strings) that we might want to change at some future point. Perl supports one basic variable type, the scalar. A scalar holds numbers and strings, so we could rewrite the simple “Hello World” example at the beginning of this chapter as
    $message = "Hello World\n";
    print $message;
    
In this example, we have assigned a literal to a variable called $message. When you assign a value to a variable, you are just populating that variable with some information. A literal is a piece of static information and in this case it is a string, but it could have been a number. By the way, when you assign information, you are assigning the value to the right of the assignation operator (the = sign) to the lvalue on the left. The lvalue is the name given to a variable or structure that can hold information. Normally this is a variable, but functions and objects are also types of lvalues. You will notice in the preceding example that the variable, $message, has a $ character at the beginning. The dollar sign identifies the variable as being a scalar. You always use a dollar sign when accessing a scalar value. The way to remember a scalar is that the $ sign looks like an "s", for scalar! There are also some compound variable types, namely the array and the hash.


What you need to take this Course

You will need a computer with the following software installed.
  1. A Web Server: You can run Perl and CGI on just about any computer (Mac, Windows, or Unix), but it's best if you do the exercises on a computer running the same OS as what you will use for your production work. It's also better if you can experiment on a machine that you can crash now and then without upsetting the apple cart. Try loading Linux and Apache on an old spare 486 for a nice, fast local Unix environment.
  2. Perl: Version 5.003 or later. This needs to be installed on the same system as your Web server.
  3. A Web Browser: This web server does not need to be installed on the same computer, but it does need to be able to access the server via a network connection.
  4. TCP/IP: Whatever operating system you are using on your server needs to have TCP/IP installed. If it's a Unix system (e.g. Linux, BSD, Solaris, etc.) then it probably already has TCP/IP installed. If it's Windows or MacOS you may need to do something to install it.

What is Perl?

The most fundamental aspect of Perl is that it is a high-level programming language written originally by Larry Wall and now supported and developed by a cast of thousands. The Perl language semantics are largely based on the C programming language, while also inheriting many of the best features of sed, awk, the Unix shell, and at least a dozen other tools and languages. Perl is particularly strong at process, file, and text manipulation. This makes it especially useful for system utilities, software tools, systems management tasks, database access, graphical programming, networking, and web programming. These strengths make it particularly attractive to CGI script authors, systems administrators, mathematicians, journalists, and just about anybody who needs to write applications and utilities very quickly.
Perl has its roots firmly planted in the Unix environment, but it has since become a cross-platform development tool. Perl runs on IBM mainframes; AS/400s; Windows 7, 10; OS/2; Novell Netware; Cray supercomputers; Digital's VMS; Tandem Guardian; HP MPE/ix; Mac OS; and all flavors of Unix, including Linux. In addition, Perl has been ported to dozens of smaller operating systems, including BeOS, Acorn's RISCOS, and even machines such as the Amiga. Larry Wall is a strong proponent of free software, and Perl is no exception. Perl, including the source code, the standard Perl library, the optional modules, and all of the documentation, is provided free and is supported entirely by its user community. Before we get into the details of how to program in Perl, it is worth taking the time to familiarize yourself with where Perl has come from, what it can be used for, and how it stacks up against other languages. We will also look at some popular misconceptions about what Perl is and at some success stories of how Perl has helped a variety of organizations solve an equally varied range of problems.


To even begin to cover a language with such a rich history and huge influence over the world of computing and the web is a challenging task, so this module touches on the highlights. By the time you finish this module, you will have a good understanding of the history of Perl and where to go to get more help when you need to know more than this course offers. Learning how to find the answers to your questions is probably one of the most valuable skills you can develop. The name of the language is Perl. Larry Wall, the creator of Perl, originally wanted a name with positive meaning and named the language Pearl, but before its release, he discovered another programming language named Pearl, so he shortened the name to Perl.
When people write Perl (uppercase), they are referring to the programming language you will study in this course. When people write perl (lowercase), they are referring to the binary executable used to run Perl, the language. In the next lesson, platform-specific links and explanations for the materials will be discussed.

Perl History

Perl is a relatively old language, with the first version having been released in 1988. The basic history is shown in Table 1-1. If you want a more detailed history of Perl, check out the perlhist documentation installed with Perl, or visit CPAST, the Comprehensive Perl Arcana Society Tapestry at history.perl.org.

Table 1-1. Perl Version History
Table 1-1. Perl Version History