Broad Network


Perl Compound Statement Syntaxes

Perl Syntax – Part 5

Perl Course

Foreword: In this part of the series, I talk about Perl compound statement syntaxes.

By: Chrysanthus Date Published: 13 Jun 2015

Introduction

This is part 5 of my series, Perl Syntax. In this part of the series, I talk about Perl compound statement syntaxes. A compound statement is an extension of the simple statement, but this time what is evaluated is after the statement modifier and not in front of it; and what is evaluated is in a block delimited by curly brackets. Also a compound statement may have one or more flow-control reserved words such as elsif. Compound statements are used to control the flow of a script (program). You should have read the previous parts of the series before reaching here, as this is a continuation.

Do not confuse between flow-control reserved words, control-flow reserved word, and loop-control reserved words. Flow control reserved words are else, elsif, when, given, unless, while, until, for and foreach. Control flow reserved words are, goto, redo, next, last, and continue. Loop-control reserved words are a subset of control-flow reserved words; they are redo, next and last.

Apart from if, all statement modifiers are flow control reserved words. In Perl, if is an operator.

Compound Statement Syntaxes
Compound statement syntaxes are:

if (EXPR) BLOCK
if (EXPR) BLOCK else BLOCK
if (EXPR) BLOCK elsif (EXPR) BLOCK ...
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
unless (EXPR) BLOCK
unless (EXPR) BLOCK else BLOCK
unless (EXPR) BLOCK elsif (EXPR) BLOCK ...
unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
given (EXPR) BLOCK
LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK
LABEL until (EXPR) BLOCK
LABEL until (EXPR) BLOCK continue BLOCK
LABEL for (EXPR; EXPR; EXPR) BLOCK
LABEL for VAR (LIST) BLOCK
LABEL for VAR (LIST) BLOCK continue BLOCK
LABEL foreach (EXPR; EXPR; EXPR) BLOCK
LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK
LABEL BLOCK
LABEL BLOCK continue BLOCK
PHASE BLOCK

Notice that each syntax here has a block as opposed to the simple statements. I will spend the rest of the tutorial explaining these syntaxes. I will talk only about the ones I consider conventional. I do not consider the unless and until compound statements as conventional, so I will not talk about them. However, in Perl, unless and until are useful simple statement modifiers (conventional in simple statements); I use them for simple statements and not for compound statements.

In all these syntaxes, EXPR is a condition. In a condition, you use == in place of =.

Another thing to note in Perl is that a block in these syntaxes must have curly brackets, even if it has zero or just one statement.

Notice that some of the syntaxes have LABEL and others do not. LABEL is for loop syntaxes. For each of these syntaxes that have LABEL, LABEL is optional. Even though a block is equivalent to a loop that is executed once, in Perl not all blocks are considered as a loop, especially blocks that return values as of the operator terms, do{}, sub{} and eval{}.

if Syntaxes
If you have been reading the parts of this volume in the order given, then you should have come across the if syntaxes. So, in this section I will just give examples.

A code example of the “if (EXPR) BLOCK” syntax is:

use strict;

my $hisVar = 20;

if ($hisVar == 20)
    {
        print "I am studying Perl, which has been optimized.";
    }

A code example of the “if (EXPR) BLOCK else BLOCK” syntax is:

use strict;

my $hisVar = 36;

if ($hisVar == 20)
    {
        print 'I am studying Perl';
    }
else
    {
        print 'I am doing something else';
    }

A code example of the “if (EXPR) BLOCK elsif (EXPR) BLOCK ...” syntax is:

use strict;

my $hisVar = 1000;

if ($hisVar == 10)
    {
        print 'Value is small';
    }
elsif ($hisVar == 100)
    {
        print 'Value is medium';
    }
elsif ($hisVar == 1000)
    {
         print 'Value is large';
    }

A code example of the “if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK” syntax is:

use strict;

my $hisVar = 10000;

if ($hisVar == 10)
    {
        print 'Value is small';
    }
elsif ($hisVar == 100)
    {
        print 'Value is medium';
    }
elsif ($hisVar == 1000)
    {
        print 'Value is large';
    }
else
    {
        print '$hisVar is very large';
    }

Here, the else block is the default block. That is, if the if-block or any of the elsif- blocks is not executed, the else block must be executed. For the “if (EXPR) BLOCK” and “if (EXPR) BLOCK elsif (EXPR) BLOCK ...” syntaxes, all the blocks may not be executed.

The given Syntax
The given syntax is:

    given (EXPR) BLOCK

Here, BLOCK may have other blocks within it. To use this feature, you have to begin the section with,

    use feature "switch";

Consider the following code:

my $hisVar = 10000;

if ($hisVar == 10)
    {
        print 'Value is small';
    }
elsif ($hisVar == 100)
    {
        print 'Value is medium';
    }
elsif ($hisVar == 1000)
    {
        print 'Value is large';
    }
else
    {
        print '$hisVar is very large';
    }

Notice that in each condition, $hisVar is compared with a value. This code can be replaced with:

given ($hisVar)
    {
        when (10)
            {
                print 'Value is small';
            }
        when (100)
            {
                print 'Value is medium';
             }
        when (1000)
            {
                print 'Value is large';
            }
        default
            {
                print '$hisVar is very large';
            }
    }

The given parentheses has the variable. Each of the when parentheses has the value for comparison. Here “default” is for “else” and like “else”, it has no condition.

The while Syntaxes
The while syntaxes are:

LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK

You should have come across the first syntax. A code example for this is:

    my $n = 0;

    SWEEPS: while ($n<5)
        {
            print $n; print "\n";
            ++$n;
        }

Here, “SWEEPS:” is a label and it is optional (it has not been used). The output is:

0
1
2
3
4

As for the second syntax, after the first block has been executed, if you want something else to be executed before the condition (EXPR) is evaluated again, you have to add the continue block. The second syntax can be rewritten as follows:

LABEL: while (EXPR)
    {
        # redo always comes here
        do_something;
    }
continue
    {
        # next always comes here
        do_something_else;
        # then back to the top to re-check EXPR
    }

So, with the presence of the continue block, the condition is assessed, if it holds, the while block is executed and the continue block is also executed. If the condition does not hold, neither the while block nor the continue block is executed.

So an iteration consists of both the while and the continue block. If for any iteration you do not want the continue block to be executed, place a conditional next command as the first statement in the continue block.

Note: The redo command in a while loop, restarts the loop block without evaluating the condition again.

The following is a simple example of the use of the continue block for the while loop:

use strict;

    my $n = 0;

    SWEEPS: while ($n<5)
        {
            print $n; print "\n";
            ++$n;
        }
    continue
        {
            print "That was for the number before $n.\n";

        }

The presence of “SWEEPS:” is optional here (it has not been used). The output is:

0
That was for the number before 1.
1
That was for the number before 2.
2
That was for the number before 3.
3
That was for the number before 4.
4
That was for the number before 5.

Note: The redo, next and last commands can be used in the while and continue blocks.

The LABEL BLOCK Syntax
The syntax is:

    LABEL BLOCK

You should have seen this in the previous part of the series. I repeat an example here:

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;
         }

The output is:

We are the world.
We are the children.
We are the world.
We are the children.

The LABEL BLOCK continue BLOCK Syntax
The syntax is:

    LABEL BLOCK continue BLOCK

Here, you have two blocks: the first block is the block of interest and the second block is an auxiliary block, which is just executed after the block of interest. The redo, next and last commands will affect the two block in one iteration.

Try the following code:

use strict;

    my $counter = 0;

    SWEEP: {
        $counter = $counter + 1;
        print "We are the world.\n";
        print "We are the children.\n";
     }
    continue
     {
        print "We are the ones to make a brighter day.\n\n";
        redo SWEEP if $counter == 1;
     }

The output is:

We are the world.
We are the children.
We are the ones to make a brighter day.

We are the world.
We are the children.
We are the ones to make a brighter day.

The PHASE BLOCK Syntax
I will talk about this syntax in the advanced course.

The for and foreach Syntaxes
I will talk about these syntaxes in the next part of the series.

And that is it for this part of the series. We take a break here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl 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

Comments

Become the Writer's Fan
Send the Writer a Message