Perl unless Statement

Perl unless Menu:

1. unless used as a modifier
2. General unless statement
    2.1. A simple unless statement
    2.2. unless-else
    2.3. unless-elsif-else
    2.4. How to use unless with the exists function

Perl unless statement represents an alternative to the conditional Perl if statement. Regardless the if statement which executes a block of code if a condition is true, the unless statement executes a block of code if the condition is false.

We can regard the unless statement like the negative or opposite of if. Sometimes it is useful to use unless instead of if, especially when you want to optimize your code or to make it more legible.

Because if and unless are very similar, we’ll not insist too much on this statement. This short tutorial will show you the main syntax forms of the Perl unless statement and it will point out some examples of how to use it in your script applications.

I think it is not a bad idea for you to consult my page dedicated to Perl if statement, first. You can use Perl unless statement either in connection with else and elsif clauses if your conditional statement has more than one branch, or as a modifier.

Now let’s look at the syntax forms available for this conditional statement.

1. unless used as a modifier


Perl will execute the above construction from right to left: the EXPR condition will be evaluated first and if the condition is false, Perl will continue the execution of the code with the STATEMENT that precedes unless.

If the EXPR condition is true, the Perl interpreter will skip to the following line of code. You can use this form when your conditional statement has no more than one branch. See the next example about how to use it:

# define an array
@colors = qw(blue green brown cyan white);
print "@colors\n" unless scalar(@colors) < 5;
# prints: blue green brown cyan white

If the @colors array has the number of elements greater than or equal to 5, this code will print the element of the array separated by space, otherwise the script will continue with the next lines of code.

By enclosing @colors array between double quotes, we tell Perl interpreter to interpolate the array and print its elements separated by space.

2. General unless statement

The second syntax form is more general:

unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK

  • EXPR represents a Boolean conditional expression
  • elsif and else clauses are optional and are used if you want to check a certain sequence of conditional expressions and find out which one of them is true
  • BLOCK consists of one or more statements enclosed in curly braces; in the construction of a block, the curly braces are always required.
We can split the general conditional Perl unless statement as follows:

2.1. A simple unless statement

unless (EXPR) BLOCK

If the EXPR condition will be evaluated false, Perl interpreter will execute the block, otherwise it will skip to the statement or declaration after the block. In this format we don’t use the else and elsif clauses. See the next snippet code:

$b = 0;
unless($b != 0) {
  $b += 1;  
print "b = $b\n";  # it outputs b = 1

Perl will evaluate the expression ($b != 0) and because it is false, it will execute the code between the curly braces of the block, i.e. it will increment the scalar variable $b by 1. The last line will print the value of $b.

2.2. unless-else

unless (EXPR) BLOCK else BLOCK

Perl will evaluate the conditional EXPR and if the result is false, it will continue with the execution of the block after the conditional expression, otherwise the script will continue with the block that follows the else clause.

Here is an example that finds the minimum of two numbers:

$a = 35;
$b = 22;
unless($a > $b) {
  print "min($a, $b) = $a\n";
} else {
  print "min($a, $b) = $b\n";
# it prints: min(35, 22) = 22

We can rewrite this code in a shorter way by using the (?) logical ternary operator - also called the conditional operator.

With the ternary operator, we can rewrite the above code like this:

$a = 35; $b = 22;
print "min($a, $b) = ", $a < $b ? $a : $b,"\n";

We’ll get the same output like in the previous example.

2.3. unless-elsif-else

unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK

If you have more than one branch in a conditional statement, you can use the Perl unless statement in connection with elsif clause that allows you to check as many conditional expressions as you need. You can finish the compound conditional statement using the else clause at the end of the form. See the next example:

$a = 12;
unless($a >= 0) {
  print "a is negative\n";
} elsif ($a == 0) {
  print "a is equal to 0\n";  
} else {
  print "a is positive\n";  
# it prints: a is positive

In this example, we used only one elsif clause, but generally there can be more.

As you have seen so far, the Perl unless statement is the reverse of the Perl if statement and you can use unless instead of if and vice-versa as you wish, in order to make your code more readable.

2.4. How to use unless with the exists function

To see this topic click here to watch a video on youtube where you'll find a complete example.

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 switch
    Perl while
    Perl do-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 from Perl unless to Perl Basics

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

Site Build It!