Broad Network


Perl Statement Modifiers and Simple Statements

Perl Syntax – Part 3

Perl Course

Foreword: In this part of the series I talk about Perl statement modifiers and simple statements.

By: Chrysanthus Date Published: 13 Jun 2015

Introduction

This is part 3 of my series, Perl Syntax. In this part of the series I talk about Perl statement modifiers and simple statements. You should have read the previous parts of the series before coming here, as this is a continuation.

Simple Statements
A declaration is a simple statement, e.g.

    my $var;

The print statement is a simple statement, e.g.

  print  "I love you\n";

There are other examples. In Perl a simple statement ends with a semicolon.

Any simple statement may optionally be followed by a SINGLE modifier and an expression, just before the terminating semicolon.

Statement Modifiers
The possible statement modifiers are depicted in the following syntaxes:

    EXPR1 if EXPR2;
    EXPR1 unless EXPR2;
    EXPR1 while EXPR2;
    EXPR1 until EXPR2;
    EXPR1 foreach LIST;
    EXPR1 for LIST;
    EXPR1 when EXPR2;

In each of the syntaxes, EXPR1 is what has to be done; then you have the modifier; and then you have EXPR2, which is a condition. EXPR2 does not necessarily have to be in parentheses. However, EXPR2 must result in true (that is 1) or false (that is 0).

I spend the rest of this tutorial explaining the modifiers with examples; at the end of the tutorial I explain when to use simple statements with modifiers. Note: for each simple statement, there has to be only one modifier.

The if Modifier
The syntax is:

    EXPR1 if EXPR2;

The syntax means, execute EXPR1 if EXPR2 is true. Consider the following code segment:

    my $boy = "I love you.";
    print "I love you too." if $boy;

If you try the code, the output will be, “I love you too.”. The string argument of the print function has been printed, because the expression after if is true. In the second statement, $boy is substituted with  "I love you.", which is not an empty string, resulting in true. Remember, a non-empty string is equivalent to true.

In some cases, the expression (EXPR2) after if may be a more complicated expression (still an expression). Even EXPR1 may also be a complicated expression.

The unless Modifier
The simple if statement syntax is:

    EXPR1 if EXPR2;

meaning execute EXPR1 if EXPR2 is true. What about the case where you want to execute EXPR1 if EXPR2 is false? No problem: Perl provides the unless modifier for that. unless means “if not”.

The simple unless statement syntax is:

    EXPR1 unless EXPR2;

meaning execute EXPR1 if not EXPR2; that is, execute EXPR1 if EXPR2 is false; that is, do not execute EXPR1 unless EXPR2 is true, so if it is false, execute it. The logic is not very convenient, but you just have to accept it. Try the following code:

use strict;

    my $boy = "";
    print "This is just text." unless $boy;

The output should be "This is just text.". So EXPR1 has been executed because EXPR2 is false. Remember, "" means false. Also always remember, that unless is the opposite of if.

The while Modifier
The syntax of the simple while statement is:

    EXPR1 while EXPR2;

meaning, repeat EXPR1 while EXPR2 is true; that is, repeat EXPR1 until EXPR2 becomes false. Try the following code:

use strict;

    my $i;
    print $i++ while $i <= 10;

The second statement is the declaration of the $i variable (with no assignment), and its value is undef. In the third statement, the print function is dealing with numbers, so undef assumes the value of 0. If it was dealing with strings, undef will assume the value of the empty string, i.e. "".

In the last statement, EPR1 is “print $i++” and EXPR2 is “$i <= 10”. So $i++ is repeated (incremented) while “$i <= 10” is true. The value of $i cannot go above 10, because at that time “$i <= 10” will be false.

If you tried the code, the output would have been, 012345678910, which is counting from zero (undef = 0) to 10.

For the simple while statement, the condition is evaluated first; that is EXPR2 is evaluated before EXPR1. For all the simple statements, the condition (EXPR2) is evaluated before EXPR1.

The until Modifier
The syntax of the simple until statement is:

    EXPR1 until EXPR2;

meaning, repeat EXPR1 while EXPR2 is false; that is, repeat EXPR1 until EXPR2 becomes true. until is the opposite of while. Try the following code:

use strict;

    my $i;
    print $i++ until $i > 10;

This code does the same thing like the previous one, counting from 0 to 10. The second statement is the declaration of the $i variable, and its value is undef. In the third statement, the print function is dealing with numbers, so undef assumes the value of 0. If it was dealing with strings, undef will assume the value of the empty string, i.e. "".

In the last statement, EPR1 is “print $i++” and EXPR2 is “$i > 10”. So $i++ is repeated (incremented) until “$i > 10” is true. The value of $i cannot go above 10, because at that time “$i > 10” will be true.

For the simple until statement, the condition is evaluated first; that is EXPR2 is evaluated before EXPR1 (same evaluation sequence as with while).

The foreach Modifier
The syntax of the simple foreach statement is:

    EXPR1 foreach LIST;

meaning, execute EXPR1 for-each item in LIST; In EXPR1, you can represent each item in the list with the special variable, $_ . Try the following code:

use strict;

    print "Hello " . $_ . "\n" foreach ('John', 'Peter', 'Mary');

The output is:

Hello John
Hello Peter
Hello Mary

Note that $_ has represented each item in the list. For EXPR1 you could still have had, "Hello $_\n", as in double quotes, $_ expands (is replaced by it value).

This code shows the use of the simple foreach statement.

The for Modifier
In Perl, for is a synonym for foreach. So the above code can be typed as:

use strict;

    print "Hello " . $_ . "\n" for ('John', 'Peter', 'Mary');

The when Modifier
The syntax of the simple when statement is:

    EXPR1 when EXPR2;

meaning execute EXPR1 when EXPR2 is true. when is like if but EXPR2 works with some variable. It should typically be used with the switch compound statement. If you are reading the series in this volume in the order given, then you should have come across the switch compound statement. Consider the following if-elsif-else compound statement, in a program:

use strict;

my $hisVar = 100;

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

The output is:

    Value is medium

The above code can be replaced by the following, where the simple when statement has been used.

use strict;

my $hisVar = 100;

use feature "switch";

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

The output is still the same:

    Value is medium

Note that before you can use the switch compound statement, you have to begin it with the statement, “use feature "switch";” . For each of the simple when statement, the number in parentheses has been compared with the variable, $hisVar to return true if they are the same or false if they are different. The parentheses can be omitted.

When to use a Simple Statement
I explain when to use a simple statement as opposed to a compound statement, in this section. Consider the following two code samples that do the same thing:

    my $boy = "I love you.";

    print "I love you too." if $boy;

and

    my $boy = "I love you.";

    if ($boy)
        {
         print "I love you too.";
        }

The second code segment has a block for the if-construct; it is a compound statement. The first code segment does not have a block for the if-construct. In the first code segment, what is evaluated (printed) is in front of if, but in the second code segment, what is evaluated is after if.

For any compound statement in Perl, you have to use a block. A block can have one or more statements. Note: if a block has only one statement, in Perl, the curly brackets must still be used.

So, use a simple statement when you would have only one statement in the block; otherwise use a compound statement.

That is it for this part of the series. We stop 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