I have a simple of example of using Storable on singleton objects that must have a simple error, but I sure can't find it. I appeal to the wisdom of the monks...

In the example below. The package Number is a silly singleton. I then have one script that creates a structure containing two references to the same singleton object. It is then store'd. Then a second script reads this and prints the object. The second occurence of the singleton is not restored.

package Number; use strict; # Hash to hold singletons use vars qw ($numbers); $numbers = {}; sub new { my ($class, $number, $text) = @_; # Check for and return an existing singleton return $numbers->{$number} if ($numbers->{$number}); # Build the object my $self = {}; bless($self, $class); $self->{'number'} = $number; $self->{'text'} = $text; # Store it as the singleton definition and return $numbers->{$number} = $self; return $numbers->{$number}; } sub asNumber { my ($self) = @_; return $self->{'number'}; } sub asText { my ($self) = @_; return $self->{'text'}; } sub STORABLE_freeze { my ($self, $cloning) = @_; my $num = $self->asNumber(); my $txt = $self->asText(); print "Freeze $num $txt\n"; return "$num$txt"; } sub STORABLE_attach { my ($class, $cloning, $serialized) = @_; my ($num, $txt) = $serialized =~ /(\d+)(\w+)/; my $self = $class->new($num, $txt); print "Attach $num $txt $self\n"; return $self; } 1;

Here is the script which creates the objects and stores them

#!/usr/bin/env dvperl use strict; use Number; use Storable; my $stuff = {'one' => new Number(1, "One"), 'two' => new Number(2, "Two"), 'twotwo' => new Number(2, "Two")}; foreach my $key (keys %$stuff) { my $number = $stuff->{$key}; my $num = $number->asNumber(); my $txt = $number->asText(); print ("Key: ($key)\tNum: ($num)\tTxt: ($txt)\tObject: ($number)\n +"); } store $stuff, 'storefile';

When I run this I get the expected output

Key: (twotwo)	Num: (2)	Txt: (Two)	Object: (Number=HASH(0x1b97c6a0))
Key: (one)	Num: (1)	Txt: (One)	Object: (Number=HASH(0x1b95ff78))
Key: (two)	Num: (2)	Txt: (Two)	Object: (Number=HASH(0x1b97c6a0))
Freeze 2 Two
Freeze 1 One

This has printed the 'one', 'two', and 'twotwo' elements, and you can see that 'two' and 'twotwo' refer to the same address

The following script reads this and prints it

#!/usr/bin/env dvperl use strict; use Number; use Storable; my $stuff; $stuff = retrieve 'storefile'; foreach my $key (keys %$stuff) { my $number = $stuff->{$key}; my $num = $number->asNumber(); my $txt = $number->asText(); print ("Key: ($key)\tNum: ($num)\tTxt: ($txt)\tObject: ($number)\n +"); }

When I run this I get:

Attach 2 Two Number=HASH(0xf3cc200)
Attach 1 One Number=HASH(0xf3cc7d0)
Key: (twotwo)	Num: (2)	Txt: (Two)	Object: (Number=HASH(0xf3cc200))
Key: (one)	Num: (1)	Txt: (One)	Object: (Number=HASH(0xf3cc7d0))
Key: (two)	Num: ()	Txt: ()	Object: (Number=HASH(0xf1c2f78))

This looks good at the beginning where only 2 calls to attach are present, but when the structure is printed the elements are not good in the second reference, and the address is not the same

please help...


In reply to Basic STORABLE_attach scenario doesn't work by jaylaw64

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.