Control flow

if / unless / with

Full

Boolean branching with if/elsif/else and unless; definedness branching with with.

Conditionals choose a block to run. if branches on truth, unless on its negation, and with on definedness (rather than truth), topicalising the tested value.

if / elsif / else #

my $n = 7;
if $n > 5 {
    say "big";
}
elsif $n > 0 {
    say "small";
}
else {
    say "non-positive";
}
Output
big

unless #

unless runs its block when the condition is false. It takes no else — reach for if when you need both branches.

my $x = 0;
unless $x {
    say "falsy";
}
say "ok" if $x == 0;
Output
falsy
ok

with / without #

with runs when its argument is defined (not just true) and sets $_ to it; without is the complement. This is the clean way to handle "maybe a value".

my $v = 42;
with $v {
    say "defined: $_";
}
without Any {
    say "no value";
}
Output
defined: 42
no value

Notes #