Controlling Program Flow in Perl
if
and the logical operators, are used to control whether portions of the program run, depending on true or false values. What You'll Learn in This Hour:
- Block statements
- Operators
- Looping
- Labels
- Exiting Perl after a program execution
In Hour 2, "Perl's Building Blocks: Numbers and Strings," you learned about statements, operators, and expressions. All the examples in that hour had one thing in common: All the statements were executed in order from top to bottom and were executed only once.
One of the reasons that you use computers is that computers are very good at performing repetitive tasksover and over againwithout getting tired or bored and without developing carpal tunnel syndrome. So far, you haven't had any way of telling Perl to "do this task X times" or to "repeat this task until it's done." In this hour, you will learn about Perl's control structures. Using them, you can group statements into something called a statement block and run the group of statements repeatedly until they've done what you want.
The other chore that computers excel at is making decisions quickly. It would be tiresomenot to mention sillyif a computer had to ask you every time it made a decision. The very act of retrieving and reading your email causes your computer to make millions of decisions that you really don't want to deal with: how to assemble network traffic, which colors to make each pixel on your screen, how your incoming mail should be pulled apart and displayed, what should be done when your mouse cursor moves even a tiny bit, and countless others. All these decisions are made up of other decisions, and some of them are made thousands of times per second. In this hour, you will learn about conditional statements. Using these statements, you can write blocks of code that will be executed or not, depending on decisions made in your Perl program.
Blocks
The fundamental way to group statements in Perl is the block. To group statements in a block, just surround the statements with a matched set of curly braces, as shown here:
{ statement_a; statement_b; statement_c; }
Within the block, statements execute from the top down, as they have until now. You can have other, smaller blocks of statements nested within a block, as you can see here:
{ statement_a; { statement_x; statement_y; } }
The format of the block, like the rest of Perl, is free-form. The statements and curly braces can be on one line or on several lines, as shown here, and with any kind of alignment you want, as long as you always have a matched set of curly braces:
{ statement; { another_statement; } { last_statement; } }
Although you can arrange blocks any way you would like, programs can be hard to read if they’re just thrown together. Good indenting, although not necessary for Perl to understand your code, makes for human-readable Perl. It can help you keep track of your program’s logic.
Blocks that occur by themselves within a program are called bare blocks or naked blocks. Most of the time, however, you will encounter blocks attached to other Perl statements.