In Control flow§
See primary documentation in context for unless
When you get sick of typing "if not (X)" you may use unless
to invert the sense of a conditional statement. You cannot use else
or elsif
with unless
because that ends up getting confusing. Other than those two differences unless
works the same as if:
unless 1 { "1 is false".say } ; # does not say anything, since 1 is true
unless 1 "1 is false".say ; # syntax error, missing block
unless 0 { "0 is false".say } ; # says "0 is false"
unless 42.say and 1 { 43.say } ; # says "42" but does not say "43" 43.say unless 42.say and 0; # says "42" and then says "43" 43.say unless 42.say and 1; # says "42" but does not say "43" $_ = 1; unless 0 { $_.say } ; # says "1" $_ = 1; unless 0 -> $_ { $_.say } ; # says "0" $_ = 1; unless False -> $a { $a.say } ; # says "False" my $c = 0; say (1, (unless 0 { $c += 42; 2; }), 3, $c); # says "(1 2 3 42)" my $d = 0; say (1, (unless 1 { $d += 42; 2; }), 3, $d); # says "(1 3 0)"