[Perldl] PDL::IO::Storable and dclone

Chad Davis chad.a.davis at gmail.com
Wed Mar 23 02:59:24 HST 2011

This seems to be a bug, but I'm hoping that someone that understand
Storable better than I can shed some light on this. I checked the list
archives and didn't find any discussions on this specific issue.

I use Storable, which is implemented for piddles with
PDL::IO::Storable, to serialize to files. I also use Storable::dclone
in certain algorithms that need to clone data structures that
(sometimes) contain piddles. dclone (deep clone) fails to make a deep
clone of piddles, though. This breaks the documented semantics of
Storable, that dclone(.) == thaw(freeze(.))

I tested this on the development release just announced
(PDL-2.4.7_013), by adding a couple tests to ./t/storable.t (patch
attached), like this:

# Test that freeze + thaw results in new object
$seq1 = sequence(3);
$seq1_tf = thaw(freeze($seq1));
$seq1->slice('1') .= 9;
ok(! all($seq1 == $seq1_tf), 'Initialization from seraialized object') or
    diag($seq1, $seq1_tf);

# Test that dclone results in a new object
# i.e. that dclone(.) == thaw(freeze(.))
$seq2 = sequence(4);
$seq2_dc = Storable::dclone($seq2);
$seq2->slice('2') .= 8;
ok(! all($seq2 == $seq2_dc), 'Initialization from dclone object') or
    diag($seq2, $seq2_dc);

The first test thaw(freeze(.)) passes, but the dclone(.) test does
not. This is because PDL::IO::Storable has a 'return if $cloning' both
in STORABLE_freeze() and STORABLE_thaw(). Presumably, this is where
I'm looking for some feedback, this is to allow Perl to do it's own
(efficient) cloning, rather than serialize and deserialize potentially
large objects. This doesn't work for piddles, though. It just results
in the Perl data structure being cloned, while the underlying piddle
data points to the original memory. I.e. modifying the clone then
modifies the original. Since $cloning is not 'true' during a
serialization, I removed the 'return if $cloning' from both functions
(patch attached). This doesn't, I believe, change standard
serialization or deserialization, but it does make cloning via dclone
possible. Of course, there are probably more efficient ways of cloning
a piddle, but it does now conform to the Storable inferface. I
verified that all tests still pass with this patch. (I didn't compile
with PDL::FFTW or Proj4 however).

I've been using this modified PDL::IO::Storable successfully for quite
a while, but wanted to mention it finally, having seen the
announcements of the new dev release.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: storable.t.patch
Type: application/octet-stream
Size: 946 bytes
Desc: not available
URL: <http://mailman.jach.hawaii.edu/pipermail/perldl/attachments/20110323/7d256a9e/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: storable.pd.patch
Type: application/octet-stream
Size: 760 bytes
Desc: not available
URL: <http://mailman.jach.hawaii.edu/pipermail/perldl/attachments/20110323/7d256a9e/attachment-0001.obj>

More information about the Perldl mailing list