antiquark:

Several things:

OK... now that I've said that, I've gotten curious. I just kicked out a quickie example:

#!/usr/bin/perl use strict; use warnings; my %hsh = ( Apple=>1, Banana=>7, Cherry=>42 ); my $txt = "The quick red fox jumped over the lazy brown dog"; my @ary = (17, 'Flugelhorn', 13.333); use Storable qw( nstore_fd ); open my $SF, '>', 'tst_Storable.db' or die "Error: $!\n"; nstore_fd \%hsh, $SF; nstore_fd \$txt, $SF; nstore_fd \@ary, $SF; close $SF;

That writes some variables to the test file, and now to read and display the data:

#!/usr/bin/perl use strict; use warnings; my %hsh; my $txt; my @ary; use Storable qw( fd_retrieve ); open my $SF, '+<', 'tst_Storable.db' or die "Error: $!\n"; %hsh = (%{fd_retrieve $SF}); $txt = ${fd_retrieve $SF}; @ary = @{fd_retrieve $SF}; close $SF; print "Text: '$txt'\n"; print "Array: (", join(', ', @ary), ")\n"; print "Hash: (", join(', ', map { "$_=>$hsh{$_}" } keys %hsh), ")\n";

So when I run it, I get the expected results:

roboticus@Boink:~ $ perl tst_Storable_2.pl Text: 'The quick red fox jumped over the lazy brown dog' Array: (17, Flugelhorn, 13.333) Hash: (Cherry=>42, Banana=>7, Apple=>1)

You can definitely store multiple objects with Storable, so once you read an object, it shouldn't matter that there's extra data. So I'm guessing that either you need to:

Personally, I think I'd just use "read" mode to read all the objects, and at the end of the program, open in "write" mode to store everything. The fact that you're using read/write leads me to suspect that you may be seeking around the file reading and writing objects and confusing Storable so it can't keep track of the state of the objects since its bookkeeping information isn't where it expects to find it.

If that's what you're doing, then an ugly hack may be to write a sentinel value after (and possibly before) every value you may want to read randomly. That way, writing the sentinel may give Storable enough bookeeping information about the end of the object (if that's what it's lacking).

...roboticus


In reply to Re: Storable.pm - corrupt when saving to non-truncated file by roboticus
in thread Storable.pm - corrupt when saving to non-truncated file by antiquark

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.