3 # $Id: tied_hook.t,v 1.0.1.1 2001/02/17 12:29:01 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 return join(":", keys %$self) . ";" . join(":", values %$self);
72 my ($self, $cloning, $frozen) = @_;
73 my ($keys, $values) = split(/;/, $frozen);
74 my @keys = split(/:/, $keys);
75 my @values = split(/:/, $values);
76 for (my $i = 0; $i < @keys; $i++) {
77 $self->{$keys[$i]} = $values[$i];
85 my $self = bless [], shift;
98 my ($idx, $value) = @_;
99 $self->[$idx] = $value;
107 sub STORABLE_freeze {
109 $main::array_hook1++;
110 return join(":", @$self);
114 my ($self, $cloning, $frozen) = @_;
115 @$self = split(/:/, $frozen);
116 $main::array_hook2++;
123 my $self = bless \$scalar, shift;
129 $main::scalar_fetch++;
139 sub STORABLE_freeze {
141 $main::scalar_hook1++;
146 my ($self, $cloning, $frozen) = @_;
148 $main::scalar_hook2++;
156 $c = tie %hash, TIED_HASH;
157 $d = tie @array, TIED_ARRAY;
158 tie $scalar, TIED_SCALAR;
161 $hash{'attribute'} = 'plain value';
162 $array[0] = \$scalar;
165 $array[3] = "plaine scalaire";
167 @tied = (\$scalar, \@array, \%hash);
168 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
169 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
170 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
172 ok 1, defined($f = freeze(\@a));
174 $dumped = &dump(\@a);
183 ok 5, $got ne $dumped; # our hooks did not handle refs in array
186 ok 6, length($f) == length($g);
188 # Ensure the tied items in the retrieved image work
189 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
190 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
191 @type = qw(SCALAR ARRAY HASH);
193 ok 7, tied $$tscalar;
194 ok 8, tied @{$tarray};
195 ok 9, tied %{$thash};
197 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
198 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
201 for ($i = 0; $i < @new; $i++) {
202 ok 10 + 2*$i, $new[$i] == $old[$i] + 1; # Tests 10,12,14
203 ok 11 + 2*$i, ref $tied[$i] eq $type[$i]; # Tests 11,13,15
206 ok 16, $$tscalar eq 'foo';
207 ok 17, $tarray->[3] eq 'plaine scalaire';
208 ok 18, $thash->{'attribute'} eq 'plain value';
210 # Ensure hooks were called
211 ok 19, ($scalar_hook1 && $scalar_hook2);
212 ok 20, ($array_hook1 && $array_hook2);
213 ok 21, ($hash_hook1 && $hash_hook2);
216 # And now for the "blessed ref to tied hash" with "store hook" test...
219 my $bc = bless \%hash, 'FOO'; # FOO does not exist -> no hook
220 my $bx = thaw freeze $bc;
222 ok 22, ref $bx eq 'FOO';
223 my $old_hash_fetch = $hash_fetch;
224 my $v = $bx->{attribute};
225 ok 23, $hash_fetch == $old_hash_fetch + 1; # Still tied
227 package TIED_HASH_REF;
230 sub STORABLE_freeze {
231 my ($self, $cloning) = @_;
237 my ($self, $cloning, $data) = @_;
243 $bc = bless \%hash, 'TIED_HASH_REF';
244 $bx = thaw freeze $bc;
246 ok 24, ref $bx eq 'TIED_HASH_REF';
247 $old_hash_fetch = $hash_fetch;
248 $v = $bx->{attribute};
249 ok 25, $hash_fetch == $old_hash_fetch + 1; # Still tied