abubacker has asked for the wisdom of the Perl Monks concerning the following question:

Dear all ,

$var = 5 ; $vvv =500 ; reset 'v' ;
it will reset both $var and $vvv ,
I want to reset $var alone not $vvv
Is it possible , can we reset a particular variable or can we give any pattern to mention a particular variable

Please help me out!

Replies are listed 'Best First'.
Re: reset particular variable
by ig (Vicar) on Aug 19, 2009 at 05:59 UTC

    You could use undef, as either undef $var; or $var = undef;

Re: reset particular variable
by james2vegas (Chaplain) on Aug 19, 2009 at 05:37 UTC
    What is the need you have to reset variables? You could use local for your package variables (since reset only works for package variables, not lexicals) and put the code that changes $var in a block. You could also use lexicals and use my;
    our $var = 42; our $vvv = 'abc'; { local $var = 500; print "$var $vvv\n"; } print "$var $vvv\n";
    or
    my $var = 42; my $vvv = 'abc'; { my $var = 500; print "$var $vvv\n"; } print "$var $vvv\n";
Re: reset particular variable
by rovf (Priest) on Aug 19, 2009 at 08:49 UTC

    In addition to what ig said, I would like to point out that, by penalty of eternal damnation, any use of reset EXPR in a Perl program is only permitted if accompanied by maniac laughter during coding.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: reset particular variable
by Marshall (Canon) on Aug 19, 2009 at 11:13 UTC
    I was amazed that there is indeed such a thing as "reset". That clears all package scope variables matching a pattern! Whoa!

    So the first thing here is that you shouldn't be using package variables in modern Perl.

    Nevertheless, usually setting $var=0, or $var=undef is enough! Those are "false" values. In Perl "undef" is actually a value. To set $var to absolutely nothing: $var=(); Many folks will write Perl for years without understand that trick.

    Curiosity gets the better of me...why would you even think of using reset? Can you post some code where you think that it is required to use "reset"?

      In Perl "undef" is actually a value. To set $var to absolutely nothing: $var=();
      I don't understand. Can you show an example in which the results of the  $var = (); and  $var = undef; and  undef $var; statements would differ?

      Would there ever be an advantage (other than obfuscation) to writing  $var = (); instead of  undef $var;?

        This () stuff does have VERY practical uses!

        If you have a subroutine and you want to return "nothing", meaning not "undef", not "zero", I mean absolutely "nothing", what do you do?

        This little gem, is on page 136 of "Effective Perl". It passes without even a comment!! Whoa!

        Lets say that we have:
        @output = map{zoomy_function}@input;

        Map{} is a critter that processes each @input and spews out an @output (map{} is a transformation operator).

        But, what would happen if for example the zoomy_function within map{} decides that it wants to "throw a value away". How do you do that? In Perl you can't even just exit a sub. The value of the last statement in a Perl sub is gonna get returned. What is that return value gonna be? "undef","0","-1"?

        In Perl, you can return (), which means that NOTHING is returned! Something can just come into the func and no result ever comes out. The "black hole" of map().