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.
10 # $Log: tied_hook.t,v $
11 # Revision 1.0.1.1 2001/02/17 12:29:01 ram
12 # patch8: added test for blessed ref to tied hash
14 # Revision 1.0 2000/09/01 19:40:42 ram
15 # Baseline for first official release.
21 @INC = ('.', '../lib', '../t/lib');
25 require Config; import Config;
26 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
27 print "1..0 # Skip: Storable was not built\n";
35 use Storable qw(freeze thaw);
39 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
44 my $self = bless {}, shift;
57 my ($key, $value) = @_;
58 $self->{$key} = $value;
75 return join(":", keys %$self) . ";" . join(":", values %$self);
79 my ($self, $cloning, $frozen) = @_;
80 my ($keys, $values) = split(/;/, $frozen);
81 my @keys = split(/:/, $keys);
82 my @values = split(/:/, $values);
83 for (my $i = 0; $i < @keys; $i++) {
84 $self->{$keys[$i]} = $values[$i];
92 my $self = bless [], shift;
100 return $self->[$idx];
105 my ($idx, $value) = @_;
106 $self->[$idx] = $value;
114 sub STORABLE_freeze {
116 $main::array_hook1++;
117 return join(":", @$self);
121 my ($self, $cloning, $frozen) = @_;
122 @$self = split(/:/, $frozen);
123 $main::array_hook2++;
130 my $self = bless \$scalar, shift;
136 $main::scalar_fetch++;
146 sub STORABLE_freeze {
148 $main::scalar_hook1++;
153 my ($self, $cloning, $frozen) = @_;
155 $main::scalar_hook2++;
163 $c = tie %hash, TIED_HASH;
164 $d = tie @array, TIED_ARRAY;
165 tie $scalar, TIED_SCALAR;
168 $hash{'attribute'} = 'plain value';
169 $array[0] = \$scalar;
172 $array[3] = "plaine scalaire";
174 @tied = (\$scalar, \@array, \%hash);
175 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
176 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
177 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
179 ok 1, defined($f = freeze(\@a));
181 $dumped = &dump(\@a);
190 ok 5, $got ne $dumped; # our hooks did not handle refs in array
193 ok 6, length($f) == length($g);
195 # Ensure the tied items in the retrieved image work
196 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
197 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
198 @type = qw(SCALAR ARRAY HASH);
200 ok 7, tied $$tscalar;
201 ok 8, tied @{$tarray};
202 ok 9, tied %{$thash};
204 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
205 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
208 for ($i = 0; $i < @new; $i++) {
209 ok 10 + 2*$i, $new[$i] == $old[$i] + 1; # Tests 10,12,14
210 ok 11 + 2*$i, ref $tied[$i] eq $type[$i]; # Tests 11,13,15
213 ok 16, $$tscalar eq 'foo';
214 ok 17, $tarray->[3] eq 'plaine scalaire';
215 ok 18, $thash->{'attribute'} eq 'plain value';
217 # Ensure hooks were called
218 ok 19, ($scalar_hook1 && $scalar_hook2);
219 ok 20, ($array_hook1 && $array_hook2);
220 ok 21, ($hash_hook1 && $hash_hook2);
223 # And now for the "blessed ref to tied hash" with "store hook" test...
226 my $bc = bless \%hash, 'FOO'; # FOO does not exist -> no hook
227 my $bx = thaw freeze $bc;
229 ok 22, ref $bx eq 'FOO';
230 my $old_hash_fetch = $hash_fetch;
231 my $v = $bx->{attribute};
232 ok 23, $hash_fetch == $old_hash_fetch + 1; # Still tied
234 package TIED_HASH_REF;
237 sub STORABLE_freeze {
238 my ($self, $cloning) = @_;
244 my ($self, $cloning, $data) = @_;
250 $bc = bless \%hash, 'TIED_HASH_REF';
251 $bx = thaw freeze $bc;
253 ok 24, ref $bx eq 'TIED_HASH_REF';
254 $old_hash_fetch = $hash_fetch;
255 $v = $bx->{attribute};
256 ok 25, $hash_fetch == $old_hash_fetch + 1; # Still tied