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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |