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.
24 require Config; import Config;
25 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
26 print "1..0 # Skip: Storable was not built\n";
29 require 'lib/st-dump.pl';
34 use Storable qw(freeze thaw);
38 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
43 my $self = bless {}, shift;
56 my ($key, $value) = @_;
57 $self->{$key} = $value;
74 return join(":", keys %$self) . ";" . join(":", values %$self);
78 my ($self, $cloning, $frozen) = @_;
79 my ($keys, $values) = split(/;/, $frozen);
80 my @keys = split(/:/, $keys);
81 my @values = split(/:/, $values);
82 for (my $i = 0; $i < @keys; $i++) {
83 $self->{$keys[$i]} = $values[$i];
91 my $self = bless [], shift;
104 my ($idx, $value) = @_;
105 $self->[$idx] = $value;
113 sub STORABLE_freeze {
115 $main::array_hook1++;
116 return join(":", @$self);
120 my ($self, $cloning, $frozen) = @_;
121 @$self = split(/:/, $frozen);
122 $main::array_hook2++;
129 my $self = bless \$scalar, shift;
135 $main::scalar_fetch++;
145 sub STORABLE_freeze {
147 $main::scalar_hook1++;
152 my ($self, $cloning, $frozen) = @_;
154 $main::scalar_hook2++;
162 $c = tie %hash, TIED_HASH;
163 $d = tie @array, TIED_ARRAY;
164 tie $scalar, TIED_SCALAR;
167 $hash{'attribute'} = 'plain value';
168 $array[0] = \$scalar;
171 $array[3] = "plaine scalaire";
173 @tied = (\$scalar, \@array, \%hash);
174 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
175 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
176 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
178 ok 1, defined($f = freeze(\@a));
180 $dumped = &dump(\@a);
189 ok 5, $got ne $dumped; # our hooks did not handle refs in array
192 ok 6, length($f) == length($g);
194 # Ensure the tied items in the retrieved image work
195 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
196 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
197 @type = qw(SCALAR ARRAY HASH);
199 ok 7, tied $$tscalar;
200 ok 8, tied @{$tarray};
201 ok 9, tied %{$thash};
203 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
204 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
207 for ($i = 0; $i < @new; $i++) {
208 ok 10 + 2*$i, $new[$i] == $old[$i] + 1; # Tests 10,12,14
209 ok 11 + 2*$i, ref $tied[$i] eq $type[$i]; # Tests 11,13,15
212 ok 16, $$tscalar eq 'foo';
213 ok 17, $tarray->[3] eq 'plaine scalaire';
214 ok 18, $thash->{'attribute'} eq 'plain value';
216 # Ensure hooks were called
217 ok 19, ($scalar_hook1 && $scalar_hook2);
218 ok 20, ($array_hook1 && $array_hook2);
219 ok 21, ($hash_hook1 && $hash_hook2);
222 # And now for the "blessed ref to tied hash" with "store hook" test...
225 my $bc = bless \%hash, 'FOO'; # FOO does not exist -> no hook
226 my $bx = thaw freeze $bc;
228 ok 22, ref $bx eq 'FOO';
229 my $old_hash_fetch = $hash_fetch;
230 my $v = $bx->{attribute};
231 ok 23, $hash_fetch == $old_hash_fetch + 1; # Still tied
233 package TIED_HASH_REF;
236 sub STORABLE_freeze {
237 my ($self, $cloning) = @_;
243 my ($self, $cloning, $data) = @_;
249 $bc = bless \%hash, 'TIED_HASH_REF';
250 $bx = thaw freeze $bc;
252 ok 24, ref $bx eq 'TIED_HASH_REF';
253 $old_hash_fetch = $hash_fetch;
254 $v = $bx->{attribute};
255 ok 25, $hash_fetch == $old_hash_fetch + 1; # Still tied