Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of ‘if’ versus ‘unless’ for Perl conditionals

People also ask

How do I use unless in Perl?

Syntax. If the boolean expression evaluates to false, then the block of code inside the unless statement will be executed. If boolean expression evaluates to true then the first set of code after the end of the unless statement (after the closing curly brace) will be executed.

What are conditional statements in Perl?

Perl conditional statements helps in the decision making, which require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the ...


In Perl Best Practices, the advice is to never use unless. Personally, I think that's lunacy.

I use unless whenever there's a simple condition that I would otherwise write as if( ! ... ). I find the unless version to be more readable, especially when used as a postfix:

do_something() unless $should_not_do_that;

I recommend avoiding unless anytime things get more complicated, such as when you will have elsif or else blocks. (Fortunately, or perhaps unfortunately, depending on your perspective, there is no elsunless)

Also, any time a conditional is a complex expression made up of other booleans. For example,

unless( $foo and !$bar )

Is pretty damn confusing, and does not have any advantage over the equivalent if.


Aside from one esoteric case1unless is just syntactic sugar for if !. It exists to allow you to write code that is clearer and more expressive. It should be used when it achieves this goal and shunned when it impairs it.

I find unless to be most useful for flow control in loops. e.g.

while (<$fh>) {
    next unless /\S/;
    # ...
}

For simple negations I find it clearer than a negated if -- it's easy to miss that leading ! when reading code.

unless ($condition) {
    do_something();
}

if (!$condition) {
    do_something();
}

But don't write unless ... else, because that's just jarring.

In postfix form it provides a hint about what the expected path through the code is.

do_normal_thing() unless $some_unlikely_condition;


1) The last expression evaluated is different, which can affect the behavior of subs without an explicit return.

A rule of thumb is that 'unless' should probably be used infrequently.

It is particularly useful in the postfix form, e.g.:

delete_old_widgets() unless $old_widget_count == 0

Situations where you should never use unless:

  • with a compound condition (and, or, not)
  • with an else clause

I spent an hour recently trying to explain to someone how two nested 'unless' clauses worked, it was difficult to decypher without having to invert them into if statements with boolean logic.

If you try to convert it into English, it helps to guide you.

A simple unless works fine. For example.

'Unless you are quiet, I am going to ignore you'.

unless ($quiet) {
    ignore();
}

Although I think this works equally well

'If you are not quiet, I am going to ignore you'

if (not $quiet) {
    ignore();
}

when it starts getting complicated is when you have negation.

'Unless you are not noisy, I am going to ignore you'

unless ( ! $noisy) {
    ignore();
}

Far better written as

'If you are noisy, I am going to ignore you'

if ($noisy) {
    ignore();
}

So, don't use an 'unless' if you also have a negate.

Also don't use 'unless else'

unless ($quiet) {
    ignore();
}
else {
    give_a_sweet();
}

'Unless you are quiet, I will ignore you, otherwise I will give you a sweet'

Change it by inverting the condition.

if ($quiet) {
    give_a_sweet();
}
else {
    ignore();
}

'If you are quiet, I will give you a sweet, otherwise I will ignore you'.

with more than one condition, it get's messy.

unless ($quiet and not $fidgit) {
    punish();
}

'Unless you are quiet and you don't fidgit, I will punish you'.

(sorry my comprehension is failing here!)

again, negate it.

if (not $quiet or $fidgit) {
    punish();
}

'If you are not quiet, or you fidgit, I will punish you'.

the problem with using 'unless' even for the simplest cases, they often (by yourself or by outhe

I hope that makes it clear when you should, or should not, use unless?

(unless you don't have another opinion?)