In Operators§

See primary documentation in context for prefix temp

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

"temporizes" the variable passed as the argument. The variable begins with the same value as it had in the outer scope, but can be assigned new values in this scope. Upon exiting the scope, the variable will be restored to its original value.

my $a = "three";
say $a# OUTPUT: «three␤» 
{
    temp $a;
    say $a# OUTPUT: «three␤» 
    $a = "four";
    say $a# OUTPUT: «four␤» 
}
say $a# OUTPUT: «three␤»

You can also assign immediately as part of the call to temp:

temp $a = "five";

Be warned the temp effects get removed once the block is left. If you were to access the value from, say, within a Promise after the temp was undone, you'd get the original value, not the temp one:

my $v = "original";
{
    temp $v = "new one";
    start {
        say "[PROMISE] Value before block is left: `$v`";
        sleep 1;
        say "[PROMISE] Block was left while we slept; value is now `$v`";
    }
    sleep ½;
    say "About to leave the block; value is `$v`";
}
say "Left the block; value is now `$v`";
sleep 2;
 
# OUTPUT: 
# [PROMISE] Value before block is left: `new one` 
# About to leave the block; value is `new one` 
# Left the block; value is now `original` 
# [PROMISE] Block was left while we slept; value is now `original` 

In Variables§

See primary documentation in context for The temp prefix

Like my, temp restores the old value of a variable at the end of its scope. However, temp does not create a new variable.

my $in = 0# temp will "entangle" the global variable with the call stack 
            # that keeps the calls at the bottom in order. 
sub f(*@c{
    (temp $in)++;
     "<f>\n"
     ~ @c».indent($in).join("\n")
     ~ (+@c ?? "\n" !! "")
     ~ '</f>'
};
sub g(*@c{
    (temp $in)++;
    "<g>\n"
    ~ @c».indent($in).join("\n")
    ~ (+@c ?? "\n" !! "")
    ~ "</g>"
};
print g(g(f(g()), g(), f()));
 
# OUTPUT: «<g> 
#           <g> 
#            <f> 
#             <g> 
#             </g> 
#            </f> 
#            <g> 
#            </g> 
#            <f> 
#            </f> 
#           </g> 
#          </g>␤»