3 # Copyright (c) 1995-2000, Raphael Manfredi
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
12 @INC = ('.', '../lib', '../ext/Storable/t');
16 require Config; import Config;
17 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
18 print "1..0 # Skip: Storable was not built\n";
26 use Storable qw(freeze thaw);
30 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
35 my $self = bless {}, shift;
48 my ($key, $value) = @_;
49 $self->{$key} = $value;
66 return join(":", keys %$self) . ";" . join(":", values %$self);
70 my ($self, $cloning, $frozen) = @_;
71 my ($keys, $values) = split(/;/, $frozen);
72 my @keys = split(/:/, $keys);
73 my @values = split(/:/, $values);
74 for (my $i = 0; $i < @keys; $i++) {
75 $self->{$keys[$i]} = $values[$i];
83 my $self = bless [], shift;
96 my ($idx, $value) = @_;
97 $self->[$idx] = $value;
105 sub STORABLE_freeze {
107 $main::array_hook1++;
108 return join(":", @$self);
112 my ($self, $cloning, $frozen) = @_;
113 @$self = split(/:/, $frozen);
114 $main::array_hook2++;
121 my $self = bless \$scalar, shift;
127 $main::scalar_fetch++;
137 sub STORABLE_freeze {
139 $main::scalar_hook1++;
144 my ($self, $cloning, $frozen) = @_;
146 $main::scalar_hook2++;
154 $c = tie %hash, TIED_HASH;
155 $d = tie @array, TIED_ARRAY;
156 tie $scalar, TIED_SCALAR;
159 $hash{'attribute'} = 'plain value';
160 $array[0] = \$scalar;
163 $array[3] = "plaine scalaire";
165 @tied = (\$scalar, \@array, \%hash);
166 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
167 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
168 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
170 ok 1, defined($f = freeze(\@a));
172 $dumped = &dump(\@a);
181 ok 5, $got ne $dumped; # our hooks did not handle refs in array
184 ok 6, length($f) == length($g);
186 # Ensure the tied items in the retrieved image work
187 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
188 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
189 @type = qw(SCALAR ARRAY HASH);
191 ok 7, tied $$tscalar;
192 ok 8, tied @{$tarray};
193 ok 9, tied %{$thash};
195 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
196 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
199 for ($i = 0; $i < @new; $i++) {
200 ok 10 + 2*$i, $new[$i] == $old[$i] + 1; # Tests 10,12,14
201 ok 11 + 2*$i, ref $tied[$i] eq $type[$i]; # Tests 11,13,15
204 ok 16, $$tscalar eq 'foo';
205 ok 17, $tarray->[3] eq 'plaine scalaire';
206 ok 18, $thash->{'attribute'} eq 'plain value';
208 # Ensure hooks were called
209 ok 19, ($scalar_hook1 && $scalar_hook2);
210 ok 20, ($array_hook1 && $array_hook2);
211 ok 21, ($hash_hook1 && $hash_hook2);
214 # And now for the "blessed ref to tied hash" with "store hook" test...
217 my $bc = bless \%hash, 'FOO'; # FOO does not exist -> no hook
218 my $bx = thaw freeze $bc;
220 ok 22, ref $bx eq 'FOO';
221 my $old_hash_fetch = $hash_fetch;
222 my $v = $bx->{attribute};
223 ok 23, $hash_fetch == $old_hash_fetch + 1; # Still tied
225 package TIED_HASH_REF;
228 sub STORABLE_freeze {
229 my ($self, $cloning) = @_;
235 my ($self, $cloning, $data) = @_;
241 $bc = bless \%hash, 'TIED_HASH_REF';
242 $bx = thaw freeze $bc;
244 ok 24, ref $bx eq 'TIED_HASH_REF';
245 $old_hash_fetch = $hash_fetch;
246 $v = $bx->{attribute};
247 ok 25, $hash_fetch == $old_hash_fetch + 1; # Still tied