Perl | reset() Function

reset() function in Perl resets (clears) all package variables starting with the letter range specified by value passed to it. Generally it is used only within a continue block or at the end of a loop.

Note: Use of reset() function is limited to variables which are not defined using my() function.

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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";

chevron_right


Output:

var1 value = 20, var2 value = 15
var1 value = , var2 value =

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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";

chevron_right


Output:

variable1 value = 10,  variable2 value = 30
variable3 value = 40
variable1 value = ,  variable2 value = , 
variable3 value = 40

In the code given in Example 2, variable 3 is defined using my() function and hence, its value shows no effect of the reset() function whereas other variable’s values get reset.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.