3 # $Id: tied.t,v 1.0 2000/09/01 19:40:42 ram Exp $
5 # Copyright (c) 1995-2000, Raphael Manfredi
7 # You may redistribute only under the same terms as Perl 5, as specified
8 # in the README file that comes with the distribution.
11 # Revision 1.0 2000/09/01 19:40:42 ram
12 # Baseline for first official release.
18 @INC = ('.', '../lib', '../ext/Storable/t');
22 require Config; import Config;
23 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
24 print "1..0 # Skip: Storable was not built\n";
32 use Storable qw(freeze thaw);
36 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
41 my $self = bless {}, shift;
54 my ($key, $value) = @_;
55 $self->{$key} = $value;
72 my $self = bless [], shift;
85 my ($idx, $value) = @_;
86 $self->[$idx] = $value;
98 my $self = bless \$scalar, shift;
104 $main::scalar_fetch++;
120 return bless [@_], $pkg;
125 my ($href, $key) = @$self;
128 return $href->{$key} = 1;
136 $c = tie %hash, TIED_HASH;
137 $d = tie @array, TIED_ARRAY;
138 tie $scalar, TIED_SCALAR;
141 #$hash{'attribute'} = \$d;
143 #$array[1] = \$scalar;
146 ### $hash{'attribute'} = $d;
147 ### below, then dump() incorectly dumps the hash value as a string the second
148 ### time it is reached. I have not investigated enough to tell whether it's
149 ### a bug in my dump() routine or in the Perl tieing mechanism.
151 $hash{'attribute'} = 'plain value';
152 $array[0] = \$scalar;
156 @tied = (\$scalar, \@array, \%hash);
157 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
158 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
159 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
161 ok 1, defined($f = freeze(\@a));
163 $dumped = &dump(\@a);
172 ### Used to see the manifestation of the bug documented above.
173 ### print "original: $dumped";
174 ### print "--------\n";
175 ### print "got: $got";
176 ### print "--------\n";
178 ok 5, $got eq $dumped;
181 ok 6, length($f) == length($g);
183 # Ensure the tied items in the retrieved image work
184 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
185 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
186 @type = qw(SCALAR ARRAY HASH);
188 ok 7, tied $$tscalar;
189 ok 8, tied @{$tarray};
190 ok 9, tied %{$thash};
192 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
193 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
196 for ($i = 0; $i < @new; $i++) {
197 print "not " unless $new[$i] == $old[$i] + 1;
198 printf "ok %d\n", 10 + 2*$i; # Tests 10,12,14
199 print "not " unless ref $tied[$i] eq $type[$i];
200 printf "ok %d\n", 11 + 2*$i; # Tests 11,13,15
205 tie $h->{'x'}, 'FAULT', $h, 'x';
208 ok 17, $FAULT::fault == 0;
209 ok 18, $h->{'x'} == 1;
210 ok 19, $FAULT::fault == 1;
214 ok 21, $ht->{'x'} == 1;
215 ok 22, $FAULT::fault == 2;