Perl Compound Statement Basics
Perl Syntax – Part 4
Perl Course
Foreword: In this part of the series, I talk about the basics of Perl compound statements.
By: Chrysanthus Date Published: 13 Jun 2015
Introduction
Statement Modifiers
Statement modifiers, which you should have seen in the previous part of the series, are:
if
unless
while
until
for
foreach
when
Control Flow Reserved Words, which you will see in this part of the series, are:
redo
goto
next
last
continue
I explain these reserved words from this part of the series onward.
Basic Blocks
Generally, a block consists of zero or more statements delimited by curly brackets, also known as braces. The whole program file in Perl can also be considered as a block, delimited by the beginning and end of the file (without braces). Consider the following program:
use strict;
#some statements and constructs
{
print "We are the world.\n";
print "We are the children.\n";
}
#some more statements and constructs
The two print statements are in a block, which has no name or identification. If you run the program, the print functions will print their arguments. Notice the start and ending curly brackets. You must have seen blocks in for-loops and if-constructs. Consider the following program, which forms a file:
use strict;
#some statements and constructs
print "We are the world.\n";
print "We are the children.\n";
#some more statements and constructs
There are no curly brackets here. However, we still have a block, which starts from the beginning of the file to the end of the file.
A block is also equivalent to a loop that is executed once.
To identify a block, use a label as in the following program
use strict;
#some statements and constructs
SWEEP: {
print "We are the world.\n";
print "We are the children.\n";
}
#some more statements and constructs
Here, “SWEEP:” is a label. A label is a name of your choice that ends with a colon. In the code, the block of the two print statements have now been identified by a label.
A block as found in sub{} or do{}, which returns a value is not considered as a loop. A label is not used to identify these kinds of blocks (that return a value).
Blocks that return a value are of the constructs, sub{}, do{} and eval{}. sub{} is used to define functions. You will see the meaning of do{} below. You will see the meaning of eval{} later. Such blocks are not considered as loops in Perl.
redo
The redo syntax is,
redo LABEL
or
redo
It means re-execute the loop identified by LABEL. The following program illustrates this.
use strict;
my $counter = 0;
SWEEP: {
$counter = $counter + 1;
print "We are the world.\n";
print "We are the children.\n";
redo SWEEP if $counter == 1;
}
In this code, the block is executed two times. At the first pass through the block, the $counter is incremented by 1. At the end of the block for the first pass, the condition is true and the block is executed again. For the second pass, $counter is 2, the condition is false, and the block is not executed again. Note that the condition has == and not = . == and = are different operators.
Try the code, if you have not already done so, and notice that strings in the block are printed twice (in 2 sets).
In the absence of the label, the redo command re-executes the block in which it finds itself (innermost enclosing loop). In the presence of the label, when loops are nested, the redo command executes the block that has the redo’s label.
Note: in typing the label after the redo command, you do not type the colon (:).
goto
Consider the following program:
use strict;
my $counter = 0;
Again:
print "seen\n";
goto Stop if $counter == 2;
$counter=$counter + 1;
goto Again;
Stop:
If you try this program, the output will be:
seen
seen
seen
As the program is running through, the first time from the top, it meets the label, “Again:” and then it prints “seen”. The execution continues downward; it then meets the simple if statement; the condition is false and the expression on the left of if is skipped (not evaluated). Then the counter is incremented. Then it meets the statement, “goto Again;”. goto is a command, that tells the execution to go to the label and start re-executing from there and coming down. The redo command will not work here because redo has to be in a loop.
For the program, the looping execution continues until the if-condition is satisfied, executing what is on the left of if. The expression on the left of if in the simple statement, sends execution to the label, “Stop:” at the bottom of the script. From there, the execution goes downward and ends.
That is one way of using the goto command. The goto command can also be used to go to a function using the following syntax:
goto &NAME
This syntax is used inside a function block (sub{}), as redo should not be used inside a function block, since the block of a function is not considered as a loop. The following program illustrates the use of goto &NAME:
use strict;
my $counter = 0;
sub fn
{
$counter=$counter + 1;
print "seen\n";
goto &fn if $counter == 1;
}
fn();
The output is:
seen
seen
The first “seen” in the output is from the function call, fn(). The second “seen” is from the effect of “goto &fn” in the function definition.
The while loop has a block. The block here is a loop that is executed zero or more times. The while loop can be identified by a label. The following code illustrates this:
use strict;
my $n = 0;
SWEEPS: while ($n<5)
{
print $n; print "\n";
++$n;
}
The output is:
0
1
2
3
4
Note how the while loop has been identified in the code, by the label, “SWEEPS:” A label can be used to identify a special block like the while block, which has the name, while. A label can also be used to identify an anonymous (no name) block, as illustrated above.
Note: The redo command in a while loop, restarts the loop block without evaluating the condition.
next
In the above code, nothing was done with the label. The syntax of the next command is:
next LABEL
or
next
In a loop, next starts the next iteration ignoring the statements that are below it (within the loop). In nested loops, and if a label is typed just after next, the next iteration is the one with the label. If no label is typed after the next command, the next iteration is the one that has the next command(the innermost enclosing loop). The following code illustrates the use of the next command:
use strict;
my $n = 0;
SWEEPS: while ($n<=5)
{
++$n;
print "We are the world.\n";
next SWEEPS if $n>=2;
print "We are the ones.\n";
}
Read through the code. The output is:
We are the world.
We are the ones.
We are the world.
We are the world.
We are the world.
We are the world.
We are the world.
Note that “We are the ones.” is printed only once, because of the next command and its if condition.
The last command stops the execution of a loop (block). The statements below the command are not executed for that iteration, and the loop stops iterating. The syntaxes are:
last LABEL
last
If LABEL is typed after last, then the loop with the label is exited. If LABEL is absent, then the loop (innermost enclosing loop) containing the command is exited. The following code illustrates the use of the last command:
use strict;
my $n = 0;
SWEEPS: while ($n<=5)
{
++$n;
print "We are the world.\n";
last SWEEPS if $n==3;
print "We are the ones.\n";
}
Read through the code. The output is:
We are the world.
We are the ones.
We are the world.
We are the ones.
We are the world.
As you can see from the output, when $n is 3, “We are the ones.” is no longer printed; and “We are the world.” is printed for the last time.
The do{} Construct
The block of the do construct is not considered as a loop. The do{} construct is called an operation term. The do construct has to end with a semicolon; that is,
do{};
You can attach a while condition at the end of the do construct to have a kind of while loop; that is:
do{}while (condition);
In this case the block iterates at least once. Even with the while condition, the do{} construct (or its block) is not still a loop.
Like sub{} for function, the do{} construct can return a value. The value returned, is the value of the last statement in the block. When a value is returned, receiving it is optional. The presence of the while condition would prevent the return of the value.
The syntaxes for the do{} construct are:
do BLOCK
do EXPR
The following two programs illustrate the use of the do BLOCK syntax:
use strict;
my $var = do
{
my $variab;
$variab = 7;
};
print $var;
use strict;
my $n = 0;
do
{
print $n; print "\n";
++$n;
} while ($n<5);
In the first program, the whole do{} construct is assigned to the scalar, $var; and so the returned value is held by $var. Note that the do{} constructs ends with a semicolon. The second program has a kind of while construct, still ending with a semicolon.
For the “do EXPR” syntax, EXPR is a filename in quotes. This syntax executes the file as a Perl program. Remember, a file is a kind of block, delimited by the beginning and end of the file (not curly brackets). The value of the last statement in the file is returned. This is like calling a function, where all the content of the function is a file. Assume that you have a file in the working directory, called temp.pl. In the current program, you can have:
my $var = do "temp.pl";
print $var;
The value of $var is the value of the last statement in the file.
LOOP Control Statements
Loop control statements or commands are, redo, next and last. These commands are used inside a loop block. They are not used outside of the loop. These commands cannot be used in the do{}, sub{} or eval{} blocks. If you want the redo effect within a function definition (sub{}), use goto.
Time to take a break. Let us stop here and continue in the next part.
Chrys
Related Links
Perl BasicsPerl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT