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:


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.
  • 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.
  • 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.


Perl Today

Today, Perl is widely used throughout the world. It comes standard on every major operating system apart from Windows and is still extensively used in web development, thus driving many websites. New startups choose Perl as their language of choice for data processing, system administration, web development, and other uses. As of this writing, Ricardo Signes, a long time Perl hacker, is overseeing the development of Perl. Perl 6, a new language with roots in Perl 5, is being actively worked on with several interesting implementations, including a Niecza, which runs on Mono/.NET.
This course focuses on 5.8.x and 5.10.x versions of Perl, even though support for both of these has offi cially been discontinued. Why?
This was a difficult decision to make, but there were several reasons for this decision. An important consideration is that surveys show most businesses still run these versions of Perl. Fortunately, this choice is not as bad as it might sound. The Perl 5 Porters (known as "'P5P") work hard to keep new releases of Perl backward compatible. Perl 5.14.2 ships with almost half a million tests (455,832, to be exact) to ensure that Perl works exactly as intended. Thus, what you learn to write throughout this course generally works unmodifi ed on later versions of Perl.
  • Versions and Naming Conventions:
    The current version of Perl is Perl 5.6, with a develop version, v5.7, already in production. Some sites are migrating to v5.6, others seem to be dragging their heels, although there are no major compatibility problems. Up until March 2000, the situation concerning the available versions of Perl was quite complex, but we will start with the current version first. From the release of Perl 5.6 there are two very simple strands. Even version numbers, such as 5.6 and 5.8 are considered to be stable releases of the language.
    Odd version numbers, such as 5.7 and 5.9, are development releases. Perl 5.6 was a long time coming over two years since the last major release but it also set a landmark for Perl's development. It was the first version that really reunited the core and Win32 versions of Perl, as well as providing some compatibility enhancements. For example, the Windows ports now support fork, something not natively provided by the Windows operating system. Also updated were the Perl compiler and the threading system (which actually supports the Windows fork function), and the addition of a new keyword, our, which handles global variables in the same way as my. Discussions have already started for Perl 6. Unlike Perl 5, which was a complete rewrite of Perl 4 and was developed and coded almost entirely by Larry, Perl 6 will have its feature set determined by the people that use it, through a series of RFCs (Requests for Comments). The language's core code will be developed by a team of programmers with input and assistance from Larry, and with features agreed upon by committees, rather than solely by Larry.

Perl History

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 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
Version Date Version Details
Perl 0 Introduced Perl to Larry Wall’s office associates
Perl 1 Jan 1988 Introduced Perl to the world
Perl 2 Jun 1988 Introduced Harry Spencer’s regular expression package
Perl 3 Oct 1989 Introduced the ability to handle binary data
Perl 4 Mar 1991 Introduced the first Camel book (Programming Perl, by Larry Wall, Tom Christiansen, and Randal L Schwartz; O’Reilly & Associates). The book drove the name change, just so it could refer to Perl 4, instead of Perl 3.
Perl 4.036 Feb 1993 The last stable release of Perl 4
Perl 5 Oct 1994 The first stable release of Perl 5, which introduced a number of new features and a complete rewrite.
Perl 5.005_02 Aug 1998 The next major stable release
Perl 5.005_03 Mar 1999 The last stable release before 5.6
Perl 5.6 Mar 2000 Introduced unified fork support, better threading, an updated Perl interpreter, and the our keyword

SEMrush Software