control flow when

Documentation for control flow when, assembled from the following types:

language documentation Control Flow

From Control Flow

(Control Flow) control flow when

The when block is similar to an if block and either or both can be used in an outer block, they also both have a "statement modifier" form. But there is a difference in how following code in the same, outer block is handled: When the when block is executed, control is passed to the enclosing block and following statements are ignored; but when the if block is executed, following statements are executed. (Note there are other ways to modify the default behavior of each which are discussed in other sections.) The following examples should illustrate the if or when block's default behavior assuming no special exit or other side effect statements are included in the if or when blocks:

    if X {...} # if X is true in boolean context, block is executed 
    # following statements are executed regardless 
    when X {...} # if X is true in boolean context, block is executed 
                 # and control passes to the outer block 
    # following statements are NOT executed 

Should the if and when blocks above appear at file scope, following statements would be executed in each case.

There is one other feature a when has that if doesn't: the when's boolean context test defaults to $_ ~~ while the if's does not. That has an effect on how one uses the X in the when block without a value for $_ (it's Any in that case and Any smart matches on True: Any ~~ True yields True). Consider the following:

    my $a = 1;
    my $b = True;
    when $a    { say 'a' }# no output 
    when so $a { say 'a' }  # a (in "so $a" 'so' coerces $a to Boolean context True 
                            # which matches with Any) 
    when $b    { say 'b' }# no output (this statement won't be run) 

Finally, when's statement modifier form does not effect execution of following statements either inside or outside of another block:

say "foo" when X# if X is true statement is executed 
                  # following statements are not affected