5.3 Storing Complex Data with StorableYou can take the output of Data::Dumper's Dumper routine, place it into a file, and then load the file to a different program, evaluating the code as Perl code, and you'd end up with two package variables, $VAR1 and $VAR2, that are equivalent to the original data. This is called marshaling the data: converting complex data into a form that can be written to a file as a stream of bytes for later reconstruction.However, another Perl core module is much better suited for marshaling: Storable. It's better suited because compared to Data::Dumper, Storable produces smaller and faster-to-process files. (The Storable module is standard in recent versions of Perl, but you can always install it from the CPAN if it's missing.)The interface is similar to using Data::Dumper, except you must put everything into one reference. For example, let's store the mutually referencing data structures: use Storable; The file produced by this step was 68 bytes on this system, which was quite a bit shorter than the equivalent Data::Dumper output. It's also much less readable for humans. It's easy for Storable to read, as you'll soon see.[1][1] The format used by Storable is architecture byte-order dependent by default. The manpage shows how to create byte-order-independent storage files. Next, fetch the data, again using the Storable module. The result will be a single array reference. Dump the result to see if it stored the right values: use Storable; Here's the result: $VAR1 = [ This is functionally the same as the original data structure. You're now looking at the two array references within one top-level array. To get something closer to what you saw before, you can be more explicit about the return value: use Storable; or equivalently: use Storable; and you'll get: $VAR1 = [ just as you did in the original program. With Storable, you can store data and retrieve it later. More information on Storable can be found in perldoc Storable, as always. |