Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Can I auto-save and restore global variables?

by davido (Cardinal)
on Jul 25, 2014 at 00:14 UTC ( [id://1094970]=note: print w/replies, xml ) Need Help??


in reply to Can I auto-save and restore global variables?

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

  • Comment on Re: Can I auto-save and restore global variables?

Replies are listed 'Best First'.
Re^2: Can I auto-save and restore global variables?
by kennethk (Abbot) on Jul 25, 2014 at 03:18 UTC

    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.

Re^2: Can I auto-save and restore global variables?
by davehorsfall (Novice) on Jul 25, 2014 at 05:34 UTC

    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"; }

        Gawd... I'm gonna have to start using OO Perl; I'm familiar with C++ etc, but I have a strong C background.

        Thanks for the brief tutorial :-)

        -- Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1094970]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found