3 # $Id: tied.t,v 0.7.1.1 2000/08/13 20:10:27 ram Exp $
5 # Copyright (c) 1995-2000, Raphael Manfredi
7 # You may redistribute only under the terms of the Artistic License,
8 # as specified in the README file that comes with the distribution.
11 # Revision 0.7.1.1 2000/08/13 20:10:27 ram
12 # patch1: added test case for "undef" in hashes
14 # Revision 0.7 2000/08/03 22:04:45 ram
15 # Baseline for second beta release.
20 require Config; import Config;
21 if ($Config{'extensions'} !~ /\bStorable\b/) {
22 print "1..0 # Skip: Storable was not built\n";
25 unshift @INC, '../lib';
26 require 'lib/st-dump.pl';
31 use Storable qw(freeze thaw);
35 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
40 my $self = bless {}, shift;
53 my ($key, $value) = @_;
54 $self->{$key} = $value;
71 my $self = bless [], shift;
84 my ($idx, $value) = @_;
85 $self->[$idx] = $value;
97 my $self = bless \$scalar, shift;
103 $main::scalar_fetch++;
119 return bless [@_], $pkg;
124 my ($href, $key) = @$self;
127 return $href->{$key} = 1;
135 $c = tie %hash, TIED_HASH;
136 $d = tie @array, TIED_ARRAY;
137 tie $scalar, TIED_SCALAR;
140 #$hash{'attribute'} = \$d;
142 #$array[1] = \$scalar;
145 ### $hash{'attribute'} = $d;
146 ### below, then dump() incorectly dumps the hash value as a string the second
147 ### time it is reached. I have not investigated enough to tell whether it's
148 ### a bug in my dump() routine or in the Perl tieing mechanism.
150 $hash{'attribute'} = 'plain value';
151 $array[0] = \$scalar;
155 @tied = (\$scalar, \@array, \%hash);
156 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
157 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
158 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
160 ok 1, defined($f = freeze(\@a));
162 $dumped = &dump(\@a);
171 ### Used to see the manifestation of the bug documented above.
172 ### print "original: $dumped";
173 ### print "--------\n";
174 ### print "got: $got";
175 ### print "--------\n";
177 ok 5, $got eq $dumped;
180 ok 6, length($f) == length($g);
182 # Ensure the tied items in the retrieved image work
183 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
184 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
185 @type = qw(SCALAR ARRAY HASH);
187 ok 7, tied $$tscalar;
188 ok 8, tied @{$tarray};
189 ok 9, tied %{$thash};
191 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
192 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
195 for ($i = 0; $i < @new; $i++) {
196 print "not " unless $new[$i] == $old[$i] + 1;
197 printf "ok %d\n", 10 + 2*$i; # Tests 10,12,14
198 print "not " unless ref $tied[$i] eq $type[$i];
199 printf "ok %d\n", 11 + 2*$i; # Tests 11,13,15
204 tie $h->{'x'}, 'FAULT', $h, 'x';
207 ok 17, $FAULT::fault == 0;
208 ok 18, $h->{'x'} == 1;
209 ok 19, $FAULT::fault == 1;
213 ok 21, $ht->{'x'} == 1;
214 ok 22, $FAULT::fault == 2;