3

I am having a hard time understanding what Storable does.

I know that it "stores" a variable into your disk, but why would I need to do that? What would I use this module for, and how would I do it?

Zaid
  • 36,680
  • 16
  • 86
  • 155
Dynamic
  • 921
  • 1
  • 12
  • 31

2 Answers2

10

Reasons that spring to mind:

  • Persist memory across script calls
  • Sharing variables across different processes (sometimes it isn't possible to pipe stuff)

Of course, that's not all that Storable does. It also:

  • Makes it possible to create deep clones of data structures
  • Serializes the data structure stored, which implies a smaller file footprint than output from Data::Dump
  • Is optimized for speed (so it's faster to retrieve than to require a file containing Data::Dump output
Zaid
  • 36,680
  • 16
  • 86
  • 155
  • It's also potentially safer than `require`ing a [`Data::Dump`]() file because someone may have added some Perl code to the output of `Data::Dumper`. By default [`Storable`](http://perldoc.perl.org/Storable.html) doesn't `eval` any code it receives, so it is safer. – Brad Gilbert Dec 18 '11 at 18:29
2

One example:

Your program spends a long time populating your data structure, a graph, or trie, and if the program crashes then you'd lose it all and have to start again from square one. To avoid losing this data and be able to continue where it stopped last time you can save a snapshot of the data to a file manually or just simply use Storable.

holygeek
  • 15,653
  • 1
  • 40
  • 50
  • Could I save STDIN? For example, if I wrote a program to take a name (in `$name`) and print "Hello, $name", could I use this to save all names ever used, and create a command to print the names? – Dynamic Aug 24 '11 at 23:58
  • @perl.j, It can be used to store an array, so yes, you could store a list of names. – ikegami Aug 25 '11 at 00:56
  • @ikegami: How do I save input from the Perl "Diamond Operator"? – Dynamic Aug 25 '11 at 02:08
  • perl.j: it saves variables, not operators. How you would capture anything ever read using `<>` into a variable is a completely different question, and not one related to storable at all. – ysth Aug 25 '11 at 02:47
  • @perl.j, Same as any other string. – ikegami Aug 25 '11 at 04:40
  • @perl.j, `my $input = <>; store_fd(\$input, $fh);` – ikegami Aug 25 '11 at 19:47
  • @ikegami: Will that store every `$input` on every use of the program? – Dynamic Aug 25 '11 at 20:38
  • @perl.j, What do you mean by "every $input"? There is only one `$input`. – ikegami Aug 25 '11 at 21:48
  • @ikegami: I mean if I am going to frequently use this program, I will have several different `$input`'s, right? So how do I store every one of those `$input`'s? – Dynamic Aug 25 '11 at 23:25