珀尔 | reset()函数
Perl 中的 reset()函数重置(清除)从传递给它的值指定的字母范围开始的所有包变量。通常,它仅在 continue 块内或循环结束时使用。
注意: reset()函数的使用仅限于未使用 my()函数定义的变量。
Syntax: reset(letter_range)
Parameter:
letter_range: a range of letters that begin with a common character or a set of characters
Returns:
1, and resets all the variable in the given range
示例 1:
#!/usr/bin/perl -w
$var1 = 20;
$var2 = 15;
# Values before reset() function call
print "var1 value = $var1, ",
"var2 value = $var2\n";
# Now reset all variables whose
# name starts with 'v'
reset('v');
# Values after reset() function call
print "var1 value = $var1, ",
"var2 value = $var2\n";
输出:
var1 value = 20, var2 value = 15
var1 value = , var2 value =
示例 2:
#!/usr/bin/perl -w
$variable1 = 10;
$variable2 = 30;
# defining private variable using my()
my $variable3 = 40;
# Values before reset() function call
print "variable1 value = $variable1, ",
" variable2 value = $variable2\n";
print "variable3 value = $variable3\n";
# Now reset all variables whose
# name starts with 'v'
reset('v');
# Values after reset() function call
print "variable1 value = $variable1, ",
" variable2 value = $variable2, \n";
print "variable3 value = $variable3";
输出:
variable1 value = 10, variable2 value = 30
variable3 value = 40
variable1 value = , variable2 value = ,
variable3 value = 40
在示例 2 中给出的代码中,变量 3 是使用my()函数定义的,因此,它的值不会显示reset()函数的影响,而其他变量的值会被重置。