Control flow
Statement modifiers
FullPostfix if / unless / for / while — a whole control structure after the statement.
A statement modifier attaches a condition or loop to the end of a single statement — no block, no braces. It reads like English and is the idiomatic form for one-liners.
Postfix if / unless #
say "even" if 4 %% 2; say "odd" unless 4 %% 2;
Output
evenThe unless line produces nothing because 4 %% 2 is true.
Postfix for #
A trailing for runs the statement once per element, with $_ set — so the .method shorthand pairs naturally with it.
say $_ for 1..3; .say for <a b>;
Output
1
2
3
a
bPostfix while / until #
my $i = 0; $i++ while $i < 3; say $i;
Output
3Notes #
- Modifiers don't nest and take no
else— reach for the block forms (if / unless / with, loops) when you need more than one clause. - The postfix
fortopicalises$_, exactly like the blockfor, so.say for @itemsis the compact "print each" idiom. - Precedence is loose, so the whole statement to the left is the body:
say $_ * 2 for 1..3doubles each element.