Conditions and comparison

if, elsif, else

The condition sits in parentheses and the block in braces. Braces are always required, even for one line, which removes the dangling-else bugs that other languages allow.

my $load = 85; if ($load >= 90) { print "critical\n"; } elsif ($load >= 80) { print "warning\n"; } else { print "ok\n"; }

perl demo.pl

warning

Note the spelling: elsif, not elseif and not else if. Two words also work, but the single word keeps the chain visibly one statement.

unless

unless is if with the test reversed. It reads better when the normal path is the empty one, such as bailing out on a missing argument.

The statement modifier form puts the condition after the action, which suits short guards:

my $file = shift @ARGV; die "usage: prog FILE\n" unless defined $file; print "processing $file\n" if -f $file;

perl demo.pl /etc/hostname

processing /etc/hostname

Do not stack modifiers. One condition after the statement is readable; two are a puzzle, so rewrite them as a full if block.

The ternary operator

When both branches only produce a value, the ternary operator keeps it to one line. It takes three parts: test, value when true, value when false.

my $count = 1; my $label = $count == 1 ? "item" : "items"; print "$count $label\n"; # 1 item

perl demo.pl

1 item

Chain ternaries only when every branch is a plain value and the list stays short. Otherwise use if and elsif, which the next maintainer will read faster.

and, or, not

Perl has two families of logical operators that differ only in precedence. The symbolic ones, &&, || and !, bind tightly and work well inside expressions. The word forms, and, or and not, bind so loosely that they sit below assignment.

my $count = 1; my $ok = $count > 0 && $count < 10; print "in range\n" if $ok; open my $fh, "<", "/etc/hostname" or die "open failed: $!\n";

perl demo.pl

in range

That last line is the idiom you will see everywhere: with or, the die belongs to the whole open statement. Written with ||, the same code would bind to the file name and always succeed silently.

What is true

Perl has exactly five false values: the number 0, the string "0", the empty string, an empty list, and undef. Everything else is true, including the strings "00" and "0.0", and the string "false".

Test for emptiness with length or defined when a value may be text that begins with zero:

my $answer = "0"; print "false\n" unless $answer; # prints false print "empty\n" unless length $answer; # does not print

perl demo.pl

false

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why are the braces of an if block never optional in Perl?

    Answer

    Because the language has no single-statement form, which removes the dangling-else bugs other languages allow

    Every block has braces, so there is no ambiguous if without an else; a single line is written with a statement modifier instead.

  2. How is the middle branch of an if chain spelled?

    Answer

    elsif

    The one-word form is elsif; else if also works as two statements, but the chain reads as one only with elsif.

  3. What does unless ($ok) mean?

    Answer

    if (!$ok)

    unless is if with the test reversed, and it reads best when the empty path is the normal one.

  4. Why does open my $fh, "<", $file or die "open failed: $!"; work, while || would not?

    Answer

    Because or binds below the assignment, so it tests the whole open call

    || binds tighter than the comma of the argument list, so it would test the file name and the die would never run.

  5. Which of these values is true in Perl?

    Answer

    "0.0"

    The false values are 0, "0", the empty string, an empty list and undef; a longer string that begins with zero is true.

  6. What is the advice about statement modifiers such as print ... if ...?

    Answer

    One condition after a statement is readable; two are a puzzle, so use a full if block instead

    The modifier form suits a short guard, and stacking conditions hides the order they run in.

  7. What does this program print? my $load = 95; if ($load >= 90) { print "critical\n"; } elsif ($load >= 80) { print "warning\n"; } else { print "ok\n"; }

    Answer

    critical

    The first test is true, so the chain stops there and the other branches never run.

  8. What does this program print? my $count = 2; my $label = $count == 1 ? "item" : "items"; print "$count $label\n";

    Answer

    2 items

    The count is not 1, so the ternary takes the second value and the string prints both numbers and the label.

  9. What does this program print? my $answer = "00"; print $answer ? "true\n" : "false\n";

    Answer

    true

    Only the string "0" is false; "00" is longer, so the condition is true.

  10. What does this program print? my $v = "0"; print "zero\n" if $v; print "text\n" if length $v;

    Answer

    text

    The string "0" is false, but it has a length of one, which is true; length is how you test a value that may be "0".

Article author: Arthur Isaev