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

I use Perl only occassionaly and hope that question isn't too trivial.

I got a file 'func_data' with a stored hash structure called FuncAssociateData
(but I don't know how the file was originally created :( ).

The first line of the file looks like:
pst0^D^D^D1234^D^D^D^H^Q^QFuncAssociateData^C^D^@^@^@^D^C{^\^@^@

and I can read the content of the file func_data using:

use Storable; %FuncAssociateData = %{ retrieve("func_data") };
and than use the hash without problem.

If I read the file content with

$href = retrieve("func_data"); print "HREF: $href"
I get
HREF: FuncAssociateData=HASH(0x8206554)

---

My problem starts, when I try to store the hash structure again in a file using:

use Storable; store(\%FuncAssociateData ,"new_func_data");

The file format looks similar, but not identical to the original file and the first line in this file looks like:
pst0^D^F^D1234^D^D^D^H^C^D^@^@^@^D^C{^\^@^@

Note that in contrast to the original file the name of the hash variable 'FuncAssociateData' is missing
and if I read the content with

$href = retrieve("new_func_data"); print "HREF: $href"
it gives
HREF: HASH(0x8206578)

How do I store a hash structure in a file, so that it is stored in the format as in the original file (including the name of the variable containing the hash) ???

e.g.
pst0^D^D^D1234^D^D^D^H^Q^QFuncAssociateData^C^D^@^@^@^D^C{^\^@^@
and NOT just
pst0^D^F^D1234^D^D^D^H^C^D^@^@^@^D^C{^\^@^@

The program using this data file works only with the first format, not with the second one.
Thanks.

Replies are listed 'Best First'.
Re: Storing hash to disk with variable name
by Abigail-II (Bishop) on Feb 23, 2004 at 16:25 UTC
    Storable doesn't store the name of the variable (that would only be marginally useful, if at all). But what it does store is the class name of any blessed structure. My bet is that whatever is stored in the file "func_data" was a hashref blessed into the class FuncAssociateData. And what you are storing in "new_func_data" is no longer blessed, so FuncAssociateData doesn't appear there.

    Abigail

      Thanks Abigail, that sounds very much like the kind of explanation I was looking for !!!

      Now please for the Perl challenged programmer who only uses Python and C:

      How do I "bless a hashref into the class FuncAssociateData" ???

        How do I "bless a hashref into the class FuncAssociateData" ???
        Take a guess.

        Abigail

Re: Storing hash to disk with variable name
by davido (Cardinal) on Feb 23, 2004 at 17:18 UTC
    Though it sounds a little dangerous to be reading variable names in from a file, if I had to do it I would probably just push one more layer of abstraction into the datastructure. That is... something like this:

    my %name = ( this => 1, that =>2 ); my $storethis = { 'name' => \%name };

    ...and then store that structure.... its name is the name of the only hash key in the top level.


    Dave

Re: Storing hash to disk with variable name
by bean (Monk) on Feb 23, 2004 at 18:43 UTC
    Although your problem has already been solved, I would suggest using Data::Dumper for debugging instead of trying to make sense of the binary data you get when Storable serializes your variable. Data::Dumper serializes variables into a human readable format. Since you aren't all that familiar with Perl you may have still been confused, but you would have started with an easier question - something like, "How do I turn a hash into a FuncAssociateData, and what does 'bless' mean?", which would not have relied on Abigail's uncommon insight to answer...