1. Perl
  2. Module
  3. here

Storable - Serialize data

The Storable module allows you to serialize your data. Serialization is the conversion of Perl's data structure into a format that can be stored externally.

# Loading modules and importing functions
use Storable qw/freeze thaw/;

Use the freeze function to serialize the data.

# Serialize data
my $data = [
  {name =>'Ken', age => 19},
  {name =>'Taro', age => 20}
];;
my $serialized_data = freeze $data;

Use the thaw function to restore the serialized data.

# Restore serialized data
my $data = thaw $serialized_data;

FAQ about Storable modules

Q. I saw an nfreeze function. How is it different from the freeze function?

A. You can use the nfreeze function to serialize your data in an OS-independent way. For example, suppose you save the data serialized by the freeze function on a database server on a certain OS. If another OS retrieves the serialized data from the database server, it cannot be restored by thaw. In such cases, you need to serialize with nfreeze.

Q. What are the advantages and disadvantages of saving Perl data structures in JSON?

A. The advantage is performance. Storable is faster than JSON and Data::Dumper. The downside is that the data can only be restored with Perl's thaw. Also, if the version of Perl goes up and binary compatibility is lost, it may not be possible to restore it with the upgraded version of Perl. Therefore, it is better to limit the data serialized by Storable to temporary data.

Related Informatrion