Documentation for prefix temp

Documentation for prefix temp, assembled from the following types:

language documentation Operators

From Operators

(Operators) 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␤» 

Note that you can also assign immediately as part of the call to temp:

temp $a = "five";

language documentation Variables

From Variables

(Variables) prefix temp

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>␤»