Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

freeze and thaw ?

by cztmonk (Monk)
on Jul 28, 2012 at 11:21 UTC ( #984184=perlquestion: print w/replies, xml ) Need Help??
cztmonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,simple question here. Can someone explain what I can do with freeze and thaw of Maybe some sample code?

Replies are listed 'Best First'.
Re: freeze and thaw ?
by moritz (Cardinal) on Jul 28, 2012 at 12:16 UTC

    freeze creates a binary image of a data structure, and thaw takes that image and turns it back into a copy of the original data structure.

    That way you can easily exchange non-trivial data structures between two perl processes, or store one and retrieve it later in a different run of the same perl script.

Re: freeze and thaw ?
by Corion (Pope) on Jul 28, 2012 at 11:54 UTC

    Have you read the documentation provided in Storable? What parts do you have problems with?

    The Storable package brings persistence to your Perl data structures containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be conveniently stored to disk and retrieved at a later time.

    "Persistence" means "Surviving things", in this case, surviving the end of the program. If you build a data structure in your program, you can store that data structure to disk using Storable.

      I have trouble understanding the term "Serializing to memory", why not use "save to memory" ...

        I think "save" is more commonly used with nonvolatile storage, while "serialize" is commonly used to mean "transform a data structure to a string (or sequence of bytes)".

Re: freeze and thaw ?
by cavac (Deacon) on Jul 31, 2012 at 17:07 UTC

    Functions like these let you store and retrieve complex data structures to and from simple scalars. Most time it makes sense to use standardized formats and data storage systems (properly defined file formats, databases, etc...), but if the data is for internal use only or to pass data between two of your own scripts, it sometimes makes sense to use storable.

    Say, for example, you have two scripts running in parallel. One of does some background calculations and the other one wants to display the preliminary results on a web page. The results are a complex data structure. One of the cheap-and-dirty ways to accomplish this for the background process to just freeze the structure into a scalar and write it to a file (or use Cache::Memcache and a database text field, etc) every once in a while. When the webpage is refreshed, the Perl script loads the scalar, thaws it and dumps it into a Template Toolkit template that renders the required data.

    This way, the webpage developer doesn't even have to know which datafields there are or what they mean. And the guy who wrote the background process doesn't have to know much about web programming. Only a few basics on how to use TT...

    Also, if the background process puts all it's internal states into that data structure, you can use this to quickly implement a "restart from last savepoint" feature with a few lines of code. Without ever having to write a complex savefile-generator and an equally complex parser.

    As mentioned, the limitation of approaches likes this are plenty. Talking with programs in other languages or even Perl programs written by other developers can get ugly very fast, since the data structures most likely reflect the internal state of whatever your program is doing with the data - so every internal change gets passed on to your communication partner. And even loading older savefiles from your own software might be problematic...

    Hope i got everything right and made the topic a bit clearer...

    "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"

      Thanks cavac, for your comments.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://984184]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-01-19 19:16 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.