Perl do-while Statement

Perl do-while statement is another looping construct provided by Perl language.

This statement is similar to while statement:

  • while statement checks the test condition before executing the block
  • do-while statement checks the test condition after executing the block.
In both cases, if the test condition is evaluated as false, the loop will end.

This free short tutorial will show you some useful ways to use the Perl do-while statement in Perl scripts.

Let’s look at the syntax form of this statement, first:

do BLOCK while (EXPR);

Before giving you some examples about how to use it, I want to point out some interesting features of Perl do-while statement:
  • The condition is evaluated after executing the block so the block will be executed at least once, unlike the while statement where if the condition is false initially, the block will be not executed at all
  • Don’t think of do as a loop block; here the keyword do means merely a term used in an expression rather than a loop block. And because the do block is not a looping block, you can’t use within the looping controls: next, last and redo
In the following, I’ll show you several examples about how to use the Perl do-while loop statement.

Example 1.

$count = 0;
do {
  print "Password: ";  
  chomp($psw = );
} while ++$count < 3 && $psw ne "1qaz"; 
if($psw eq "1qaz") {
  print "Password OK ";
} else {
  print "Wrong password\n";

A possible output for this snippet code is:

Password: 1
Password: 2
Password: 3
Wrong password

This code reads a password from standard input. The loop ends either after three iterations or after the input of the correct password.

Example 2.

do {
  print "$var ";
} while $var <= 5;
print "\n";
# it displays 1 2 3 4 5 6

At the first iteration, the $var scalar variable is undefined and ++$var will set its value to 1. The loop ends when $var becomes greater than 5.

If you don’ feel like testing the condition after the block by using Perl do-while statement, you can initialize an iteration variable and use while statement instead, in order to execute your block at least once. You can rewrite the previous example as follows:

$var = 0;
while ($var <= 5){
  print "$var ";
print "\n";

You’ll get the same output as before.

Example 3.

As I mentioned before, the do block doesn’t behave like a loop block, therefore you can’t use any of the control looping within the block. Unfortunately you may but the result after the execution of your code will be unpredictable. You know, sometimes Perl let you do a lot of things and if you do not do them in the appropriate way, it will take you a lot of time to debug and correct your script code.

But there is still a way to use the looping controls with the do construct. By instance, you can use the next and redo controls by putting a bare block inside the do block, like in the following snippet code:

my $a = 1; 
  # the do block
    # the bare block
    next if $a > 7;  
    print "$a ";
} while $a++ < 10;
print "\na = $a\n";

If you will run this code, you’ll get the output:

1 2 3 4 5 6 7
a = 11

The next looping control will reiterate the looping if $a > 7, skipping the rest of the statements from within the bare block.

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order

return to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!