Perl Programming   «Prev  Next»
Lesson 2Perl Course Expectations
ObjectiveExplore key course features.

Perl Course and Key Features

You can download a compressed file that contains
  1. Any code you will need to complete all course exercises
  2. Sample code for the example programs explained in Module 6

Click the Resources button on the toolbar to download the compressed file.


Diagram Example

Whenever you see the MouseOver graphic below, a figure appear that explains or dissects several lines of Perl code follows it.

foreach loop nested
foreach loop nested within a while loop in Perl
This is shorthand for while(<STDIN>), which means "while there is input from the keyboard."

Syntax for while Loop in Perl

while (condition) {
    # code to be executed
}

You can replace "condition" with any expression that evaluates to a boolean value (i.e., true or false). The code within the curly braces will be executed repeatedly as long as the condition is true.
Example:
$i = 0;
while ($i < 5) {
    print $i;
    $i++;
}

Information Display of Perl

Every MouseOver and SlideShow has an accompanying Information Display for Perl. This allows you to look at the content presented. In this example MouseOver, you saw the following code fragment:

Perl while loop
Perl while loop
and were told that using while(<>) is shorthand for while(<STDIN>), which means while there is input from the keyboard.

Using While/Until Loops

The while statement has the general syntax of
while (expression)
block

The block is executed while the EXPRESSION is true.

my $i = 10;
while ( $i > 0 ) {
  if ( rand(3) > 2 ) {
    $i++;
  }
  else {
    $i--;
  }
  print $i,$/;
}

The previous code gradually lowers the value of $i until the expression $i > 0 evaluates as false. The main difference between while loops and for loops is that while loops iterate until a condition is false, whereas for loops iterate over a list. You commonly use the while loop in Perl with iterators, that is, the each() iterator for hashes.

What Is an Iterator?

The readline and glob functions, and the flip-flop operator, are all iterators when used in scalar context. A user-defined iterator usually takes the form of a code reference that calculates the next item in a list and returns it when executed.
When the iterator reaches the end of the list, it returns an agreed-upon value. While implementations vary, a subroutine that creates a closure around any necessary state variables and returns the code reference is common.
This technique is called a factory and facilitates code reuse.


A Slide Show is a Carousel that presents a series of images which you can traverse, either forward or backward. In this course, we will be using Slide Shows to explain certain aspects of the Perl language.
Perl grew out of the Unix programming community. Though it did not formally appear until the late 1980s, the technical components and motivations for Perl were developed in the two decades prior to that. Here are the main events in the "genealogy of Perl:
  1. 1969 Unix is created at Bell Labs
  2. 1977 awk is invented by Aho, Weinberger, and Kernighan
  3. 1978 "sh" shell is developed for Unix
  4. 1987 Perl is created
  5. 1995 (March) Perl 5.001 released, the most recent major version;
  6. as of this writing, Perl version 5.8.0 is the newest download at http://www.perl.com
The Unix philosophy of software construction, at least in the early days of that operating system, was to provide users with a large toolbox of useful "filters", programs that could do one small task well, and then compose a larger program from the smaller ones. The shell script notations sh and csh were the means by which composition was done; sed, awk, tr, and other programs were some of the more commonly used filters. Perl was developed ostensibly to solve a problem in text processing that awk was not good at and has continued to evolve from there.

Course glossary

Many of the terms used in the course are in a glossary. The terms from the glossary can be obtained from the following glossary link.

Quizzes and exercises

At regular intervals throughout the course, you will find exercises and multiple-choice quizzes. These learning checks will allow you to assess what you've learned and, if necessary, what to go back and review. Most of the exercises involve modifying or writing code and then submitting the code to your mentor for grading. If an exercise involves modifying code, you will be given "starter" code to work with. You can either cut and paste the code directly from the Web page or use the code included in the compressed download file.

SEMrush Software