2.3.2 Explicit Block of Statements

A group of sequential statements can be explicitly structured to belong to a block. A block of statements is a sequence of statements enclosed inside braces. The { and } are the front-side and back-side fences delimiting the boundary of a block. We can modify the program given above to contain an explicit block. The modified program is given below.
Program 2.8

#!/usr/bin/perl
#file temp2.pl

use strict vars;
my ($fahrenheit, $celsius);

{
$fahrenheit = 50;
$celsius = 5/9 * ($fahrenheit - 32);
printf "%4.0fF = %6.1fC\n", $fahrenheit, $celsius;
}

Here, we see an explicit block of statements surrounded by { and }. From the point of usefulness, this block is not really necessary. It does not change the behavior of the program. The output is the same as that of the program in Section 2.3.1. The statements in the explicit block are executed one by one in sequential order, just like in an implicit block. Although we see only one explicit block in this program, the whole program itself is an implicit block although it is not delimited by { and } at the top and the bottom, respectively. Therefore, there are two blocks in this program, the outer implicit block containing the inner explicit one. The variables declared with my in the outer implicit block are available throughout the outer block or the file, including any contained blocks.
A block such as the one above is called a bare block. A bare block is considered a simple variant of more complex loop statements (a type of compound statement) that executes only once. Perl allows one to place names or labels before a loop statement. Thus, a bare block can be labeled. We discuss loop statements and labels later in the chapter. Bare blocks are usually not used unless they have a label and used as a variant of a loop statement using loop jump operators such as redo. We see jump operators later in the chapter.