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