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 my $self = bless [], shift;
79 my ($idx, $value) = @_;
80 $self->[$idx] = $value;
92 my $self = bless \$scalar, shift;
98 $main::scalar_fetch++;
114 return bless [@_], $pkg;
119 my ($href, $key) = @$self;
122 return $href->{$key} = 1;
130 $c = tie %hash, TIED_HASH;
131 $d = tie @array, TIED_ARRAY;
132 tie $scalar, TIED_SCALAR;
135 #$hash{'attribute'} = \$d;
137 #$array[1] = \$scalar;
140 ### $hash{'attribute'} = $d;
141 ### below, then dump() incorectly dumps the hash value as a string the second
142 ### time it is reached. I have not investigated enough to tell whether it's
143 ### a bug in my dump() routine or in the Perl tieing mechanism.
145 $hash{'attribute'} = 'plain value';
146 $array[0] = \$scalar;
150 @tied = (\$scalar, \@array, \%hash);
151 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
152 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
153 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
155 ok 1, defined($f = freeze(\@a));
157 $dumped = &dump(\@a);
166 ### Used to see the manifestation of the bug documented above.
167 ### print "original: $dumped";
168 ### print "--------\n";
169 ### print "got: $got";
170 ### print "--------\n";
172 ok 5, $got eq $dumped;
175 ok 6, length($f) == length($g);
177 # Ensure the tied items in the retrieved image work
178 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
179 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
180 @type = qw(SCALAR ARRAY HASH);
182 ok 7, tied $$tscalar;
183 ok 8, tied @{$tarray};
184 ok 9, tied %{$thash};
186 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
187 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
190 for ($i = 0; $i < @new; $i++) {
191 print "not " unless $new[$i] == $old[$i] + 1;
192 printf "ok %d\n", 10 + 2*$i; # Tests 10,12,14
193 print "not " unless ref $tied[$i] eq $type[$i];
194 printf "ok %d\n", 11 + 2*$i; # Tests 11,13,15
199 tie $h->{'x'}, 'FAULT', $h, 'x';
202 ok 17, $FAULT::fault == 0;
203 ok 18, $h->{'x'} == 1;
204 ok 19, $FAULT::fault == 1;
208 ok 21, $ht->{'x'} == 1;
209 ok 22, $FAULT::fault == 2;
213 use Storable qw(freeze thaw);
216 sub TIESCALAR { bless \$a } sub FETCH { "ok " }
217 tie $a, P; my $r = thaw freeze \$a; $b = $$r;