1. Perl
  2. Syntax
  3. here

unless statement

Use unless to describe what to do if the condition is not met.

# unless
unless (condition) {
  # What to do if the conditions are not met
}

This is an example unless. This is the process to terminate the program when the conditions are not met.

# Exit the program if the conditions are not met
my $num;
unless (defined $num) {
  die "The number must be defined";
}

It can also be written using if statement and the negation operator. Perl tends to prefer unless.

# Expressed using if statement and negation operation
if (! defined $num) {
  die "The number must be defined";
}

You can also add unless.

# Postfix unless
die "The number must be defined" unless defined $num;

Related Informatrion