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.
19 require Config; import Config;
20 if ($Config{'extensions'} !~ /\bStorable\b/) {
21 print "1..0 # Skip: Storable was not built\n";
24 require 'lib/st-dump.pl';
29 use Storable qw(freeze thaw);
33 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
38 my $self = bless {}, shift;
51 my ($key, $value) = @_;
52 $self->{$key} = $value;
69 my $self = bless [], shift;
82 my ($idx, $value) = @_;
83 $self->[$idx] = $value;
95 my $self = bless \$scalar, shift;
101 $main::scalar_fetch++;
117 return bless [@_], $pkg;
122 my ($href, $key) = @$self;
125 return $href->{$key} = 1;
133 $c = tie %hash, TIED_HASH;
134 $d = tie @array, TIED_ARRAY;
135 tie $scalar, TIED_SCALAR;
138 #$hash{'attribute'} = \$d;
140 #$array[1] = \$scalar;
143 ### $hash{'attribute'} = $d;
144 ### below, then dump() incorectly dumps the hash value as a string the second
145 ### time it is reached. I have not investigated enough to tell whether it's
146 ### a bug in my dump() routine or in the Perl tieing mechanism.
148 $hash{'attribute'} = 'plain value';
149 $array[0] = \$scalar;
153 @tied = (\$scalar, \@array, \%hash);
154 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
155 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
156 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
158 ok 1, defined($f = freeze(\@a));
160 $dumped = &dump(\@a);
169 ### Used to see the manifestation of the bug documented above.
170 ### print "original: $dumped";
171 ### print "--------\n";
172 ### print "got: $got";
173 ### print "--------\n";
175 ok 5, $got eq $dumped;
178 ok 6, length($f) == length($g);
180 # Ensure the tied items in the retrieved image work
181 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
182 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
183 @type = qw(SCALAR ARRAY HASH);
185 ok 7, tied $$tscalar;
186 ok 8, tied @{$tarray};
187 ok 9, tied %{$thash};
189 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
190 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
193 for ($i = 0; $i < @new; $i++) {
194 print "not " unless $new[$i] == $old[$i] + 1;
195 printf "ok %d\n", 10 + 2*$i; # Tests 10,12,14
196 print "not " unless ref $tied[$i] eq $type[$i];
197 printf "ok %d\n", 11 + 2*$i; # Tests 11,13,15
202 tie $h->{'x'}, 'FAULT', $h, 'x';
205 ok 17, $FAULT::fault == 0;
206 ok 18, $h->{'x'} == 1;
207 ok 19, $FAULT::fault == 1;
211 ok 21, $ht->{'x'} == 1;
212 ok 22, $FAULT::fault == 2;