156d68f1eSafresh1# 256d68f1eSafresh1# Copyright (c) 1995-2001, Raphael Manfredi 356d68f1eSafresh1# Copyright (c) 2002-2014 by the Perl 5 Porters 456d68f1eSafresh1# Copyright (c) 2015-2016 cPanel Inc 556d68f1eSafresh1# Copyright (c) 2017 Reini Urban 656d68f1eSafresh1# 756d68f1eSafresh1# You may redistribute only under the same terms as Perl 5, as specified 856d68f1eSafresh1# in the README file that comes with the distribution. 956d68f1eSafresh1# 1056d68f1eSafresh1 1156d68f1eSafresh1BEGIN { require XSLoader } 1256d68f1eSafresh1require Exporter; 1356d68f1eSafresh1package Storable; 1456d68f1eSafresh1 1556d68f1eSafresh1our @ISA = qw(Exporter); 1656d68f1eSafresh1our @EXPORT = qw(store retrieve); 1756d68f1eSafresh1our @EXPORT_OK = qw( 1856d68f1eSafresh1 nstore store_fd nstore_fd fd_retrieve 1956d68f1eSafresh1 freeze nfreeze thaw 2056d68f1eSafresh1 dclone 2156d68f1eSafresh1 retrieve_fd 2256d68f1eSafresh1 lock_store lock_nstore lock_retrieve 2356d68f1eSafresh1 file_magic read_magic 2456d68f1eSafresh1 BLESS_OK TIE_OK FLAGS_COMPAT 2556d68f1eSafresh1 stack_depth stack_depth_hash 2656d68f1eSafresh1); 2756d68f1eSafresh1 2856d68f1eSafresh1our ($canonical, $forgive_me); 2956d68f1eSafresh1 3056d68f1eSafresh1BEGIN { 31*e0680481Safresh1 our $VERSION = '3.32'; 3256d68f1eSafresh1} 3356d68f1eSafresh1 3456d68f1eSafresh1our $recursion_limit; 3556d68f1eSafresh1our $recursion_limit_hash; 3656d68f1eSafresh1 3756d68f1eSafresh1$recursion_limit = 512 3856d68f1eSafresh1 unless defined $recursion_limit; 3956d68f1eSafresh1$recursion_limit_hash = 256 4056d68f1eSafresh1 unless defined $recursion_limit_hash; 4156d68f1eSafresh1 4256d68f1eSafresh1use Carp; 4356d68f1eSafresh1 4456d68f1eSafresh1BEGIN { 4556d68f1eSafresh1 if (eval { 4656d68f1eSafresh1 local $SIG{__DIE__}; 4756d68f1eSafresh1 local @INC = @INC; 4856d68f1eSafresh1 pop @INC if $INC[-1] eq '.'; 4956d68f1eSafresh1 require Log::Agent; 5056d68f1eSafresh1 1; 5156d68f1eSafresh1 }) { 5256d68f1eSafresh1 Log::Agent->import; 5356d68f1eSafresh1 } 5456d68f1eSafresh1 # 5556d68f1eSafresh1 # Use of Log::Agent is optional. If it hasn't imported these subs then 5656d68f1eSafresh1 # provide a fallback implementation. 5756d68f1eSafresh1 # 5856d68f1eSafresh1 unless ($Storable::{logcroak} && *{$Storable::{logcroak}}{CODE}) { 5956d68f1eSafresh1 *logcroak = \&Carp::croak; 6056d68f1eSafresh1 } 6156d68f1eSafresh1 else { 6256d68f1eSafresh1 # Log::Agent's logcroak always adds a newline to the error it is 6356d68f1eSafresh1 # given. This breaks refs getting thrown. We can just discard what 6456d68f1eSafresh1 # it throws (but keep whatever logging it does) and throw the original 6556d68f1eSafresh1 # args. 6656d68f1eSafresh1 no warnings 'redefine'; 6756d68f1eSafresh1 my $logcroak = \&logcroak; 6856d68f1eSafresh1 *logcroak = sub { 6956d68f1eSafresh1 my @args = @_; 7056d68f1eSafresh1 eval { &$logcroak }; 7156d68f1eSafresh1 Carp::croak(@args); 7256d68f1eSafresh1 }; 7356d68f1eSafresh1 } 7456d68f1eSafresh1 unless ($Storable::{logcarp} && *{$Storable::{logcarp}}{CODE}) { 7556d68f1eSafresh1 *logcarp = \&Carp::carp; 7656d68f1eSafresh1 } 7756d68f1eSafresh1} 7856d68f1eSafresh1 7956d68f1eSafresh1# 8056d68f1eSafresh1# They might miss :flock in Fcntl 8156d68f1eSafresh1# 8256d68f1eSafresh1 8356d68f1eSafresh1BEGIN { 8456d68f1eSafresh1 if (eval { require Fcntl; 1 } && exists $Fcntl::EXPORT_TAGS{'flock'}) { 8556d68f1eSafresh1 Fcntl->import(':flock'); 8656d68f1eSafresh1 } else { 8756d68f1eSafresh1 eval q{ 8856d68f1eSafresh1 sub LOCK_SH () { 1 } 8956d68f1eSafresh1 sub LOCK_EX () { 2 } 9056d68f1eSafresh1 }; 9156d68f1eSafresh1 } 9256d68f1eSafresh1} 9356d68f1eSafresh1 9456d68f1eSafresh1sub CLONE { 9556d68f1eSafresh1 # clone context under threads 9656d68f1eSafresh1 Storable::init_perinterp(); 9756d68f1eSafresh1} 9856d68f1eSafresh1 9956d68f1eSafresh1sub BLESS_OK () { 2 } 10056d68f1eSafresh1sub TIE_OK () { 4 } 10156d68f1eSafresh1sub FLAGS_COMPAT () { BLESS_OK | TIE_OK } 10256d68f1eSafresh1 10356d68f1eSafresh1# By default restricted hashes are downgraded on earlier perls. 10456d68f1eSafresh1 10556d68f1eSafresh1$Storable::flags = FLAGS_COMPAT; 10656d68f1eSafresh1$Storable::downgrade_restricted = 1; 10756d68f1eSafresh1$Storable::accept_future_minor = 1; 10856d68f1eSafresh1 10956d68f1eSafresh1BEGIN { XSLoader::load('Storable') }; 11056d68f1eSafresh1 11156d68f1eSafresh1# 11256d68f1eSafresh1# Determine whether locking is possible, but only when needed. 11356d68f1eSafresh1# 11456d68f1eSafresh1 11556d68f1eSafresh1sub show_file_magic { 11656d68f1eSafresh1 print <<EOM; 11756d68f1eSafresh1# 11856d68f1eSafresh1# To recognize the data files of the Perl module Storable, 11956d68f1eSafresh1# the following lines need to be added to the local magic(5) file, 12056d68f1eSafresh1# usually either /usr/share/misc/magic or /etc/magic. 12156d68f1eSafresh1# 12256d68f1eSafresh10 string perl-store perl Storable(v0.6) data 12356d68f1eSafresh1>4 byte >0 (net-order %d) 12456d68f1eSafresh1>>4 byte &01 (network-ordered) 12556d68f1eSafresh1>>4 byte =3 (major 1) 12656d68f1eSafresh1>>4 byte =2 (major 1) 12756d68f1eSafresh1 12856d68f1eSafresh10 string pst0 perl Storable(v0.7) data 12956d68f1eSafresh1>4 byte >0 13056d68f1eSafresh1>>4 byte &01 (network-ordered) 13156d68f1eSafresh1>>4 byte =5 (major 2) 13256d68f1eSafresh1>>4 byte =4 (major 2) 13356d68f1eSafresh1>>5 byte >0 (minor %d) 13456d68f1eSafresh1EOM 13556d68f1eSafresh1} 13656d68f1eSafresh1 13756d68f1eSafresh1sub file_magic { 13856d68f1eSafresh1 require IO::File; 13956d68f1eSafresh1 14056d68f1eSafresh1 my $file = shift; 14156d68f1eSafresh1 my $fh = IO::File->new; 14256d68f1eSafresh1 open($fh, "<", $file) || die "Can't open '$file': $!"; 14356d68f1eSafresh1 binmode($fh); 14456d68f1eSafresh1 defined(sysread($fh, my $buf, 32)) || die "Can't read from '$file': $!"; 14556d68f1eSafresh1 close($fh); 14656d68f1eSafresh1 14756d68f1eSafresh1 $file = "./$file" unless $file; # ensure TRUE value 14856d68f1eSafresh1 14956d68f1eSafresh1 return read_magic($buf, $file); 15056d68f1eSafresh1} 15156d68f1eSafresh1 15256d68f1eSafresh1sub read_magic { 15356d68f1eSafresh1 my($buf, $file) = @_; 15456d68f1eSafresh1 my %info; 15556d68f1eSafresh1 15656d68f1eSafresh1 my $buflen = length($buf); 15756d68f1eSafresh1 my $magic; 15856d68f1eSafresh1 if ($buf =~ s/^(pst0|perl-store)//) { 15956d68f1eSafresh1 $magic = $1; 16056d68f1eSafresh1 $info{file} = $file || 1; 16156d68f1eSafresh1 } 16256d68f1eSafresh1 else { 16356d68f1eSafresh1 return undef if $file; 16456d68f1eSafresh1 $magic = ""; 16556d68f1eSafresh1 } 16656d68f1eSafresh1 16756d68f1eSafresh1 return undef unless length($buf); 16856d68f1eSafresh1 16956d68f1eSafresh1 my $net_order; 17056d68f1eSafresh1 if ($magic eq "perl-store" && ord(substr($buf, 0, 1)) > 1) { 17156d68f1eSafresh1 $info{version} = -1; 17256d68f1eSafresh1 $net_order = 0; 17356d68f1eSafresh1 } 17456d68f1eSafresh1 else { 17556d68f1eSafresh1 $buf =~ s/(.)//s; 17656d68f1eSafresh1 my $major = (ord $1) >> 1; 17756d68f1eSafresh1 return undef if $major > 4; # sanity (assuming we never go that high) 17856d68f1eSafresh1 $info{major} = $major; 17956d68f1eSafresh1 $net_order = (ord $1) & 0x01; 18056d68f1eSafresh1 if ($major > 1) { 18156d68f1eSafresh1 return undef unless $buf =~ s/(.)//s; 18256d68f1eSafresh1 my $minor = ord $1; 18356d68f1eSafresh1 $info{minor} = $minor; 18456d68f1eSafresh1 $info{version} = "$major.$minor"; 18556d68f1eSafresh1 $info{version_nv} = sprintf "%d.%03d", $major, $minor; 18656d68f1eSafresh1 } 18756d68f1eSafresh1 else { 18856d68f1eSafresh1 $info{version} = $major; 18956d68f1eSafresh1 } 19056d68f1eSafresh1 } 19156d68f1eSafresh1 $info{version_nv} ||= $info{version}; 19256d68f1eSafresh1 $info{netorder} = $net_order; 19356d68f1eSafresh1 19456d68f1eSafresh1 unless ($net_order) { 19556d68f1eSafresh1 return undef unless $buf =~ s/(.)//s; 19656d68f1eSafresh1 my $len = ord $1; 19756d68f1eSafresh1 return undef unless length($buf) >= $len; 19856d68f1eSafresh1 return undef unless $len == 4 || $len == 8; # sanity 19956d68f1eSafresh1 @info{qw(byteorder intsize longsize ptrsize)} 20056d68f1eSafresh1 = unpack "a${len}CCC", $buf; 20156d68f1eSafresh1 (substr $buf, 0, $len + 3) = ''; 20256d68f1eSafresh1 if ($info{version_nv} >= 2.002) { 20356d68f1eSafresh1 return undef unless $buf =~ s/(.)//s; 20456d68f1eSafresh1 $info{nvsize} = ord $1; 20556d68f1eSafresh1 } 20656d68f1eSafresh1 } 20756d68f1eSafresh1 $info{hdrsize} = $buflen - length($buf); 20856d68f1eSafresh1 20956d68f1eSafresh1 return \%info; 21056d68f1eSafresh1} 21156d68f1eSafresh1 21256d68f1eSafresh1sub BIN_VERSION_NV { 21356d68f1eSafresh1 sprintf "%d.%03d", BIN_MAJOR(), BIN_MINOR(); 21456d68f1eSafresh1} 21556d68f1eSafresh1 21656d68f1eSafresh1sub BIN_WRITE_VERSION_NV { 21756d68f1eSafresh1 sprintf "%d.%03d", BIN_MAJOR(), BIN_WRITE_MINOR(); 21856d68f1eSafresh1} 21956d68f1eSafresh1 22056d68f1eSafresh1# 22156d68f1eSafresh1# store 22256d68f1eSafresh1# 22356d68f1eSafresh1# Store target object hierarchy, identified by a reference to its root. 22456d68f1eSafresh1# The stored object tree may later be retrieved to memory via retrieve. 22556d68f1eSafresh1# Returns undef if an I/O error occurred, in which case the file is 22656d68f1eSafresh1# removed. 22756d68f1eSafresh1# 22856d68f1eSafresh1sub store { 22956d68f1eSafresh1 return _store(\&pstore, @_, 0); 23056d68f1eSafresh1} 23156d68f1eSafresh1 23256d68f1eSafresh1# 23356d68f1eSafresh1# nstore 23456d68f1eSafresh1# 23556d68f1eSafresh1# Same as store, but in network order. 23656d68f1eSafresh1# 23756d68f1eSafresh1sub nstore { 23856d68f1eSafresh1 return _store(\&net_pstore, @_, 0); 23956d68f1eSafresh1} 24056d68f1eSafresh1 24156d68f1eSafresh1# 24256d68f1eSafresh1# lock_store 24356d68f1eSafresh1# 24456d68f1eSafresh1# Same as store, but flock the file first (advisory locking). 24556d68f1eSafresh1# 24656d68f1eSafresh1sub lock_store { 24756d68f1eSafresh1 return _store(\&pstore, @_, 1); 24856d68f1eSafresh1} 24956d68f1eSafresh1 25056d68f1eSafresh1# 25156d68f1eSafresh1# lock_nstore 25256d68f1eSafresh1# 25356d68f1eSafresh1# Same as nstore, but flock the file first (advisory locking). 25456d68f1eSafresh1# 25556d68f1eSafresh1sub lock_nstore { 25656d68f1eSafresh1 return _store(\&net_pstore, @_, 1); 25756d68f1eSafresh1} 25856d68f1eSafresh1 25956d68f1eSafresh1# Internal store to file routine 26056d68f1eSafresh1sub _store { 26156d68f1eSafresh1 my $xsptr = shift; 26256d68f1eSafresh1 my $self = shift; 26356d68f1eSafresh1 my ($file, $use_locking) = @_; 26456d68f1eSafresh1 logcroak "not a reference" unless ref($self); 26556d68f1eSafresh1 logcroak "wrong argument number" unless @_ == 2; # No @foo in arglist 26656d68f1eSafresh1 local *FILE; 26756d68f1eSafresh1 if ($use_locking) { 26856d68f1eSafresh1 open(FILE, ">>", $file) || logcroak "can't write into $file: $!"; 26956d68f1eSafresh1 unless (CAN_FLOCK) { 27056d68f1eSafresh1 logcarp 27156d68f1eSafresh1 "Storable::lock_store: fcntl/flock emulation broken on $^O"; 27256d68f1eSafresh1 return undef; 27356d68f1eSafresh1 } 27456d68f1eSafresh1 flock(FILE, LOCK_EX) || 27556d68f1eSafresh1 logcroak "can't get exclusive lock on $file: $!"; 27656d68f1eSafresh1 truncate FILE, 0; 27756d68f1eSafresh1 # Unlocking will happen when FILE is closed 27856d68f1eSafresh1 } else { 27956d68f1eSafresh1 open(FILE, ">", $file) || logcroak "can't create $file: $!"; 28056d68f1eSafresh1 } 28156d68f1eSafresh1 binmode FILE; # Archaic systems... 28256d68f1eSafresh1 my $da = $@; # Don't mess if called from exception handler 28356d68f1eSafresh1 my $ret; 28456d68f1eSafresh1 # Call C routine nstore or pstore, depending on network order 28556d68f1eSafresh1 eval { $ret = &$xsptr(*FILE, $self) }; 28656d68f1eSafresh1 # close will return true on success, so the or short-circuits, the () 28756d68f1eSafresh1 # expression is true, and for that case the block will only be entered 28856d68f1eSafresh1 # if $@ is true (ie eval failed) 28956d68f1eSafresh1 # if close fails, it returns false, $ret is altered, *that* is (also) 29056d68f1eSafresh1 # false, so the () expression is false, !() is true, and the block is 29156d68f1eSafresh1 # entered. 29256d68f1eSafresh1 if (!(close(FILE) or undef $ret) || $@) { 29356d68f1eSafresh1 unlink($file) or warn "Can't unlink $file: $!\n"; 29456d68f1eSafresh1 } 29556d68f1eSafresh1 if ($@) { 29656d68f1eSafresh1 $@ =~ s/\.?\n$/,/ unless ref $@; 29756d68f1eSafresh1 logcroak $@; 29856d68f1eSafresh1 } 29956d68f1eSafresh1 $@ = $da; 30056d68f1eSafresh1 return $ret; 30156d68f1eSafresh1} 30256d68f1eSafresh1 30356d68f1eSafresh1# 30456d68f1eSafresh1# store_fd 30556d68f1eSafresh1# 30656d68f1eSafresh1# Same as store, but perform on an already opened file descriptor instead. 30756d68f1eSafresh1# Returns undef if an I/O error occurred. 30856d68f1eSafresh1# 30956d68f1eSafresh1sub store_fd { 31056d68f1eSafresh1 return _store_fd(\&pstore, @_); 31156d68f1eSafresh1} 31256d68f1eSafresh1 31356d68f1eSafresh1# 31456d68f1eSafresh1# nstore_fd 31556d68f1eSafresh1# 31656d68f1eSafresh1# Same as store_fd, but in network order. 31756d68f1eSafresh1# 31856d68f1eSafresh1sub nstore_fd { 31956d68f1eSafresh1 my ($self, $file) = @_; 32056d68f1eSafresh1 return _store_fd(\&net_pstore, @_); 32156d68f1eSafresh1} 32256d68f1eSafresh1 32356d68f1eSafresh1# Internal store routine on opened file descriptor 32456d68f1eSafresh1sub _store_fd { 32556d68f1eSafresh1 my $xsptr = shift; 32656d68f1eSafresh1 my $self = shift; 32756d68f1eSafresh1 my ($file) = @_; 32856d68f1eSafresh1 logcroak "not a reference" unless ref($self); 32956d68f1eSafresh1 logcroak "too many arguments" unless @_ == 1; # No @foo in arglist 33056d68f1eSafresh1 my $fd = fileno($file); 33156d68f1eSafresh1 logcroak "not a valid file descriptor" unless defined $fd; 33256d68f1eSafresh1 my $da = $@; # Don't mess if called from exception handler 33356d68f1eSafresh1 my $ret; 33456d68f1eSafresh1 # Call C routine nstore or pstore, depending on network order 33556d68f1eSafresh1 eval { $ret = &$xsptr($file, $self) }; 33656d68f1eSafresh1 logcroak $@ if $@ =~ s/\.?\n$/,/; 33756d68f1eSafresh1 local $\; print $file ''; # Autoflush the file if wanted 33856d68f1eSafresh1 $@ = $da; 33956d68f1eSafresh1 return $ret; 34056d68f1eSafresh1} 34156d68f1eSafresh1 34256d68f1eSafresh1# 34356d68f1eSafresh1# freeze 34456d68f1eSafresh1# 34556d68f1eSafresh1# Store object and its hierarchy in memory and return a scalar 34656d68f1eSafresh1# containing the result. 34756d68f1eSafresh1# 34856d68f1eSafresh1sub freeze { 34956d68f1eSafresh1 _freeze(\&mstore, @_); 35056d68f1eSafresh1} 35156d68f1eSafresh1 35256d68f1eSafresh1# 35356d68f1eSafresh1# nfreeze 35456d68f1eSafresh1# 35556d68f1eSafresh1# Same as freeze but in network order. 35656d68f1eSafresh1# 35756d68f1eSafresh1sub nfreeze { 35856d68f1eSafresh1 _freeze(\&net_mstore, @_); 35956d68f1eSafresh1} 36056d68f1eSafresh1 36156d68f1eSafresh1# Internal freeze routine 36256d68f1eSafresh1sub _freeze { 36356d68f1eSafresh1 my $xsptr = shift; 36456d68f1eSafresh1 my $self = shift; 36556d68f1eSafresh1 logcroak "not a reference" unless ref($self); 36656d68f1eSafresh1 logcroak "too many arguments" unless @_ == 0; # No @foo in arglist 36756d68f1eSafresh1 my $da = $@; # Don't mess if called from exception handler 36856d68f1eSafresh1 my $ret; 36956d68f1eSafresh1 # Call C routine mstore or net_mstore, depending on network order 37056d68f1eSafresh1 eval { $ret = &$xsptr($self) }; 37156d68f1eSafresh1 if ($@) { 37256d68f1eSafresh1 $@ =~ s/\.?\n$/,/ unless ref $@; 37356d68f1eSafresh1 logcroak $@; 37456d68f1eSafresh1 } 37556d68f1eSafresh1 $@ = $da; 37656d68f1eSafresh1 return $ret ? $ret : undef; 37756d68f1eSafresh1} 37856d68f1eSafresh1 37956d68f1eSafresh1# 38056d68f1eSafresh1# retrieve 38156d68f1eSafresh1# 38256d68f1eSafresh1# Retrieve object hierarchy from disk, returning a reference to the root 38356d68f1eSafresh1# object of that tree. 38456d68f1eSafresh1# 38556d68f1eSafresh1# retrieve(file, flags) 38656d68f1eSafresh1# flags include by default BLESS_OK=2 | TIE_OK=4 38756d68f1eSafresh1# with flags=0 or the global $Storable::flags set to 0, no resulting object 38856d68f1eSafresh1# will be blessed nor tied. 38956d68f1eSafresh1# 39056d68f1eSafresh1sub retrieve { 39156d68f1eSafresh1 _retrieve(shift, 0, @_); 39256d68f1eSafresh1} 39356d68f1eSafresh1 39456d68f1eSafresh1# 39556d68f1eSafresh1# lock_retrieve 39656d68f1eSafresh1# 39756d68f1eSafresh1# Same as retrieve, but with advisory locking. 39856d68f1eSafresh1# 39956d68f1eSafresh1sub lock_retrieve { 40056d68f1eSafresh1 _retrieve(shift, 1, @_); 40156d68f1eSafresh1} 40256d68f1eSafresh1 40356d68f1eSafresh1# Internal retrieve routine 40456d68f1eSafresh1sub _retrieve { 40556d68f1eSafresh1 my ($file, $use_locking, $flags) = @_; 40656d68f1eSafresh1 $flags = $Storable::flags unless defined $flags; 40756d68f1eSafresh1 my $FILE; 40856d68f1eSafresh1 open($FILE, "<", $file) || logcroak "can't open $file: $!"; 40956d68f1eSafresh1 binmode $FILE; # Archaic systems... 41056d68f1eSafresh1 my $self; 41156d68f1eSafresh1 my $da = $@; # Could be from exception handler 41256d68f1eSafresh1 if ($use_locking) { 41356d68f1eSafresh1 unless (CAN_FLOCK) { 41456d68f1eSafresh1 logcarp 41556d68f1eSafresh1 "Storable::lock_store: fcntl/flock emulation broken on $^O"; 41656d68f1eSafresh1 return undef; 41756d68f1eSafresh1 } 41856d68f1eSafresh1 flock($FILE, LOCK_SH) || logcroak "can't get shared lock on $file: $!"; 41956d68f1eSafresh1 # Unlocking will happen when FILE is closed 42056d68f1eSafresh1 } 42156d68f1eSafresh1 eval { $self = pretrieve($FILE, $flags) }; # Call C routine 42256d68f1eSafresh1 close($FILE); 42356d68f1eSafresh1 if ($@) { 42456d68f1eSafresh1 $@ =~ s/\.?\n$/,/ unless ref $@; 42556d68f1eSafresh1 logcroak $@; 42656d68f1eSafresh1 } 42756d68f1eSafresh1 $@ = $da; 42856d68f1eSafresh1 return $self; 42956d68f1eSafresh1} 43056d68f1eSafresh1 43156d68f1eSafresh1# 43256d68f1eSafresh1# fd_retrieve 43356d68f1eSafresh1# 43456d68f1eSafresh1# Same as retrieve, but perform from an already opened file descriptor instead. 43556d68f1eSafresh1# 43656d68f1eSafresh1sub fd_retrieve { 43756d68f1eSafresh1 my ($file, $flags) = @_; 43856d68f1eSafresh1 $flags = $Storable::flags unless defined $flags; 43956d68f1eSafresh1 my $fd = fileno($file); 44056d68f1eSafresh1 logcroak "not a valid file descriptor" unless defined $fd; 44156d68f1eSafresh1 my $self; 44256d68f1eSafresh1 my $da = $@; # Could be from exception handler 44356d68f1eSafresh1 eval { $self = pretrieve($file, $flags) }; # Call C routine 44456d68f1eSafresh1 if ($@) { 44556d68f1eSafresh1 $@ =~ s/\.?\n$/,/ unless ref $@; 44656d68f1eSafresh1 logcroak $@; 44756d68f1eSafresh1 } 44856d68f1eSafresh1 $@ = $da; 44956d68f1eSafresh1 return $self; 45056d68f1eSafresh1} 45156d68f1eSafresh1 45256d68f1eSafresh1sub retrieve_fd { &fd_retrieve } # Backward compatibility 45356d68f1eSafresh1 45456d68f1eSafresh1# 45556d68f1eSafresh1# thaw 45656d68f1eSafresh1# 45756d68f1eSafresh1# Recreate objects in memory from an existing frozen image created 45856d68f1eSafresh1# by freeze. If the frozen image passed is undef, return undef. 45956d68f1eSafresh1# 46056d68f1eSafresh1# thaw(frozen_obj, flags) 46156d68f1eSafresh1# flags include by default BLESS_OK=2 | TIE_OK=4 46256d68f1eSafresh1# with flags=0 or the global $Storable::flags set to 0, no resulting object 46356d68f1eSafresh1# will be blessed nor tied. 46456d68f1eSafresh1# 46556d68f1eSafresh1sub thaw { 46656d68f1eSafresh1 my ($frozen, $flags) = @_; 46756d68f1eSafresh1 $flags = $Storable::flags unless defined $flags; 46856d68f1eSafresh1 return undef unless defined $frozen; 46956d68f1eSafresh1 my $self; 47056d68f1eSafresh1 my $da = $@; # Could be from exception handler 47156d68f1eSafresh1 eval { $self = mretrieve($frozen, $flags) };# Call C routine 47256d68f1eSafresh1 if ($@) { 47356d68f1eSafresh1 $@ =~ s/\.?\n$/,/ unless ref $@; 47456d68f1eSafresh1 logcroak $@; 47556d68f1eSafresh1 } 47656d68f1eSafresh1 $@ = $da; 47756d68f1eSafresh1 return $self; 47856d68f1eSafresh1} 47956d68f1eSafresh1 48056d68f1eSafresh1# 48156d68f1eSafresh1# _make_re($re, $flags) 48256d68f1eSafresh1# 48356d68f1eSafresh1# Internal function used to thaw a regular expression. 48456d68f1eSafresh1# 48556d68f1eSafresh1 48656d68f1eSafresh1my $re_flags; 48756d68f1eSafresh1BEGIN { 48856d68f1eSafresh1 if ($] < 5.010) { 48956d68f1eSafresh1 $re_flags = qr/\A[imsx]*\z/; 49056d68f1eSafresh1 } 49156d68f1eSafresh1 elsif ($] < 5.014) { 49256d68f1eSafresh1 $re_flags = qr/\A[msixp]*\z/; 49356d68f1eSafresh1 } 49456d68f1eSafresh1 elsif ($] < 5.022) { 49556d68f1eSafresh1 $re_flags = qr/\A[msixpdual]*\z/; 49656d68f1eSafresh1 } 49756d68f1eSafresh1 else { 49856d68f1eSafresh1 $re_flags = qr/\A[msixpdualn]*\z/; 49956d68f1eSafresh1 } 50056d68f1eSafresh1} 50156d68f1eSafresh1 50256d68f1eSafresh1sub _make_re { 50356d68f1eSafresh1 my ($re, $flags) = @_; 50456d68f1eSafresh1 50556d68f1eSafresh1 $flags =~ $re_flags 50656d68f1eSafresh1 or die "regexp flags invalid"; 50756d68f1eSafresh1 50856d68f1eSafresh1 my $qr = eval "qr/\$re/$flags"; 50956d68f1eSafresh1 die $@ if $@; 51056d68f1eSafresh1 51156d68f1eSafresh1 $qr; 51256d68f1eSafresh1} 51356d68f1eSafresh1 51456d68f1eSafresh1if ($] < 5.012) { 51556d68f1eSafresh1 eval <<'EOS' 51656d68f1eSafresh1sub _regexp_pattern { 51756d68f1eSafresh1 my $re = "" . shift; 51856d68f1eSafresh1 $re =~ /\A\(\?([xism]*)(?:-[xism]*)?:(.*)\)\z/s 51956d68f1eSafresh1 or die "Cannot parse regexp /$re/"; 52056d68f1eSafresh1 return ($2, $1); 52156d68f1eSafresh1} 52256d68f1eSafresh11 52356d68f1eSafresh1EOS 52456d68f1eSafresh1 or die "Cannot define _regexp_pattern: $@"; 52556d68f1eSafresh1} 52656d68f1eSafresh1 52756d68f1eSafresh11; 52856d68f1eSafresh1__END__ 52956d68f1eSafresh1 53056d68f1eSafresh1=head1 NAME 53156d68f1eSafresh1 53256d68f1eSafresh1Storable - persistence for Perl data structures 53356d68f1eSafresh1 53456d68f1eSafresh1=head1 SYNOPSIS 53556d68f1eSafresh1 53656d68f1eSafresh1 use Storable; 53756d68f1eSafresh1 store \%table, 'file'; 53856d68f1eSafresh1 $hashref = retrieve('file'); 53956d68f1eSafresh1 54056d68f1eSafresh1 use Storable qw(nstore store_fd nstore_fd freeze thaw dclone); 54156d68f1eSafresh1 54256d68f1eSafresh1 # Network order 54356d68f1eSafresh1 nstore \%table, 'file'; 54456d68f1eSafresh1 $hashref = retrieve('file'); # There is NO nretrieve() 54556d68f1eSafresh1 54656d68f1eSafresh1 # Storing to and retrieving from an already opened file 54756d68f1eSafresh1 store_fd \@array, \*STDOUT; 54856d68f1eSafresh1 nstore_fd \%table, \*STDOUT; 54956d68f1eSafresh1 $aryref = fd_retrieve(\*SOCKET); 55056d68f1eSafresh1 $hashref = fd_retrieve(\*SOCKET); 55156d68f1eSafresh1 55256d68f1eSafresh1 # Serializing to memory 55356d68f1eSafresh1 $serialized = freeze \%table; 55456d68f1eSafresh1 %table_clone = %{ thaw($serialized) }; 55556d68f1eSafresh1 55656d68f1eSafresh1 # Deep (recursive) cloning 55756d68f1eSafresh1 $cloneref = dclone($ref); 55856d68f1eSafresh1 55956d68f1eSafresh1 # Advisory locking 56056d68f1eSafresh1 use Storable qw(lock_store lock_nstore lock_retrieve) 56156d68f1eSafresh1 lock_store \%table, 'file'; 56256d68f1eSafresh1 lock_nstore \%table, 'file'; 56356d68f1eSafresh1 $hashref = lock_retrieve('file'); 56456d68f1eSafresh1 56556d68f1eSafresh1=head1 DESCRIPTION 56656d68f1eSafresh1 56756d68f1eSafresh1The Storable package brings persistence to your Perl data structures 56856d68f1eSafresh1containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be 56956d68f1eSafresh1conveniently stored to disk and retrieved at a later time. 57056d68f1eSafresh1 57156d68f1eSafresh1It can be used in the regular procedural way by calling C<store> with 57256d68f1eSafresh1a reference to the object to be stored, along with the file name where 57356d68f1eSafresh1the image should be written. 57456d68f1eSafresh1 57556d68f1eSafresh1The routine returns C<undef> for I/O problems or other internal error, 57656d68f1eSafresh1a true value otherwise. Serious errors are propagated as a C<die> exception. 57756d68f1eSafresh1 57856d68f1eSafresh1To retrieve data stored to disk, use C<retrieve> with a file name. 57956d68f1eSafresh1The objects stored into that file are recreated into memory for you, 58056d68f1eSafresh1and a I<reference> to the root object is returned. In case an I/O error 58156d68f1eSafresh1occurs while reading, C<undef> is returned instead. Other serious 58256d68f1eSafresh1errors are propagated via C<die>. 58356d68f1eSafresh1 58456d68f1eSafresh1Since storage is performed recursively, you might want to stuff references 58556d68f1eSafresh1to objects that share a lot of common data into a single array or hash 58656d68f1eSafresh1table, and then store that object. That way, when you retrieve back the 58756d68f1eSafresh1whole thing, the objects will continue to share what they originally shared. 58856d68f1eSafresh1 58956d68f1eSafresh1At the cost of a slight header overhead, you may store to an already 59056d68f1eSafresh1opened file descriptor using the C<store_fd> routine, and retrieve 59156d68f1eSafresh1from a file via C<fd_retrieve>. Those names aren't imported by default, 59256d68f1eSafresh1so you will have to do that explicitly if you need those routines. 59356d68f1eSafresh1The file descriptor you supply must be already opened, for read 59456d68f1eSafresh1if you're going to retrieve and for write if you wish to store. 59556d68f1eSafresh1 59656d68f1eSafresh1 store_fd(\%table, *STDOUT) || die "can't store to stdout\n"; 59756d68f1eSafresh1 $hashref = fd_retrieve(*STDIN); 59856d68f1eSafresh1 59956d68f1eSafresh1You can also store data in network order to allow easy sharing across 60056d68f1eSafresh1multiple platforms, or when storing on a socket known to be remotely 60156d68f1eSafresh1connected. The routines to call have an initial C<n> prefix for I<network>, 60256d68f1eSafresh1as in C<nstore> and C<nstore_fd>. At retrieval time, your data will be 60356d68f1eSafresh1correctly restored so you don't have to know whether you're restoring 60456d68f1eSafresh1from native or network ordered data. Double values are stored stringified 60556d68f1eSafresh1to ensure portability as well, at the slight risk of loosing some precision 60656d68f1eSafresh1in the last decimals. 60756d68f1eSafresh1 60856d68f1eSafresh1When using C<fd_retrieve>, objects are retrieved in sequence, one 60956d68f1eSafresh1object (i.e. one recursive tree) per associated C<store_fd>. 61056d68f1eSafresh1 61156d68f1eSafresh1If you're more from the object-oriented camp, you can inherit from 61256d68f1eSafresh1Storable and directly store your objects by invoking C<store> as 61356d68f1eSafresh1a method. The fact that the root of the to-be-stored tree is a 61456d68f1eSafresh1blessed reference (i.e. an object) is special-cased so that the 61556d68f1eSafresh1retrieve does not provide a reference to that object but rather the 61656d68f1eSafresh1blessed object reference itself. (Otherwise, you'd get a reference 61756d68f1eSafresh1to that blessed object). 61856d68f1eSafresh1 61956d68f1eSafresh1=head1 MEMORY STORE 62056d68f1eSafresh1 62156d68f1eSafresh1The Storable engine can also store data into a Perl scalar instead, to 62256d68f1eSafresh1later retrieve them. This is mainly used to freeze a complex structure in 62356d68f1eSafresh1some safe compact memory place (where it can possibly be sent to another 62456d68f1eSafresh1process via some IPC, since freezing the structure also serializes it in 62556d68f1eSafresh1effect). Later on, and maybe somewhere else, you can thaw the Perl scalar 62656d68f1eSafresh1out and recreate the original complex structure in memory. 62756d68f1eSafresh1 62856d68f1eSafresh1Surprisingly, the routines to be called are named C<freeze> and C<thaw>. 62956d68f1eSafresh1If you wish to send out the frozen scalar to another machine, use 63056d68f1eSafresh1C<nfreeze> instead to get a portable image. 63156d68f1eSafresh1 63256d68f1eSafresh1Note that freezing an object structure and immediately thawing it 63356d68f1eSafresh1actually achieves a deep cloning of that structure: 63456d68f1eSafresh1 63556d68f1eSafresh1 dclone(.) = thaw(freeze(.)) 63656d68f1eSafresh1 63756d68f1eSafresh1Storable provides you with a C<dclone> interface which does not create 63856d68f1eSafresh1that intermediary scalar but instead freezes the structure in some 63956d68f1eSafresh1internal memory space and then immediately thaws it out. 64056d68f1eSafresh1 64156d68f1eSafresh1=head1 ADVISORY LOCKING 64256d68f1eSafresh1 64356d68f1eSafresh1The C<lock_store> and C<lock_nstore> routine are equivalent to 64456d68f1eSafresh1C<store> and C<nstore>, except that they get an exclusive lock on 64556d68f1eSafresh1the file before writing. Likewise, C<lock_retrieve> does the same 64656d68f1eSafresh1as C<retrieve>, but also gets a shared lock on the file before reading. 64756d68f1eSafresh1 64856d68f1eSafresh1As with any advisory locking scheme, the protection only works if you 64956d68f1eSafresh1systematically use C<lock_store> and C<lock_retrieve>. If one side of 65056d68f1eSafresh1your application uses C<store> whilst the other uses C<lock_retrieve>, 65156d68f1eSafresh1you will get no protection at all. 65256d68f1eSafresh1 65356d68f1eSafresh1The internal advisory locking is implemented using Perl's flock() 65456d68f1eSafresh1routine. If your system does not support any form of flock(), or if 65556d68f1eSafresh1you share your files across NFS, you might wish to use other forms 65656d68f1eSafresh1of locking by using modules such as LockFile::Simple which lock a 65756d68f1eSafresh1file using a filesystem entry, instead of locking the file descriptor. 65856d68f1eSafresh1 65956d68f1eSafresh1=head1 SPEED 66056d68f1eSafresh1 66156d68f1eSafresh1The heart of Storable is written in C for decent speed. Extra low-level 66256d68f1eSafresh1optimizations have been made when manipulating perl internals, to 66356d68f1eSafresh1sacrifice encapsulation for the benefit of greater speed. 66456d68f1eSafresh1 66556d68f1eSafresh1=head1 CANONICAL REPRESENTATION 66656d68f1eSafresh1 66756d68f1eSafresh1Normally, Storable stores elements of hashes in the order they are 66856d68f1eSafresh1stored internally by Perl, i.e. pseudo-randomly. If you set 66956d68f1eSafresh1C<$Storable::canonical> to some C<TRUE> value, Storable will store 67056d68f1eSafresh1hashes with the elements sorted by their key. This allows you to 67156d68f1eSafresh1compare data structures by comparing their frozen representations (or 67256d68f1eSafresh1even the compressed frozen representations), which can be useful for 67356d68f1eSafresh1creating lookup tables for complicated queries. 67456d68f1eSafresh1 67556d68f1eSafresh1Canonical order does not imply network order; those are two orthogonal 67656d68f1eSafresh1settings. 67756d68f1eSafresh1 67856d68f1eSafresh1=head1 CODE REFERENCES 67956d68f1eSafresh1 68056d68f1eSafresh1Since Storable version 2.05, CODE references may be serialized with 68156d68f1eSafresh1the help of L<B::Deparse>. To enable this feature, set 68256d68f1eSafresh1C<$Storable::Deparse> to a true value. To enable deserialization, 68356d68f1eSafresh1C<$Storable::Eval> should be set to a true value. Be aware that 68456d68f1eSafresh1deserialization is done through C<eval>, which is dangerous if the 68556d68f1eSafresh1Storable file contains malicious data. You can set C<$Storable::Eval> 68656d68f1eSafresh1to a subroutine reference which would be used instead of C<eval>. See 68756d68f1eSafresh1below for an example using a L<Safe> compartment for deserialization 68856d68f1eSafresh1of CODE references. 68956d68f1eSafresh1 69056d68f1eSafresh1If C<$Storable::Deparse> and/or C<$Storable::Eval> are set to false 69156d68f1eSafresh1values, then the value of C<$Storable::forgive_me> (see below) is 69256d68f1eSafresh1respected while serializing and deserializing. 69356d68f1eSafresh1 69456d68f1eSafresh1=head1 FORWARD COMPATIBILITY 69556d68f1eSafresh1 69656d68f1eSafresh1This release of Storable can be used on a newer version of Perl to 69756d68f1eSafresh1serialize data which is not supported by earlier Perls. By default, 69856d68f1eSafresh1Storable will attempt to do the right thing, by C<croak()>ing if it 69956d68f1eSafresh1encounters data that it cannot deserialize. However, the defaults 70056d68f1eSafresh1can be changed as follows: 70156d68f1eSafresh1 70256d68f1eSafresh1=over 4 70356d68f1eSafresh1 70456d68f1eSafresh1=item utf8 data 70556d68f1eSafresh1 70656d68f1eSafresh1Perl 5.6 added support for Unicode characters with code points > 255, 70756d68f1eSafresh1and Perl 5.8 has full support for Unicode characters in hash keys. 70856d68f1eSafresh1Perl internally encodes strings with these characters using utf8, and 70956d68f1eSafresh1Storable serializes them as utf8. By default, if an older version of 71056d68f1eSafresh1Perl encounters a utf8 value it cannot represent, it will C<croak()>. 71156d68f1eSafresh1To change this behaviour so that Storable deserializes utf8 encoded 71256d68f1eSafresh1values as the string of bytes (effectively dropping the I<is_utf8> flag) 71356d68f1eSafresh1set C<$Storable::drop_utf8> to some C<TRUE> value. This is a form of 71456d68f1eSafresh1data loss, because with C<$drop_utf8> true, it becomes impossible to tell 71556d68f1eSafresh1whether the original data was the Unicode string, or a series of bytes 71656d68f1eSafresh1that happen to be valid utf8. 71756d68f1eSafresh1 71856d68f1eSafresh1=item restricted hashes 71956d68f1eSafresh1 72056d68f1eSafresh1Perl 5.8 adds support for restricted hashes, which have keys 72156d68f1eSafresh1restricted to a given set, and can have values locked to be read only. 72256d68f1eSafresh1By default, when Storable encounters a restricted hash on a perl 72356d68f1eSafresh1that doesn't support them, it will deserialize it as a normal hash, 72456d68f1eSafresh1silently discarding any placeholder keys and leaving the keys and 72556d68f1eSafresh1all values unlocked. To make Storable C<croak()> instead, set 72656d68f1eSafresh1C<$Storable::downgrade_restricted> to a C<FALSE> value. To restore 72756d68f1eSafresh1the default set it back to some C<TRUE> value. 72856d68f1eSafresh1 72956d68f1eSafresh1The cperl PERL_PERTURB_KEYS_TOP hash strategy has a known problem with 73056d68f1eSafresh1restricted hashes. 73156d68f1eSafresh1 73256d68f1eSafresh1=item huge objects 73356d68f1eSafresh1 73456d68f1eSafresh1On 64bit systems some data structures may exceed the 2G (i.e. I32_MAX) 73556d68f1eSafresh1limit. On 32bit systems also strings between I32 and U32 (2G-4G). 73656d68f1eSafresh1Since Storable 3.00 (not in perl5 core) we are able to store and 73756d68f1eSafresh1retrieve these objects, even if perl5 itself is not able to handle 73856d68f1eSafresh1them. These are strings longer then 4G, arrays with more then 2G 73956d68f1eSafresh1elements and hashes with more then 2G elements. cperl forbids hashes 74056d68f1eSafresh1with more than 2G elements, but this fail in cperl then. perl5 itself 74156d68f1eSafresh1at least until 5.26 allows it, but cannot iterate over them. 74256d68f1eSafresh1Note that creating those objects might cause out of memory 74356d68f1eSafresh1exceptions by the operating system before perl has a chance to abort. 74456d68f1eSafresh1 74556d68f1eSafresh1=item files from future versions of Storable 74656d68f1eSafresh1 74756d68f1eSafresh1Earlier versions of Storable would immediately croak if they encountered 74856d68f1eSafresh1a file with a higher internal version number than the reading Storable 74956d68f1eSafresh1knew about. Internal version numbers are increased each time new data 75056d68f1eSafresh1types (such as restricted hashes) are added to the vocabulary of the file 75156d68f1eSafresh1format. This meant that a newer Storable module had no way of writing a 75256d68f1eSafresh1file readable by an older Storable, even if the writer didn't store newer 75356d68f1eSafresh1data types. 75456d68f1eSafresh1 75556d68f1eSafresh1This version of Storable will defer croaking until it encounters a data 75656d68f1eSafresh1type in the file that it does not recognize. This means that it will 75756d68f1eSafresh1continue to read files generated by newer Storable modules which are careful 75856d68f1eSafresh1in what they write out, making it easier to upgrade Storable modules in a 75956d68f1eSafresh1mixed environment. 76056d68f1eSafresh1 76156d68f1eSafresh1The old behaviour of immediate croaking can be re-instated by setting 76256d68f1eSafresh1C<$Storable::accept_future_minor> to some C<FALSE> value. 76356d68f1eSafresh1 76456d68f1eSafresh1=back 76556d68f1eSafresh1 76656d68f1eSafresh1All these variables have no effect on a newer Perl which supports the 76756d68f1eSafresh1relevant feature. 76856d68f1eSafresh1 76956d68f1eSafresh1=head1 ERROR REPORTING 77056d68f1eSafresh1 77156d68f1eSafresh1Storable uses the "exception" paradigm, in that it does not try to 77256d68f1eSafresh1workaround failures: if something bad happens, an exception is 77356d68f1eSafresh1generated from the caller's perspective (see L<Carp> and C<croak()>). 77456d68f1eSafresh1Use eval {} to trap those exceptions. 77556d68f1eSafresh1 77656d68f1eSafresh1When Storable croaks, it tries to report the error via the C<logcroak()> 77756d68f1eSafresh1routine from the C<Log::Agent> package, if it is available. 77856d68f1eSafresh1 77956d68f1eSafresh1Normal errors are reported by having store() or retrieve() return C<undef>. 78056d68f1eSafresh1Such errors are usually I/O errors (or truncated stream errors at retrieval). 78156d68f1eSafresh1 78256d68f1eSafresh1When Storable throws the "Max. recursion depth with nested structures 78356d68f1eSafresh1exceeded" error we are already out of stack space. Unfortunately on 78456d68f1eSafresh1some earlier perl versions cleaning up a recursive data structure 78556d68f1eSafresh1recurses into the free calls, which will lead to stack overflows in 78656d68f1eSafresh1the cleanup. This data structure is not properly cleaned up then, it 78756d68f1eSafresh1will only be destroyed during global destruction. 78856d68f1eSafresh1 78956d68f1eSafresh1=head1 WIZARDS ONLY 79056d68f1eSafresh1 79156d68f1eSafresh1=head2 Hooks 79256d68f1eSafresh1 79356d68f1eSafresh1Any class may define hooks that will be called during the serialization 79456d68f1eSafresh1and deserialization process on objects that are instances of that class. 79556d68f1eSafresh1Those hooks can redefine the way serialization is performed (and therefore, 79656d68f1eSafresh1how the symmetrical deserialization should be conducted). 79756d68f1eSafresh1 79856d68f1eSafresh1Since we said earlier: 79956d68f1eSafresh1 80056d68f1eSafresh1 dclone(.) = thaw(freeze(.)) 80156d68f1eSafresh1 80256d68f1eSafresh1everything we say about hooks should also hold for deep cloning. However, 80356d68f1eSafresh1hooks get to know whether the operation is a mere serialization, or a cloning. 80456d68f1eSafresh1 80556d68f1eSafresh1Therefore, when serializing hooks are involved, 80656d68f1eSafresh1 80756d68f1eSafresh1 dclone(.) <> thaw(freeze(.)) 80856d68f1eSafresh1 80956d68f1eSafresh1Well, you could keep them in sync, but there's no guarantee it will always 81056d68f1eSafresh1hold on classes somebody else wrote. Besides, there is little to gain in 81156d68f1eSafresh1doing so: a serializing hook could keep only one attribute of an object, 81256d68f1eSafresh1which is probably not what should happen during a deep cloning of that 81356d68f1eSafresh1same object. 81456d68f1eSafresh1 81556d68f1eSafresh1Here is the hooking interface: 81656d68f1eSafresh1 81756d68f1eSafresh1=over 4 81856d68f1eSafresh1 81956d68f1eSafresh1=item C<STORABLE_freeze> I<obj>, I<cloning> 82056d68f1eSafresh1 82156d68f1eSafresh1The serializing hook, called on the object during serialization. It can be 82256d68f1eSafresh1inherited, or defined in the class itself, like any other method. 82356d68f1eSafresh1 82456d68f1eSafresh1Arguments: I<obj> is the object to serialize, I<cloning> is a flag indicating 82556d68f1eSafresh1whether we're in a dclone() or a regular serialization via store() or freeze(). 82656d68f1eSafresh1 82756d68f1eSafresh1Returned value: A LIST C<($serialized, $ref1, $ref2, ...)> where $serialized 82856d68f1eSafresh1is the serialized form to be used, and the optional $ref1, $ref2, etc... are 82956d68f1eSafresh1extra references that you wish to let the Storable engine serialize. 83056d68f1eSafresh1 83156d68f1eSafresh1At deserialization time, you will be given back the same LIST, but all the 83256d68f1eSafresh1extra references will be pointing into the deserialized structure. 83356d68f1eSafresh1 83456d68f1eSafresh1The B<first time> the hook is hit in a serialization flow, you may have it 83556d68f1eSafresh1return an empty list. That will signal the Storable engine to further 83656d68f1eSafresh1discard that hook for this class and to therefore revert to the default 83756d68f1eSafresh1serialization of the underlying Perl data. The hook will again be normally 83856d68f1eSafresh1processed in the next serialization. 83956d68f1eSafresh1 84056d68f1eSafresh1Unless you know better, serializing hook should always say: 84156d68f1eSafresh1 84256d68f1eSafresh1 sub STORABLE_freeze { 84356d68f1eSafresh1 my ($self, $cloning) = @_; 84456d68f1eSafresh1 return if $cloning; # Regular default serialization 84556d68f1eSafresh1 .... 84656d68f1eSafresh1 } 84756d68f1eSafresh1 84856d68f1eSafresh1in order to keep reasonable dclone() semantics. 84956d68f1eSafresh1 85056d68f1eSafresh1=item C<STORABLE_thaw> I<obj>, I<cloning>, I<serialized>, ... 85156d68f1eSafresh1 85256d68f1eSafresh1The deserializing hook called on the object during deserialization. 85356d68f1eSafresh1But wait: if we're deserializing, there's no object yet... right? 85456d68f1eSafresh1 85556d68f1eSafresh1Wrong: the Storable engine creates an empty one for you. If you know Eiffel, 85656d68f1eSafresh1you can view C<STORABLE_thaw> as an alternate creation routine. 85756d68f1eSafresh1 85856d68f1eSafresh1This means the hook can be inherited like any other method, and that 85956d68f1eSafresh1I<obj> is your blessed reference for this particular instance. 86056d68f1eSafresh1 86156d68f1eSafresh1The other arguments should look familiar if you know C<STORABLE_freeze>: 86256d68f1eSafresh1I<cloning> is true when we're part of a deep clone operation, I<serialized> 86356d68f1eSafresh1is the serialized string you returned to the engine in C<STORABLE_freeze>, 86456d68f1eSafresh1and there may be an optional list of references, in the same order you gave 86556d68f1eSafresh1them at serialization time, pointing to the deserialized objects (which 86656d68f1eSafresh1have been processed courtesy of the Storable engine). 86756d68f1eSafresh1 86856d68f1eSafresh1When the Storable engine does not find any C<STORABLE_thaw> hook routine, 86956d68f1eSafresh1it tries to load the class by requiring the package dynamically (using 87056d68f1eSafresh1the blessed package name), and then re-attempts the lookup. If at that 87156d68f1eSafresh1time the hook cannot be located, the engine croaks. Note that this mechanism 87256d68f1eSafresh1will fail if you define several classes in the same file, but L<perlmod> 87356d68f1eSafresh1warned you. 87456d68f1eSafresh1 87556d68f1eSafresh1It is up to you to use this information to populate I<obj> the way you want. 87656d68f1eSafresh1 87756d68f1eSafresh1Returned value: none. 87856d68f1eSafresh1 87956d68f1eSafresh1=item C<STORABLE_attach> I<class>, I<cloning>, I<serialized> 88056d68f1eSafresh1 88156d68f1eSafresh1While C<STORABLE_freeze> and C<STORABLE_thaw> are useful for classes where 88256d68f1eSafresh1each instance is independent, this mechanism has difficulty (or is 88356d68f1eSafresh1incompatible) with objects that exist as common process-level or 88456d68f1eSafresh1system-level resources, such as singleton objects, database pools, caches 88556d68f1eSafresh1or memoized objects. 88656d68f1eSafresh1 88756d68f1eSafresh1The alternative C<STORABLE_attach> method provides a solution for these 88856d68f1eSafresh1shared objects. Instead of C<STORABLE_freeze> --E<gt> C<STORABLE_thaw>, 88956d68f1eSafresh1you implement C<STORABLE_freeze> --E<gt> C<STORABLE_attach> instead. 89056d68f1eSafresh1 89156d68f1eSafresh1Arguments: I<class> is the class we are attaching to, I<cloning> is a flag 89256d68f1eSafresh1indicating whether we're in a dclone() or a regular de-serialization via 89356d68f1eSafresh1thaw(), and I<serialized> is the stored string for the resource object. 89456d68f1eSafresh1 89556d68f1eSafresh1Because these resource objects are considered to be owned by the entire 89656d68f1eSafresh1process/system, and not the "property" of whatever is being serialized, 89756d68f1eSafresh1no references underneath the object should be included in the serialized 89856d68f1eSafresh1string. Thus, in any class that implements C<STORABLE_attach>, the 89956d68f1eSafresh1C<STORABLE_freeze> method cannot return any references, and C<Storable> 90056d68f1eSafresh1will throw an error if C<STORABLE_freeze> tries to return references. 90156d68f1eSafresh1 90256d68f1eSafresh1All information required to "attach" back to the shared resource object 90356d68f1eSafresh1B<must> be contained B<only> in the C<STORABLE_freeze> return string. 90456d68f1eSafresh1Otherwise, C<STORABLE_freeze> behaves as normal for C<STORABLE_attach> 90556d68f1eSafresh1classes. 90656d68f1eSafresh1 90756d68f1eSafresh1Because C<STORABLE_attach> is passed the class (rather than an object), 90856d68f1eSafresh1it also returns the object directly, rather than modifying the passed 90956d68f1eSafresh1object. 91056d68f1eSafresh1 91156d68f1eSafresh1Returned value: object of type C<class> 91256d68f1eSafresh1 91356d68f1eSafresh1=back 91456d68f1eSafresh1 91556d68f1eSafresh1=head2 Predicates 91656d68f1eSafresh1 91756d68f1eSafresh1Predicates are not exportable. They must be called by explicitly prefixing 91856d68f1eSafresh1them with the Storable package name. 91956d68f1eSafresh1 92056d68f1eSafresh1=over 4 92156d68f1eSafresh1 92256d68f1eSafresh1=item C<Storable::last_op_in_netorder> 92356d68f1eSafresh1 92456d68f1eSafresh1The C<Storable::last_op_in_netorder()> predicate will tell you whether 92556d68f1eSafresh1network order was used in the last store or retrieve operation. If you 92656d68f1eSafresh1don't know how to use this, just forget about it. 92756d68f1eSafresh1 92856d68f1eSafresh1=item C<Storable::is_storing> 92956d68f1eSafresh1 93056d68f1eSafresh1Returns true if within a store operation (via STORABLE_freeze hook). 93156d68f1eSafresh1 93256d68f1eSafresh1=item C<Storable::is_retrieving> 93356d68f1eSafresh1 93456d68f1eSafresh1Returns true if within a retrieve operation (via STORABLE_thaw hook). 93556d68f1eSafresh1 93656d68f1eSafresh1=back 93756d68f1eSafresh1 93856d68f1eSafresh1=head2 Recursion 93956d68f1eSafresh1 94056d68f1eSafresh1With hooks comes the ability to recurse back to the Storable engine. 94156d68f1eSafresh1Indeed, hooks are regular Perl code, and Storable is convenient when 94256d68f1eSafresh1it comes to serializing and deserializing things, so why not use it 94356d68f1eSafresh1to handle the serialization string? 94456d68f1eSafresh1 94556d68f1eSafresh1There are a few things you need to know, however: 94656d68f1eSafresh1 94756d68f1eSafresh1=over 4 94856d68f1eSafresh1 94956d68f1eSafresh1=item * 95056d68f1eSafresh1 95156d68f1eSafresh1From Storable 3.05 to 3.13 we probed for the stack recursion limit for references, 95256d68f1eSafresh1arrays and hashes to a maximal depth of ~1200-35000, otherwise we might 95356d68f1eSafresh1fall into a stack-overflow. On JSON::XS this limit is 512 btw. With 95456d68f1eSafresh1references not immediately referencing each other there's no such 95556d68f1eSafresh1limit yet, so you might fall into such a stack-overflow segfault. 95656d68f1eSafresh1 95756d68f1eSafresh1This probing and the checks we performed have some limitations: 95856d68f1eSafresh1 95956d68f1eSafresh1=over 96056d68f1eSafresh1 96156d68f1eSafresh1=item * 96256d68f1eSafresh1 96356d68f1eSafresh1the stack size at build time might be different at run time, eg. the 96456d68f1eSafresh1stack size may have been modified with ulimit(1). If it's larger at 96556d68f1eSafresh1run time Storable may fail the freeze() or thaw() unnecessarily. If 96656d68f1eSafresh1it's larger at build time Storable may segmentation fault when 96756d68f1eSafresh1processing a deep structure at run time. 96856d68f1eSafresh1 96956d68f1eSafresh1=item * 97056d68f1eSafresh1 97156d68f1eSafresh1the stack size might be different in a thread. 97256d68f1eSafresh1 97356d68f1eSafresh1=item * 97456d68f1eSafresh1 97556d68f1eSafresh1array and hash recursion limits are checked separately against the 97656d68f1eSafresh1same recursion depth, a frozen structure with a large sequence of 97756d68f1eSafresh1nested arrays within many nested hashes may exhaust the processor 97856d68f1eSafresh1stack without triggering Storable's recursion protection. 97956d68f1eSafresh1 98056d68f1eSafresh1=back 98156d68f1eSafresh1 98256d68f1eSafresh1So these now have simple defaults rather than probing at build-time. 98356d68f1eSafresh1 98456d68f1eSafresh1You can control the maximum array and hash recursion depths by 98556d68f1eSafresh1modifying C<$Storable::recursion_limit> and 98656d68f1eSafresh1C<$Storable::recursion_limit_hash> respectively. Either can be set to 98756d68f1eSafresh1C<-1> to prevent any depth checks, though this isn't recommended. 98856d68f1eSafresh1 98956d68f1eSafresh1If you want to test what the limits are, the F<stacksize> tool is 99056d68f1eSafresh1included in the C<Storable> distribution. 99156d68f1eSafresh1 99256d68f1eSafresh1=item * 99356d68f1eSafresh1 99456d68f1eSafresh1You can create endless loops if the things you serialize via freeze() 99556d68f1eSafresh1(for instance) point back to the object we're trying to serialize in 99656d68f1eSafresh1the hook. 99756d68f1eSafresh1 99856d68f1eSafresh1=item * 99956d68f1eSafresh1 100056d68f1eSafresh1Shared references among objects will not stay shared: if we're serializing 100156d68f1eSafresh1the list of object [A, C] where both object A and C refer to the SAME object 100256d68f1eSafresh1B, and if there is a serializing hook in A that says freeze(B), then when 100356d68f1eSafresh1deserializing, we'll get [A', C'] where A' refers to B', but C' refers to D, 100456d68f1eSafresh1a deep clone of B'. The topology was not preserved. 100556d68f1eSafresh1 100656d68f1eSafresh1=item * 100756d68f1eSafresh1 100856d68f1eSafresh1The maximal stack recursion limit for your system is returned by 100956d68f1eSafresh1C<stack_depth()> and C<stack_depth_hash()>. The hash limit is usually 101056d68f1eSafresh1half the size of the array and ref limit, as the Perl hash API is not optimal. 101156d68f1eSafresh1 101256d68f1eSafresh1=back 101356d68f1eSafresh1 101456d68f1eSafresh1That's why C<STORABLE_freeze> lets you provide a list of references 101556d68f1eSafresh1to serialize. The engine guarantees that those will be serialized in the 101656d68f1eSafresh1same context as the other objects, and therefore that shared objects will 101756d68f1eSafresh1stay shared. 101856d68f1eSafresh1 101956d68f1eSafresh1In the above [A, C] example, the C<STORABLE_freeze> hook could return: 102056d68f1eSafresh1 102156d68f1eSafresh1 ("something", $self->{B}) 102256d68f1eSafresh1 102356d68f1eSafresh1and the B part would be serialized by the engine. In C<STORABLE_thaw>, you 102456d68f1eSafresh1would get back the reference to the B' object, deserialized for you. 102556d68f1eSafresh1 102656d68f1eSafresh1Therefore, recursion should normally be avoided, but is nonetheless supported. 102756d68f1eSafresh1 102856d68f1eSafresh1=head2 Deep Cloning 102956d68f1eSafresh1 103056d68f1eSafresh1There is a Clone module available on CPAN which implements deep cloning 103156d68f1eSafresh1natively, i.e. without freezing to memory and thawing the result. It is 103256d68f1eSafresh1aimed to replace Storable's dclone() some day. However, it does not currently 103356d68f1eSafresh1support Storable hooks to redefine the way deep cloning is performed. 103456d68f1eSafresh1 103556d68f1eSafresh1=head1 Storable magic 103656d68f1eSafresh1 103756d68f1eSafresh1Yes, there's a lot of that :-) But more precisely, in UNIX systems 103856d68f1eSafresh1there's a utility called C<file>, which recognizes data files based on 103956d68f1eSafresh1their contents (usually their first few bytes). For this to work, 104056d68f1eSafresh1a certain file called F<magic> needs to taught about the I<signature> 104156d68f1eSafresh1of the data. Where that configuration file lives depends on the UNIX 104256d68f1eSafresh1flavour; often it's something like F</usr/share/misc/magic> or 104356d68f1eSafresh1F</etc/magic>. Your system administrator needs to do the updating of 104456d68f1eSafresh1the F<magic> file. The necessary signature information is output to 104556d68f1eSafresh1STDOUT by invoking Storable::show_file_magic(). Note that the GNU 104656d68f1eSafresh1implementation of the C<file> utility, version 3.38 or later, 104756d68f1eSafresh1is expected to contain support for recognising Storable files 104856d68f1eSafresh1out-of-the-box, in addition to other kinds of Perl files. 104956d68f1eSafresh1 105056d68f1eSafresh1You can also use the following functions to extract the file header 105156d68f1eSafresh1information from Storable images: 105256d68f1eSafresh1 105356d68f1eSafresh1=over 105456d68f1eSafresh1 105556d68f1eSafresh1=item $info = Storable::file_magic( $filename ) 105656d68f1eSafresh1 105756d68f1eSafresh1If the given file is a Storable image return a hash describing it. If 105856d68f1eSafresh1the file is readable, but not a Storable image return C<undef>. If 105956d68f1eSafresh1the file does not exist or is unreadable then croak. 106056d68f1eSafresh1 106156d68f1eSafresh1The hash returned has the following elements: 106256d68f1eSafresh1 106356d68f1eSafresh1=over 106456d68f1eSafresh1 106556d68f1eSafresh1=item C<version> 106656d68f1eSafresh1 106756d68f1eSafresh1This returns the file format version. It is a string like "2.7". 106856d68f1eSafresh1 106956d68f1eSafresh1Note that this version number is not the same as the version number of 107056d68f1eSafresh1the Storable module itself. For instance Storable v0.7 create files 107156d68f1eSafresh1in format v2.0 and Storable v2.15 create files in format v2.7. The 107256d68f1eSafresh1file format version number only increment when additional features 107356d68f1eSafresh1that would confuse older versions of the module are added. 107456d68f1eSafresh1 107556d68f1eSafresh1Files older than v2.0 will have the one of the version numbers "-1", 107656d68f1eSafresh1"0" or "1". No minor number was used at that time. 107756d68f1eSafresh1 107856d68f1eSafresh1=item C<version_nv> 107956d68f1eSafresh1 108056d68f1eSafresh1This returns the file format version as number. It is a string like 108156d68f1eSafresh1"2.007". This value is suitable for numeric comparisons. 108256d68f1eSafresh1 108356d68f1eSafresh1The constant function C<Storable::BIN_VERSION_NV> returns a comparable 108456d68f1eSafresh1number that represents the highest file version number that this 108556d68f1eSafresh1version of Storable fully supports (but see discussion of 108656d68f1eSafresh1C<$Storable::accept_future_minor> above). The constant 108756d68f1eSafresh1C<Storable::BIN_WRITE_VERSION_NV> function returns what file version 108856d68f1eSafresh1is written and might be less than C<Storable::BIN_VERSION_NV> in some 108956d68f1eSafresh1configurations. 109056d68f1eSafresh1 109156d68f1eSafresh1=item C<major>, C<minor> 109256d68f1eSafresh1 109356d68f1eSafresh1This also returns the file format version. If the version is "2.7" 109456d68f1eSafresh1then major would be 2 and minor would be 7. The minor element is 109556d68f1eSafresh1missing for when major is less than 2. 109656d68f1eSafresh1 109756d68f1eSafresh1=item C<hdrsize> 109856d68f1eSafresh1 109956d68f1eSafresh1The is the number of bytes that the Storable header occupies. 110056d68f1eSafresh1 110156d68f1eSafresh1=item C<netorder> 110256d68f1eSafresh1 110356d68f1eSafresh1This is TRUE if the image store data in network order. This means 110456d68f1eSafresh1that it was created with nstore() or similar. 110556d68f1eSafresh1 110656d68f1eSafresh1=item C<byteorder> 110756d68f1eSafresh1 110856d68f1eSafresh1This is only present when C<netorder> is FALSE. It is the 110956d68f1eSafresh1$Config{byteorder} string of the perl that created this image. It is 111056d68f1eSafresh1a string like "1234" (32 bit little endian) or "87654321" (64 bit big 111156d68f1eSafresh1endian). This must match the current perl for the image to be 111256d68f1eSafresh1readable by Storable. 111356d68f1eSafresh1 111456d68f1eSafresh1=item C<intsize>, C<longsize>, C<ptrsize>, C<nvsize> 111556d68f1eSafresh1 111656d68f1eSafresh1These are only present when C<netorder> is FALSE. These are the sizes of 111756d68f1eSafresh1various C datatypes of the perl that created this image. These must 111856d68f1eSafresh1match the current perl for the image to be readable by Storable. 111956d68f1eSafresh1 112056d68f1eSafresh1The C<nvsize> element is only present for file format v2.2 and 112156d68f1eSafresh1higher. 112256d68f1eSafresh1 112356d68f1eSafresh1=item C<file> 112456d68f1eSafresh1 112556d68f1eSafresh1The name of the file. 112656d68f1eSafresh1 112756d68f1eSafresh1=back 112856d68f1eSafresh1 112956d68f1eSafresh1=item $info = Storable::read_magic( $buffer ) 113056d68f1eSafresh1 113156d68f1eSafresh1=item $info = Storable::read_magic( $buffer, $must_be_file ) 113256d68f1eSafresh1 113356d68f1eSafresh1The $buffer should be a Storable image or the first few bytes of it. 113456d68f1eSafresh1If $buffer starts with a Storable header, then a hash describing the 113556d68f1eSafresh1image is returned, otherwise C<undef> is returned. 113656d68f1eSafresh1 113756d68f1eSafresh1The hash has the same structure as the one returned by 113856d68f1eSafresh1Storable::file_magic(). The C<file> element is true if the image is a 113956d68f1eSafresh1file image. 114056d68f1eSafresh1 114156d68f1eSafresh1If the $must_be_file argument is provided and is TRUE, then return 114256d68f1eSafresh1C<undef> unless the image looks like it belongs to a file dump. 114356d68f1eSafresh1 114456d68f1eSafresh1The maximum size of a Storable header is currently 21 bytes. If the 114556d68f1eSafresh1provided $buffer is only the first part of a Storable image it should 114656d68f1eSafresh1at least be this long to ensure that read_magic() will recognize it as 114756d68f1eSafresh1such. 114856d68f1eSafresh1 114956d68f1eSafresh1=back 115056d68f1eSafresh1 115156d68f1eSafresh1=head1 EXAMPLES 115256d68f1eSafresh1 115356d68f1eSafresh1Here are some code samples showing a possible usage of Storable: 115456d68f1eSafresh1 115556d68f1eSafresh1 use Storable qw(store retrieve freeze thaw dclone); 115656d68f1eSafresh1 115756d68f1eSafresh1 %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1); 115856d68f1eSafresh1 115956d68f1eSafresh1 store(\%color, 'mycolors') or die "Can't store %a in mycolors!\n"; 116056d68f1eSafresh1 116156d68f1eSafresh1 $colref = retrieve('mycolors'); 116256d68f1eSafresh1 die "Unable to retrieve from mycolors!\n" unless defined $colref; 116356d68f1eSafresh1 printf "Blue is still %lf\n", $colref->{'Blue'}; 116456d68f1eSafresh1 116556d68f1eSafresh1 $colref2 = dclone(\%color); 116656d68f1eSafresh1 116756d68f1eSafresh1 $str = freeze(\%color); 116856d68f1eSafresh1 printf "Serialization of %%color is %d bytes long.\n", length($str); 116956d68f1eSafresh1 $colref3 = thaw($str); 117056d68f1eSafresh1 117156d68f1eSafresh1which prints (on my machine): 117256d68f1eSafresh1 117356d68f1eSafresh1 Blue is still 0.100000 117456d68f1eSafresh1 Serialization of %color is 102 bytes long. 117556d68f1eSafresh1 117656d68f1eSafresh1Serialization of CODE references and deserialization in a safe 117756d68f1eSafresh1compartment: 117856d68f1eSafresh1 117956d68f1eSafresh1=for example begin 118056d68f1eSafresh1 118156d68f1eSafresh1 use Storable qw(freeze thaw); 118256d68f1eSafresh1 use Safe; 118356d68f1eSafresh1 use strict; 118456d68f1eSafresh1 my $safe = new Safe; 118556d68f1eSafresh1 # because of opcodes used in "use strict": 118656d68f1eSafresh1 $safe->permit(qw(:default require)); 118756d68f1eSafresh1 local $Storable::Deparse = 1; 118856d68f1eSafresh1 local $Storable::Eval = sub { $safe->reval($_[0]) }; 118956d68f1eSafresh1 my $serialized = freeze(sub { 42 }); 119056d68f1eSafresh1 my $code = thaw($serialized); 119156d68f1eSafresh1 $code->() == 42; 119256d68f1eSafresh1 119356d68f1eSafresh1=for example end 119456d68f1eSafresh1 119556d68f1eSafresh1=for example_testing 119656d68f1eSafresh1 is( $code->(), 42 ); 119756d68f1eSafresh1 119856d68f1eSafresh1=head1 SECURITY WARNING 119956d68f1eSafresh1 1200*e0680481Safresh1B<Do not accept Storable documents from untrusted sources!> There is 1201*e0680481Safresh1B<no> way to configure Storable so that it can be used safely to process 1202*e0680481Safresh1untrusted data. While there I<are> various options that can be used to 1203*e0680481Safresh1mitigate specific security issues these options do I<not> comprise a 1204*e0680481Safresh1complete safety net for the user, and processing untrusted data may 1205*e0680481Safresh1result in segmentation faults, remote code execution, or privilege 1206*e0680481Safresh1escalation. The following lists some known features which represent 1207*e0680481Safresh1security issues that should be considered by users of this module. 120856d68f1eSafresh1 1209*e0680481Safresh1Most obviously, the optional (off by default) CODE reference 121056d68f1eSafresh1serialization feature allows transfer of code to the deserializing 121156d68f1eSafresh1process. Furthermore, any serialized object will cause Storable to 121256d68f1eSafresh1helpfully load the module corresponding to the class of the object in 121356d68f1eSafresh1the deserializing module. For manipulated module names, this can load 121456d68f1eSafresh1almost arbitrary code. Finally, the deserialized object's destructors 121556d68f1eSafresh1will be invoked when the objects get destroyed in the deserializing 121656d68f1eSafresh1process. Maliciously crafted Storable documents may put such objects 121756d68f1eSafresh1in the value of a hash key that is overridden by another key/value 121856d68f1eSafresh1pair in the same hash, thus causing immediate destructor execution. 121956d68f1eSafresh1 122056d68f1eSafresh1To disable blessing objects while thawing/retrieving remove the flag 122156d68f1eSafresh1C<BLESS_OK> = 2 from C<$Storable::flags> or set the 2nd argument for 122256d68f1eSafresh1thaw/retrieve to 0. 122356d68f1eSafresh1 122456d68f1eSafresh1To disable tieing data while thawing/retrieving remove the flag C<TIE_OK> 122556d68f1eSafresh1= 4 from C<$Storable::flags> or set the 2nd argument for thaw/retrieve 122656d68f1eSafresh1to 0. 122756d68f1eSafresh1 122856d68f1eSafresh1With the default setting of C<$Storable::flags> = 6, creating or destroying 122956d68f1eSafresh1random objects, even renamed objects can be controlled by an attacker. 123056d68f1eSafresh1See CVE-2015-1592 and its metasploit module. 123156d68f1eSafresh1 1232*e0680481Safresh1If your application requires accepting data from untrusted sources, you 1233*e0680481Safresh1are best off with a less powerful and more-likely safe serialization 1234*e0680481Safresh1format and implementation. If your data is sufficiently simple, 1235*e0680481Safresh1L<Cpanel::JSON::XS> or L<Data::MessagePack> are fine alternatives. For 1236*e0680481Safresh1more complex data structures containing various Perl specific data types 1237*e0680481Safresh1like regular expressions or aliased data L<Sereal> is the best 1238*e0680481Safresh1alternative and offers maximum interoperability. Note that Sereal is 1239*e0680481Safresh1L<unsafe by default|Sereal::Decoder/ROBUSTNESS>, but you can configure 1240*e0680481Safresh1the encoder and decoder to mitigate any security issues. 124156d68f1eSafresh1 124256d68f1eSafresh1=head1 WARNING 124356d68f1eSafresh1 124456d68f1eSafresh1If you're using references as keys within your hash tables, you're bound 124556d68f1eSafresh1to be disappointed when retrieving your data. Indeed, Perl stringifies 124656d68f1eSafresh1references used as hash table keys. If you later wish to access the 124756d68f1eSafresh1items via another reference stringification (i.e. using the same 124856d68f1eSafresh1reference that was used for the key originally to record the value into 124956d68f1eSafresh1the hash table), it will work because both references stringify to the 125056d68f1eSafresh1same string. 125156d68f1eSafresh1 125256d68f1eSafresh1It won't work across a sequence of C<store> and C<retrieve> operations, 125356d68f1eSafresh1however, because the addresses in the retrieved objects, which are 125456d68f1eSafresh1part of the stringified references, will probably differ from the 125556d68f1eSafresh1original addresses. The topology of your structure is preserved, 125656d68f1eSafresh1but not hidden semantics like those. 125756d68f1eSafresh1 125856d68f1eSafresh1On platforms where it matters, be sure to call C<binmode()> on the 125956d68f1eSafresh1descriptors that you pass to Storable functions. 126056d68f1eSafresh1 126156d68f1eSafresh1Storing data canonically that contains large hashes can be 126256d68f1eSafresh1significantly slower than storing the same data normally, as 126356d68f1eSafresh1temporary arrays to hold the keys for each hash have to be allocated, 126456d68f1eSafresh1populated, sorted and freed. Some tests have shown a halving of the 126556d68f1eSafresh1speed of storing -- the exact penalty will depend on the complexity of 126656d68f1eSafresh1your data. There is no slowdown on retrieval. 126756d68f1eSafresh1 126856d68f1eSafresh1=head1 REGULAR EXPRESSIONS 126956d68f1eSafresh1 127056d68f1eSafresh1Storable now has experimental support for storing regular expressions, 127156d68f1eSafresh1but there are significant limitations: 127256d68f1eSafresh1 127356d68f1eSafresh1=over 127456d68f1eSafresh1 127556d68f1eSafresh1=item * 127656d68f1eSafresh1 127756d68f1eSafresh1perl 5.8 or later is required. 127856d68f1eSafresh1 127956d68f1eSafresh1=item * 128056d68f1eSafresh1 128156d68f1eSafresh1regular expressions with code blocks, ie C</(?{ ... })/> or C</(??{ 128256d68f1eSafresh1... })/> will throw an exception when thawed. 128356d68f1eSafresh1 128456d68f1eSafresh1=item * 128556d68f1eSafresh1 128656d68f1eSafresh1regular expression syntax and flags have changed over the history of 128756d68f1eSafresh1perl, so a regular expression that you freeze in one version of perl 128856d68f1eSafresh1may fail to thaw or behave differently in another version of perl. 128956d68f1eSafresh1 129056d68f1eSafresh1=item * 129156d68f1eSafresh1 129256d68f1eSafresh1depending on the version of perl, regular expressions can change in 129356d68f1eSafresh1behaviour depending on the context, but later perls will bake that 129456d68f1eSafresh1behaviour into the regexp. 129556d68f1eSafresh1 129656d68f1eSafresh1=back 129756d68f1eSafresh1 129856d68f1eSafresh1Storable will throw an exception if a frozen regular expression cannot 129956d68f1eSafresh1be thawed. 130056d68f1eSafresh1 130156d68f1eSafresh1=head1 BUGS 130256d68f1eSafresh1 130356d68f1eSafresh1You can't store GLOB, FORMLINE, etc.... If you can define semantics 130456d68f1eSafresh1for those operations, feel free to enhance Storable so that it can 130556d68f1eSafresh1deal with them. 130656d68f1eSafresh1 130756d68f1eSafresh1The store functions will C<croak> if they run into such references 130856d68f1eSafresh1unless you set C<$Storable::forgive_me> to some C<TRUE> value. In that 130956d68f1eSafresh1case, the fatal message is converted to a warning and some meaningless 131056d68f1eSafresh1string is stored instead. 131156d68f1eSafresh1 131256d68f1eSafresh1Setting C<$Storable::canonical> may not yield frozen strings that 131356d68f1eSafresh1compare equal due to possible stringification of numbers. When the 131456d68f1eSafresh1string version of a scalar exists, it is the form stored; therefore, 131556d68f1eSafresh1if you happen to use your numbers as strings between two freezing 131656d68f1eSafresh1operations on the same data structures, you will get different 131756d68f1eSafresh1results. 131856d68f1eSafresh1 131956d68f1eSafresh1When storing doubles in network order, their value is stored as text. 132056d68f1eSafresh1However, you should also not expect non-numeric floating-point values 132156d68f1eSafresh1such as infinity and "not a number" to pass successfully through a 132256d68f1eSafresh1nstore()/retrieve() pair. 132356d68f1eSafresh1 132456d68f1eSafresh1As Storable neither knows nor cares about character sets (although it 132556d68f1eSafresh1does know that characters may be more than eight bits wide), any difference 132656d68f1eSafresh1in the interpretation of character codes between a host and a target 132756d68f1eSafresh1system is your problem. In particular, if host and target use different 132856d68f1eSafresh1code points to represent the characters used in the text representation 132956d68f1eSafresh1of floating-point numbers, you will not be able be able to exchange 133056d68f1eSafresh1floating-point data, even with nstore(). 133156d68f1eSafresh1 133256d68f1eSafresh1C<Storable::drop_utf8> is a blunt tool. There is no facility either to 133356d68f1eSafresh1return B<all> strings as utf8 sequences, or to attempt to convert utf8 133456d68f1eSafresh1data back to 8 bit and C<croak()> if the conversion fails. 133556d68f1eSafresh1 133656d68f1eSafresh1Prior to Storable 2.01, no distinction was made between signed and 133756d68f1eSafresh1unsigned integers on storing. By default Storable prefers to store a 133856d68f1eSafresh1scalars string representation (if it has one) so this would only cause 133956d68f1eSafresh1problems when storing large unsigned integers that had never been converted 134056d68f1eSafresh1to string or floating point. In other words values that had been generated 134156d68f1eSafresh1by integer operations such as logic ops and then not used in any string or 134256d68f1eSafresh1arithmetic context before storing. 134356d68f1eSafresh1 134456d68f1eSafresh1=head2 64 bit data in perl 5.6.0 and 5.6.1 134556d68f1eSafresh1 134656d68f1eSafresh1This section only applies to you if you have existing data written out 134756d68f1eSafresh1by Storable 2.02 or earlier on perl 5.6.0 or 5.6.1 on Unix or Linux which 134856d68f1eSafresh1has been configured with 64 bit integer support (not the default) 134956d68f1eSafresh1If you got a precompiled perl, rather than running Configure to build 135056d68f1eSafresh1your own perl from source, then it almost certainly does not affect you, 135156d68f1eSafresh1and you can stop reading now (unless you're curious). If you're using perl 135256d68f1eSafresh1on Windows it does not affect you. 135356d68f1eSafresh1 135456d68f1eSafresh1Storable writes a file header which contains the sizes of various C 135556d68f1eSafresh1language types for the C compiler that built Storable (when not writing in 135656d68f1eSafresh1network order), and will refuse to load files written by a Storable not 135756d68f1eSafresh1on the same (or compatible) architecture. This check and a check on 135856d68f1eSafresh1machine byteorder is needed because the size of various fields in the file 135956d68f1eSafresh1are given by the sizes of the C language types, and so files written on 136056d68f1eSafresh1different architectures are incompatible. This is done for increased speed. 136156d68f1eSafresh1(When writing in network order, all fields are written out as standard 136256d68f1eSafresh1lengths, which allows full interworking, but takes longer to read and write) 136356d68f1eSafresh1 136456d68f1eSafresh1Perl 5.6.x introduced the ability to optional configure the perl interpreter 136556d68f1eSafresh1to use C's C<long long> type to allow scalars to store 64 bit integers on 32 136656d68f1eSafresh1bit systems. However, due to the way the Perl configuration system 136756d68f1eSafresh1generated the C configuration files on non-Windows platforms, and the way 136856d68f1eSafresh1Storable generates its header, nothing in the Storable file header reflected 136956d68f1eSafresh1whether the perl writing was using 32 or 64 bit integers, despite the fact 137056d68f1eSafresh1that Storable was storing some data differently in the file. Hence Storable 137156d68f1eSafresh1running on perl with 64 bit integers will read the header from a file 137256d68f1eSafresh1written by a 32 bit perl, not realise that the data is actually in a subtly 137356d68f1eSafresh1incompatible format, and then go horribly wrong (possibly crashing) if it 137456d68f1eSafresh1encountered a stored integer. This is a design failure. 137556d68f1eSafresh1 137656d68f1eSafresh1Storable has now been changed to write out and read in a file header with 137756d68f1eSafresh1information about the size of integers. It's impossible to detect whether 137856d68f1eSafresh1an old file being read in was written with 32 or 64 bit integers (they have 137956d68f1eSafresh1the same header) so it's impossible to automatically switch to a correct 138056d68f1eSafresh1backwards compatibility mode. Hence this Storable defaults to the new, 138156d68f1eSafresh1correct behaviour. 138256d68f1eSafresh1 138356d68f1eSafresh1What this means is that if you have data written by Storable 1.x running 138456d68f1eSafresh1on perl 5.6.0 or 5.6.1 configured with 64 bit integers on Unix or Linux 138556d68f1eSafresh1then by default this Storable will refuse to read it, giving the error 138656d68f1eSafresh1I<Byte order is not compatible>. If you have such data then you 138756d68f1eSafresh1should set C<$Storable::interwork_56_64bit> to a true value to make this 138856d68f1eSafresh1Storable read and write files with the old header. You should also 138956d68f1eSafresh1migrate your data, or any older perl you are communicating with, to this 139056d68f1eSafresh1current version of Storable. 139156d68f1eSafresh1 139256d68f1eSafresh1If you don't have data written with specific configuration of perl described 139356d68f1eSafresh1above, then you do not and should not do anything. Don't set the flag - 139456d68f1eSafresh1not only will Storable on an identically configured perl refuse to load them, 139556d68f1eSafresh1but Storable a differently configured perl will load them believing them 139656d68f1eSafresh1to be correct for it, and then may well fail or crash part way through 139756d68f1eSafresh1reading them. 139856d68f1eSafresh1 139956d68f1eSafresh1=head1 CREDITS 140056d68f1eSafresh1 140156d68f1eSafresh1Thank you to (in chronological order): 140256d68f1eSafresh1 140356d68f1eSafresh1 Jarkko Hietaniemi <jhi@iki.fi> 140456d68f1eSafresh1 Ulrich Pfeifer <pfeifer@charly.informatik.uni-dortmund.de> 140556d68f1eSafresh1 Benjamin A. Holzman <bholzman@earthlink.net> 140656d68f1eSafresh1 Andrew Ford <A.Ford@ford-mason.co.uk> 140756d68f1eSafresh1 Gisle Aas <gisle@aas.no> 140856d68f1eSafresh1 Jeff Gresham <gresham_jeffrey@jpmorgan.com> 140956d68f1eSafresh1 Murray Nesbitt <murray@activestate.com> 141056d68f1eSafresh1 Marc Lehmann <pcg@opengroup.org> 141156d68f1eSafresh1 Justin Banks <justinb@wamnet.com> 141256d68f1eSafresh1 Jarkko Hietaniemi <jhi@iki.fi> (AGAIN, as perl 5.7.0 Pumpkin!) 141356d68f1eSafresh1 Salvador Ortiz Garcia <sog@msg.com.mx> 141456d68f1eSafresh1 Dominic Dunlop <domo@computer.org> 141556d68f1eSafresh1 Erik Haugan <erik@solbors.no> 141656d68f1eSafresh1 Benjamin A. Holzman <ben.holzman@grantstreet.com> 141756d68f1eSafresh1 Reini Urban <rurban@cpan.org> 141856d68f1eSafresh1 Todd Rinaldo <toddr@cpanel.net> 141956d68f1eSafresh1 Aaron Crane <arc@cpan.org> 142056d68f1eSafresh1 142156d68f1eSafresh1for their bug reports, suggestions and contributions. 142256d68f1eSafresh1 142356d68f1eSafresh1Benjamin Holzman contributed the tied variable support, Andrew Ford 142456d68f1eSafresh1contributed the canonical order for hashes, and Gisle Aas fixed 142556d68f1eSafresh1a few misunderstandings of mine regarding the perl internals, 142656d68f1eSafresh1and optimized the emission of "tags" in the output streams by 142756d68f1eSafresh1simply counting the objects instead of tagging them (leading to 142856d68f1eSafresh1a binary incompatibility for the Storable image starting at version 142956d68f1eSafresh10.6--older images are, of course, still properly understood). 143056d68f1eSafresh1Murray Nesbitt made Storable thread-safe. Marc Lehmann added overloading 143156d68f1eSafresh1and references to tied items support. Benjamin Holzman added a performance 143256d68f1eSafresh1improvement for overloaded classes; thanks to Grant Street Group for footing 143356d68f1eSafresh1the bill. 1434eac174f2Safresh1Reini Urban took over maintenance from p5p, and added security fixes 143556d68f1eSafresh1and huge object support. 143656d68f1eSafresh1 143756d68f1eSafresh1=head1 AUTHOR 143856d68f1eSafresh1 143956d68f1eSafresh1Storable was written by Raphael Manfredi 144056d68f1eSafresh1F<E<lt>Raphael_Manfredi@pobox.comE<gt>> 144156d68f1eSafresh1Maintenance is now done by cperl L<http://perl11.org/cperl> 144256d68f1eSafresh1 144356d68f1eSafresh1Please e-mail us with problems, bug fixes, comments and complaints, 144456d68f1eSafresh1although if you have compliments you should send them to Raphael. 144556d68f1eSafresh1Please don't e-mail Raphael with problems, as he no longer works on 144656d68f1eSafresh1Storable, and your message will be delayed while he forwards it to us. 144756d68f1eSafresh1 144856d68f1eSafresh1=head1 SEE ALSO 144956d68f1eSafresh1 145056d68f1eSafresh1L<Clone>. 145156d68f1eSafresh1 145256d68f1eSafresh1=cut 1453