Control flow
if / unless / with
FullBoolean 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
bigunless #
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
okwith / 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 valueNotes #
- All of these are expressions in statement position; for a value-returning choice inside an expression use the ternary
?? !!. - The postfix forms —
say $x if $cond,... unless ...,... with ...— read well for one-line guards. with 0 { }runs:0is defined even though it is false. That is the whole point ofwithversusif.