prefix let

Documentation for prefix let assembled from the following types:

language documentation Variables

From Variables

(Variables) prefix let

Restores the previous value if the block exits unsuccessfully. A successful exit means the block returned a defined value or a list.

my $answer = 42;
 
{
    let $answer = 84;
    die if not Bool.pick;
    CATCH {
        default { say "it's been reset :(" }
    }
    say "we made it 84 sticks!";
}
 
say $answer;

In the above case, if the Bool.pick returns true, the answer will stay as 84 because the block returns a defined value (say returns True). Otherwise the die statement will cause the block to exit unsuccessfully, resetting the answer to 42.

language documentation Operators

From Operators

(Operators) prefix let

sub prefix:<let>(Mu $a is rw)

Refers to a variable in an outer scope whose value will be restored if the block exits unsuccessfully, implying that the block returned a defined object.

my $name = "Jane Doe";
 
{
    let $name = prompt("Say your name ");
    die if !$name;
    CATCH {
        default { say "No name entered" }
    }
    say "We have $name";
}
 
say "We got $name";

This code provides a default name for $name. If the user exits from the prompt or simply does not provide a valid input for $name; let will restore the default value provided at the top. If user input is valid, it will keep that.