3 # $Id: tied_hook.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.
10 # $Log: tied_hook.t,v $
11 # Revision 1.0 2000/09/01 19:40:42 ram
12 # Baseline for first official release.
19 require Config; import Config;
20 if ($Config{'extensions'} !~ /\bStorable\b/) {
21 print "1..0 # Skip: Storable was not built\n";
24 require 'lib/st-dump.pl';
29 use Storable qw(freeze thaw);
33 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
38 my $self = bless {}, shift;
51 my ($key, $value) = @_;
52 $self->{$key} = $value;
69 return join(":", keys %$self) . ";" . join(":", values %$self);
73 my ($self, $cloning, $frozen) = @_;
74 my ($keys, $values) = split(/;/, $frozen);
75 my @keys = split(/:/, $keys);
76 my @values = split(/:/, $values);
77 for (my $i = 0; $i < @keys; $i++) {
78 $self->{$keys[$i]} = $values[$i];
86 my $self = bless [], shift;
99 my ($idx, $value) = @_;
100 $self->[$idx] = $value;
108 sub STORABLE_freeze {
110 $main::array_hook1++;
111 return join(":", @$self);
115 my ($self, $cloning, $frozen) = @_;
116 @$self = split(/:/, $frozen);
117 $main::array_hook2++;
124 my $self = bless \$scalar, shift;
130 $main::scalar_fetch++;
140 sub STORABLE_freeze {
142 $main::scalar_hook1++;
147 my ($self, $cloning, $frozen) = @_;
149 $main::scalar_hook2++;
157 $c = tie %hash, TIED_HASH;
158 $d = tie @array, TIED_ARRAY;
159 tie $scalar, TIED_SCALAR;
162 $hash{'attribute'} = 'plain value';
163 $array[0] = \$scalar;
166 $array[3] = "plaine scalaire";
168 @tied = (\$scalar, \@array, \%hash);
169 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
170 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
171 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
173 ok 1, defined($f = freeze(\@a));
175 $dumped = &dump(\@a);
184 ok 5, $got ne $dumped; # our hooks did not handle refs in array
187 ok 6, length($f) == length($g);
189 # Ensure the tied items in the retrieved image work
190 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
191 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
192 @type = qw(SCALAR ARRAY HASH);
194 ok 7, tied $$tscalar;
195 ok 8, tied @{$tarray};
196 ok 9, tied %{$thash};
198 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
199 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
202 for ($i = 0; $i < @new; $i++) {
203 ok 10 + 2*$i, $new[$i] == $old[$i] + 1; # Tests 10,12,14
204 ok 11 + 2*$i, ref $tied[$i] eq $type[$i]; # Tests 11,13,15
207 ok 16, $$tscalar eq 'foo';
208 ok 17, $tarray->[3] eq 'plaine scalaire';
209 ok 18, $thash->{'attribute'} eq 'plain value';
211 # Ensure hooks were called
212 ok 19, ($scalar_hook1 && $scalar_hook2);
213 ok 20, ($array_hook1 && $array_hook2);
214 ok 21, ($hash_hook1 && $hash_hook2);