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

Perl 5.10 on MacBook 10.6.8 (Snow Leopard).

Is there a way to auto-save and restore global variables, such as those found in Data::Dumper?

I envisage something like:

use Data::Dumper; $Data::Dumper::blah = 0; # Set defaults $Data::Dumper::fred = 'xyz'; ... if ($i_am_debugging_this_bit) { SAVE(Data::Dumper); $Data::Dumper::blah = 1; # Override these for a bit $Data::Dumper::fred = 'nerd'; ... RESTORE(Data::Dumper); # Put them all back }

Replies are listed 'Best First'.
Re: Can I auto-save and restore global variables?
by McA (Priest) on Jul 24, 2014 at 21:47 UTC

    Hi,

    have a look at perldoc -f local. I'm not sure whether you want the following:

    use Data::Dumper; $Data::Dumper::blah = 0; # Set defaults $Data::Dumper::fred = 'xyz'; ... if ($i_am_debugging_this_bit) { local $Data::Dumper::blah = 1; # Override these for a bit local $Data::Dumper::fred = 'nerd'; ... }

    This should do what you want.

    Regards
    McA

      Perfect; that's just what I want! I'd forgotten that "local" did that; I need to temporarily override the values so that for example a 1000-element array doesn't take one line each...

      My Perl is a little rusty after being out of the workforce for a few years now, so I thought I'd brush up by writing a script that solves the "target" puzzle that's syndicated in some papers (and without any false modesty, it's *fast*).

      Thanks.

Re: Can I auto-save and restore global variables?
by AppleFritter (Vicar) on Jul 24, 2014 at 21:49 UTC

    You can use local, though that only works for individual variables, not an entire package:

    $Data::Dumper::blah = 0; say $Data::Dumper::blah; # 0 { local $Data::Dumper::blah = 1; say $Data::Dumper::blah; # 1 } say $Data::Dumper::blah; # 0

    The nice thing about local variables is that they're dynamically-scoped, so if you invoke subroutines from that block, they will also see the localized value.

Re: Can I auto-save and restore global variables?
by davido (Cardinal) on Jul 25, 2014 at 00:14 UTC

    You could tie a variable to a class that writes to and reads from some persistent storage. Storable might facilitate serializing to your persistent storage, for example. Just don't expect to see impressive throughput. If you dont need to save to persistent storage local is probably an adequate solution. We really would need to know the use case.


    Dave

      Recent, tangentially relevant thread: Perl data notation.


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      OK, here's the gory details. Consider a 3x3 grid of shuffled letters; the goal is to generate all words, 4-9 letters long, each containing the centre letter. Proper nouns are not allowed, and neither are plurals ending in "s" (I can't do much about that, but at least I print a warning).

      I gobble up all words fitting the criteria from as many free dictionaries as I can find (Chambers is supposed to be used, but it ain't available nor is it free). Each word is letter-sorted (the slowest part of the operation) and hashed; the value is a reference to an anonymous array of its anagrams as they're found. Still with me? I then dig out all permutations of those letters (8P(3..8) plus the centre letter), sort them and print them.

      Some (revised) code snippets; avert thine eyes if BSD-style formatting offends thee:

      $Data::Dumper::Terse = 1; # Briefer - looks nice (no silly $VARnnn) ... print "\n### words ###\n", Dumper(%words) if ($Dbits & $D_WORDS); ... if ($stats_required) { ... if ($Dbits & $D_LENGTHS) { local $Data::Dumper::Indent = 0; local $Data::Dumper::Pad = ' '; print "\n### lengths ###\n", Dumper(@lengths), "\n"; } ... }

      Hope that's clear.

      In the unlikely event of being bored, I'll think about something like:

      $Data::Dumper::Push(Indent Pad) # Save these ... $Data::Dumper::Pop() # Restore everything

      but "local" is just as good.

      -- Dave

        Data::Dumper is more flexible than you think :)
        sub dd { use Data::Dumper; print Data::Dumper->new([@_])->Sortkeys(1) ->Indent(1)->Useqq(1)->Dump . "\n"; }
Re: Can I auto-save and restore global variables?
by Anonymous Monk on Jul 25, 2014 at 01:27 UTC

    See docs for Safe::World ... then think of a better programming strategy :)