September 2017
Beginner
402 pages
9h 52m
English
The temp operator temporary replaces a variable with a new value.
Consider the following example:
my $var = 1;f(); # 1{ f(); # 1 temp $var = 2; f(); # 2}f(); # 1sub f() { say $var;}
The f subroutine prints the value of the global variable $var. Initially, the value of the variable is 1. Thus, the first call of f prints 1.
Then, we have a block of code between a pair of curly braces. The second call of f is using the save value of $var as before. Then, the value of it is temporarily set to 2. So, the third call of f prints 2.
After exiting from the code block, the scope of the temporary value ends and the original value of $var is restored. So, the fourth call of f prints 1 again. The temp keyword is different from the ...