mkurtis has asked for the wisdom of the Perl Monks concerning the following question:

I have Storable 2.12. I am trying to make it save my hash so that I can get it back and view it later.
#!/usr/bin/perl -w use strict; use Storable; for($i=0; $i<5001; $i++) { our %docs; open(IN,"/info/$i.txt") || die $!; my @file = <IN>; $docs{$i} = @file; close(IN); } store(\%docs, '/info/docs');
And this appears to save it correctly. But then I try to get the stuff back...
#!/usr/bin/perl -w use strict; use Storable; $docs = retrieve('/info/docs'); print $docs{1};
and this gives me a use of uninitialized value in print error.

Thanks

Replies are listed 'Best First'.
Re: Making Storable Work
by Zaxo (Archbishop) on Apr 12, 2004 at 01:35 UTC

    I'm not sure it relates to your problem, but line 10 of your storage script is in error. The value in a hash pair must be a scalar, so you want perhaps     $docs{$i} = \@file; Have you looked at the file content? It it what you expected? Your code will store line counts.

    After Compline,
    Zaxo

      Thank you saintmike and Zaxo, I didn't know how to use hash references before, I looked it up and now I got it. If anybody else doesn't know how to do it here it is:
      %docs = %{$docs};
      Just use that and it will fill %docs with your original hash.

      mkurtis

Re: Making Storable Work
by saintmike (Vicar) on Apr 12, 2004 at 01:29 UTC
    You're using use strict in both snippets, but none of them compiles, and exactly because you're violating use strict requirements.

    Especially

    $docs = retrieve('/info/docs'); print $docs{1};
    causes problems because you're retrieving a hash reference ($docs) which you're using as a hash (%docs or $docs{1}) later on. use strict catches this.

    Also, checking the result of store() makes sure it's really doing what you expect it to do.