Perl Conditional Statements

Perl Conditional Statements are similar to other languages in the sense that it has plain if / elsif / else statements. However, the real difference of Perl conditional statements come from the implicit abilities of the statements. Here are some examples of Perl conditional statements.

Perl Boolean Logic

While talking about Perl conditional statements, it is also valuable to know how Perl boolean logic works. Unlike other languages, Perl does not have a specific boolean true or false value. Instead, based on the official Perl documentation here:

“The number 0, the strings ‘0’ and “” , the empty list () , and undef are all false in a boolean context. All other values are true. Negation of a true value by “!” or “not” operators returns a special false value. When evaluated as a string it is treated as “” , but as a number, it is treated as 0. Most Perl operators that return true or false behave this way.”

Write as you would read it

Calling conditional operators after an expression is very powerful because it is extremely helpful when reading the code. When you try to vocalize this construct of code it is usually in the form of “do something if this is true”.

Perl has a negative conditional statement named “unless”. Unlike the traditional “if” statement, “unless” execute expressions only when the conditional statement evaluates to “false”.

Inline True/False (Ternary) Operator

Like other languages, Perl has a ternary operator as well. This operator evaluates the conditional expression and based on the result returns the value in the true or false block.


Leave a Reply
