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.
14 @INC = ('.', '../lib', '../ext/Storable/t');
18 require Config; import Config;
19 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
20 print "1..0 # Skip: Storable was not built\n";
28 use Storable qw(freeze thaw);
32 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
37 my $self = bless {}, shift;
50 my ($key, $value) = @_;
51 $self->{$key} = $value;
68 my $self = bless [], shift;
81 my ($idx, $value) = @_;
82 $self->[$idx] = $value;
94 my $self = bless \$scalar, shift;
100 $main::scalar_fetch++;
116 return bless [@_], $pkg;
121 my ($href, $key) = @$self;
124 return $href->{$key} = 1;
132 $c = tie %hash, TIED_HASH;
133 $d = tie @array, TIED_ARRAY;
134 tie $scalar, TIED_SCALAR;
137 #$hash{'attribute'} = \$d;
139 #$array[1] = \$scalar;
142 ### $hash{'attribute'} = $d;
143 ### below, then dump() incorectly dumps the hash value as a string the second
144 ### time it is reached. I have not investigated enough to tell whether it's
145 ### a bug in my dump() routine or in the Perl tieing mechanism.
147 $hash{'attribute'} = 'plain value';
148 $array[0] = \$scalar;
152 @tied = (\$scalar, \@array, \%hash);
153 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
154 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
155 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
157 ok 1, defined($f = freeze(\@a));
159 $dumped = &dump(\@a);
168 ### Used to see the manifestation of the bug documented above.
169 ### print "original: $dumped";
170 ### print "--------\n";
171 ### print "got: $got";
172 ### print "--------\n";
174 ok 5, $got eq $dumped;
177 ok 6, length($f) == length($g);
179 # Ensure the tied items in the retrieved image work
180 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
181 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
182 @type = qw(SCALAR ARRAY HASH);
184 ok 7, tied $$tscalar;
185 ok 8, tied @{$tarray};
186 ok 9, tied %{$thash};
188 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
189 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
192 for ($i = 0; $i < @new; $i++) {
193 print "not " unless $new[$i] == $old[$i] + 1;
194 printf "ok %d\n", 10 + 2*$i; # Tests 10,12,14
195 print "not " unless ref $tied[$i] eq $type[$i];
196 printf "ok %d\n", 11 + 2*$i; # Tests 11,13,15
201 tie $h->{'x'}, 'FAULT', $h, 'x';
204 ok 17, $FAULT::fault == 0;
205 ok 18, $h->{'x'} == 1;
206 ok 19, $FAULT::fault == 1;
210 ok 21, $ht->{'x'} == 1;
211 ok 22, $FAULT::fault == 2;