3 # $Id: tied_hook.t,v 0.7 2000/08/03 22:04:45 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.
10 # $Log: tied_hook.t,v $
11 # Revision 0.7 2000/08/03 22:04:45 ram
12 # Baseline for second beta release.
17 unshift @INC, '../lib';
18 require 'lib/st-dump.pl';
23 use Storable qw(freeze thaw);
27 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
32 my $self = bless {}, shift;
45 my ($key, $value) = @_;
46 $self->{$key} = $value;
63 return join(":", keys %$self) . ";" . join(":", values %$self);
67 my ($self, $cloning, $frozen) = @_;
68 my ($keys, $values) = split(/;/, $frozen);
69 my @keys = split(/:/, $keys);
70 my @values = split(/:/, $values);
71 for (my $i = 0; $i < @keys; $i++) {
72 $self->{$keys[$i]} = $values[$i];
80 my $self = bless [], shift;
93 my ($idx, $value) = @_;
94 $self->[$idx] = $value;
102 sub STORABLE_freeze {
104 $main::array_hook1++;
105 return join(":", @$self);
109 my ($self, $cloning, $frozen) = @_;
110 @$self = split(/:/, $frozen);
111 $main::array_hook2++;
118 my $self = bless \$scalar, shift;
124 $main::scalar_fetch++;
134 sub STORABLE_freeze {
136 $main::scalar_hook1++;
141 my ($self, $cloning, $frozen) = @_;
143 $main::scalar_hook2++;
151 $c = tie %hash, TIED_HASH;
152 $d = tie @array, TIED_ARRAY;
153 tie $scalar, TIED_SCALAR;
156 $hash{'attribute'} = 'plain value';
157 $array[0] = \$scalar;
160 $array[3] = "plaine scalaire";
162 @tied = (\$scalar, \@array, \%hash);
163 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
164 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
165 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
167 ok 1, defined($f = freeze(\@a));
169 $dumped = &dump(\@a);
178 ok 5, $got ne $dumped; # our hooks did not handle refs in array
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 ok 10 + 2*$i, $new[$i] == $old[$i] + 1; # Tests 10,12,14
198 ok 11 + 2*$i, ref $tied[$i] eq $type[$i]; # Tests 11,13,15
201 ok 16, $$tscalar eq 'foo';
202 ok 17, $tarray->[3] eq 'plaine scalaire';
203 ok 18, $thash->{'attribute'} eq 'plain value';
205 # Ensure hooks were called
206 ok 19, ($scalar_hook1 && $scalar_hook2);
207 ok 20, ($array_hook1 && $array_hook2);
208 ok 21, ($hash_hook1 && $hash_hook2);