1. Perl
  2. Syntax
  3. here

unless modifier - postfix unless

You can add unless with the unless modifier .

Statement unless condition;

This is an example using the unless modifier. If $num is not defined, 1 is assigned to "$num".

$num = 1 unless defined $num;

There is a merit to use the unless modifier when it is a simple condition that can be written in one line. "Next unless condition" and "last unless condition" are often used.

Benefits of the unless modifier

The advantage of the unless modifier is performance over the regular unless statement. unless statement creates scope, while the unless modifier does not. Therefore, the unless modifier has better performance.

if modifier

There is an if qualifier that can be followed in the same way as the unless qualifier.

$num1 = 1 if defined $num1;

The if modifier is explained in detail below.


This is an example unless modifier.

use strict;
use warnings;

# Postfix unless
print "1: Example of postfix unless\n";
my $num1;
$num1 = 1 unless defined $num1;
print "\$num1 = $num1\n";

my $num2 = 10;
$num2 = 2 unless defined $num2;
print "\$num2 = $num2\n";


1: Example of postfix unless
$num1 = 1
$num2 = 10

Related Informatrion