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

Hi Masters,

Any leads to reset my $var Please help.

#!/usr/bin/perl -w my $var = 10; $van = 5; print "Var value =$var, Van value =$van\n"; # Now reset all variables who name starts with 'v' reset('v'); print "Var value =$var, Van value =$van\n";

Replies are listed 'Best First'.
Re: reset function
by davido (Cardinal) on Nov 20, 2013 at 23:43 UTC

    You are creating two types of variables:

    my $var = 10; # This is a lexical variable. $van = 5; # This is a package global.

    If you were dealing only with lexicals, you could do this:

    { my $var = 10; my $van = 5; print "\$var = $var; \$van = $van\n"; } # Here, $var and $van don't exist.

    But you're talking about resetting just those variables that start with 'v'. Don't think that way. Think in terms of using a hash:

    use strict; use warnings; my %hash; $hash{var} = 10; $hash{van} = 5; print "var = $hash{var}; van = $hash{van}\n"; delete hash{$_} for grep { /^v/ } keys %hash; print "var = $hash{var}; van = $hash{van}\n";

    If you don't want to delete the keys, you could also just do this:

    $hash{$_} = undef for grep { /^v/ } keys %hash;

    Dave

Re: reset function
by LanX (Saint) on Nov 21, 2013 at 00:06 UTC
    Pretty sure you don't know what you are doing (see XY Problem)

    But FWIW:

    use strict; use warnings; my $var = 42; # lexical var our $van = 42; # package var { my $var = 10; local $van = 5; print "Var value =$var, Van value =$van\n"; } # Leaving block "resets" scoped variables print "Var value =$var, Van value =$van\n";

    thats the Perl way to realize "resets".

    But we normally just use different namespaces/scopes or just entries of a hash %v.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: reset function
by davido (Cardinal) on Nov 21, 2013 at 00:31 UTC

    Of course just because it's clumsy, buggy, error prone, and just plain stupid, doesn't mean it's impossible:

    use strict; use warnings; our $var = 10; our $keep = 20; print "\$var = $var\n"; print "\$keep = $keep\n"; print "Reset <<\$$_>>\n" for dangerous_reset(qr/^v/); print "\$var = $var\n"; print "\$keep = $keep\n"; sub dangerous_reset { my $criteria = shift; my $package = (caller)[0] . '::'; { no strict 'refs'; my @wanted = grep { /$criteria/ # Meets cr +iteria && defined *{$package . $_}{SCALAR} # Has scal +ar element && /^[\p{alpha}_]\w*$/ # Isn't sp +ecial. } keys %{$package}; undef ${*{$package . $_}{SCALAR}} for @wanted; return @wanted; # Return count, or list of variables removed. } }

    We had a troll here a few years ago who kept pushing some strategy he had for resetting variables programatically. I'm sure his kludge was a lot better thought out than the code I've shown here, but it was still a terrible, bug-prone, broken sledge-hammer of a tool. It's just not how things should be done.


    Dave

Re: reset function
by Eily (Monsignor) on Nov 20, 2013 at 23:54 UTC

    You surely don't want to do that. That looks like a very bad idea.

    If you're still there, you can change the SCALAR portion of all GLOBs in the symbol table of your current package which start with a v. That's easy, you just have to look inside a hash. This will reset all variables starting with a v that are not lexically scoped, ie: for which you don't have a my.

    For the lexicals, there is the module PadWalker, peek_my(0) will give you a list of visible lexical variables in a hashref. How wonderful :).

    Good luck with that crazy idea.

Re: reset function
by BrowserUk (Patriarch) on Nov 21, 2013 at 00:40 UTC

    Why do you want to reset variables that start with v? What purpose does this serve for you?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: reset function (never)
by Anonymous Monk on Nov 21, 2013 at 01:57 UTC
      Once in a while I repeat saying something like

      "Perl doesn't stop to surprise me!"

      And I always hope the intervals between these moments will get bigger...

      But now... oh man ... this reset builtin is really scary!

      DB<132> @x=(4,2);$x=42; $y=0; while ($y++<3) { $x=$y; print "$x -" ; reset('x') if $y>1 ; print " $x : @x\n"; } 1 - 1 : 4 2 2 - : 3 - :

      IMHO it doesn't "reset" at all, it just deletes all variables of the glob *x without deleting the glob.

      Only delete $main::{'x'} is more radical...

      o_O WTF !?!

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      update

      Me thinks this must be a relict of one-liner hacking. Anything else is just to dangerous..

        WOW!! WTF, indeed! I can understand something that would reset a match, but variables that start with a given letter? Can someone provide a use-case for this that makes sense?

        It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.