Commit | Line | Data |
7a6a85bf |
1 | #!./perl |
7a6a85bf |
2 | # |
3 | # Copyright (c) 1995-2000, Raphael Manfredi |
4 | # |
9e21b3d0 |
5 | # You may redistribute only under the same terms as Perl 5, as specified |
6 | # in the README file that comes with the distribution. |
7a6a85bf |
7 | # |
7a6a85bf |
8 | |
9 | sub BEGIN { |
48c887dd |
10 | unshift @INC, 't'; |
9f233367 |
11 | require Config; import Config; |
0c384302 |
12 | if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) { |
9f233367 |
13 | print "1..0 # Skip: Storable was not built\n"; |
14 | exit 0; |
15 | } |
372cb964 |
16 | require 'st-dump.pl'; |
7a6a85bf |
17 | } |
18 | |
19 | sub ok; |
20 | |
21 | use Storable qw(freeze thaw); |
22 | |
06389709 |
23 | print "1..23\n"; |
7a6a85bf |
24 | |
25 | ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0); |
26 | |
27 | package TIED_HASH; |
28 | |
29 | sub TIEHASH { |
30 | my $self = bless {}, shift; |
31 | return $self; |
32 | } |
33 | |
34 | sub FETCH { |
35 | my $self = shift; |
36 | my ($key) = @_; |
37 | $main::hash_fetch++; |
38 | return $self->{$key}; |
39 | } |
40 | |
41 | sub STORE { |
42 | my $self = shift; |
43 | my ($key, $value) = @_; |
44 | $self->{$key} = $value; |
45 | } |
46 | |
47 | sub FIRSTKEY { |
48 | my $self = shift; |
49 | scalar keys %{$self}; |
50 | return each %{$self}; |
51 | } |
52 | |
53 | sub NEXTKEY { |
54 | my $self = shift; |
55 | return each %{$self}; |
56 | } |
57 | |
58 | package TIED_ARRAY; |
59 | |
60 | sub TIEARRAY { |
61 | my $self = bless [], shift; |
62 | return $self; |
63 | } |
64 | |
65 | sub FETCH { |
66 | my $self = shift; |
67 | my ($idx) = @_; |
68 | $main::array_fetch++; |
69 | return $self->[$idx]; |
70 | } |
71 | |
72 | sub STORE { |
73 | my $self = shift; |
74 | my ($idx, $value) = @_; |
75 | $self->[$idx] = $value; |
76 | } |
77 | |
78 | sub FETCHSIZE { |
79 | my $self = shift; |
80 | return @{$self}; |
81 | } |
82 | |
83 | package TIED_SCALAR; |
84 | |
85 | sub TIESCALAR { |
86 | my $scalar; |
87 | my $self = bless \$scalar, shift; |
88 | return $self; |
89 | } |
90 | |
91 | sub FETCH { |
92 | my $self = shift; |
93 | $main::scalar_fetch++; |
94 | return $$self; |
95 | } |
96 | |
97 | sub STORE { |
98 | my $self = shift; |
99 | my ($value) = @_; |
100 | $$self = $value; |
101 | } |
102 | |
103 | package FAULT; |
104 | |
105 | $fault = 0; |
106 | |
107 | sub TIESCALAR { |
108 | my $pkg = shift; |
109 | return bless [@_], $pkg; |
110 | } |
111 | |
112 | sub FETCH { |
113 | my $self = shift; |
114 | my ($href, $key) = @$self; |
115 | $fault++; |
116 | untie $href->{$key}; |
117 | return $href->{$key} = 1; |
118 | } |
119 | |
120 | package main; |
121 | |
122 | $a = 'toto'; |
123 | $b = \$a; |
124 | |
125 | $c = tie %hash, TIED_HASH; |
126 | $d = tie @array, TIED_ARRAY; |
127 | tie $scalar, TIED_SCALAR; |
128 | |
129 | #$scalar = 'foo'; |
130 | #$hash{'attribute'} = \$d; |
131 | #$array[0] = $c; |
132 | #$array[1] = \$scalar; |
133 | |
134 | ### If I say |
135 | ### $hash{'attribute'} = $d; |
136 | ### below, then dump() incorectly dumps the hash value as a string the second |
137 | ### time it is reached. I have not investigated enough to tell whether it's |
138 | ### a bug in my dump() routine or in the Perl tieing mechanism. |
139 | $scalar = 'foo'; |
140 | $hash{'attribute'} = 'plain value'; |
141 | $array[0] = \$scalar; |
142 | $array[1] = $c; |
143 | $array[2] = \@array; |
144 | |
145 | @tied = (\$scalar, \@array, \%hash); |
146 | %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar); |
147 | @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d, |
148 | $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied); |
149 | |
150 | ok 1, defined($f = freeze(\@a)); |
151 | |
152 | $dumped = &dump(\@a); |
153 | ok 2, 1; |
154 | |
155 | $root = thaw($f); |
156 | ok 3, defined $root; |
157 | |
158 | $got = &dump($root); |
159 | ok 4, 1; |
160 | |
161 | ### Used to see the manifestation of the bug documented above. |
162 | ### print "original: $dumped"; |
163 | ### print "--------\n"; |
164 | ### print "got: $got"; |
165 | ### print "--------\n"; |
166 | |
167 | ok 5, $got eq $dumped; |
168 | |
169 | $g = freeze($root); |
170 | ok 6, length($f) == length($g); |
171 | |
172 | # Ensure the tied items in the retrieved image work |
173 | @old = ($scalar_fetch, $array_fetch, $hash_fetch); |
174 | @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]}; |
175 | @type = qw(SCALAR ARRAY HASH); |
176 | |
177 | ok 7, tied $$tscalar; |
178 | ok 8, tied @{$tarray}; |
179 | ok 9, tied %{$thash}; |
180 | |
181 | @new = ($$tscalar, $tarray->[0], $thash->{'attribute'}); |
182 | @new = ($scalar_fetch, $array_fetch, $hash_fetch); |
183 | |
184 | # Tests 10..15 |
185 | for ($i = 0; $i < @new; $i++) { |
186 | print "not " unless $new[$i] == $old[$i] + 1; |
187 | printf "ok %d\n", 10 + 2*$i; # Tests 10,12,14 |
188 | print "not " unless ref $tied[$i] eq $type[$i]; |
189 | printf "ok %d\n", 11 + 2*$i; # Tests 11,13,15 |
190 | } |
191 | |
192 | # Check undef ties |
193 | my $h = {}; |
194 | tie $h->{'x'}, 'FAULT', $h, 'x'; |
195 | my $hf = freeze($h); |
196 | ok 16, defined $hf; |
197 | ok 17, $FAULT::fault == 0; |
198 | ok 18, $h->{'x'} == 1; |
199 | ok 19, $FAULT::fault == 1; |
200 | |
201 | my $ht = thaw($hf); |
202 | ok 20, defined $ht; |
203 | ok 21, $ht->{'x'} == 1; |
204 | ok 22, $FAULT::fault == 2; |
f4193312 |
205 | |
206 | { |
207 | package P; |
208 | use Storable qw(freeze thaw); |
d5f92636 |
209 | use vars qw($a $b); |
f4193312 |
210 | $b = "not ok "; |
211 | sub TIESCALAR { bless \$a } sub FETCH { "ok " } |
212 | tie $a, P; my $r = thaw freeze \$a; $b = $$r; |
213 | print $b , 23, "\n"; |
214 | } |
215 | |