In Operators§
See primary documentation in context for prefix temp
sub prefix:<temp>(Mu 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 = "three";say ; # OUTPUT: «three»say ; # OUTPUT: «three»
You can also assign immediately as part of the call to temp:
temp = "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 = "original";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 = 0; # temp will "entangle" the global variable with the call stack# that keeps the calls at the bottom in order.sub f(*);sub g(*);print g(g(f(g()), g(), f()));# OUTPUT: «<g># <g># <f># <g># </g># </f># <g># </g># <f># </f># </g># </g>»