3 BEGIN { unshift @INC, './lib', '../lib';
4 require Config; import Config;
9 # Anonymous subroutines:
10 '+' => sub {new Oscalar $ {$_[0]}+$_[1]},
11 '-' => sub {new Oscalar
12 $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]},
13 '<=>' => sub {new Oscalar
14 $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]},
15 'cmp' => sub {new Oscalar
16 $_[2]? ($_[1] cmp ${$_[0]}) : (${$_[0]} cmp $_[1])},
17 '*' => sub {new Oscalar ${$_[0]}*$_[1]},
18 '/' => sub {new Oscalar
19 $_[2]? $_[1]/${$_[0]} :
21 '%' => sub {new Oscalar
22 $_[2]? $_[1]%${$_[0]} : ${$_[0]}%$_[1]},
23 '**' => sub {new Oscalar
24 $_[2]? $_[1]**${$_[0]} : ${$_[0]}-$_[1]},
28 0+ numify) # Order of arguments unsignificant
36 sub stringify { "${$_[0]}" }
37 sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead
38 # comparing to direct compilation based on
45 print "1..",&last,"\n";
48 $test++; if (shift) {print "ok $test\n";1} else {print "not ok $test\n";0}
51 $a = new Oscalar "087";
54 # All test numbers in comments are off by 1.
55 # So much for hard-wiring them in :-)
57 test ($b eq "087"); # 3
58 test (ref $a eq "Oscalar"); # 4
60 test ($a eq "087"); # 6
64 test (ref $c eq "Oscalar"); # 7
65 test (!($c eq $a)); # 8
66 test ($c eq "94"); # 9
70 test (ref $a eq "Oscalar"); # 10
74 test (ref $b eq "Oscalar"); # 11
75 test ( $a eq "087"); # 12
76 test ( $b eq "88"); # 13
77 test (ref $a eq "Oscalar"); # 14
82 test (ref $c eq "Oscalar"); # 15
83 test ( $a eq "087"); # 16
84 test ( $c eq "1"); # 17
85 test (ref $a eq "Oscalar"); # 18
90 test (ref $b eq "Oscalar"); # 19
91 test ( $a eq "087"); # 20
92 test ( $b eq "88"); # 21
93 test (ref $a eq "Oscalar"); # 22
95 eval q[ package Oscalar; use overload ('++' => sub { $ {$_[0]}++;$_[0] } ) ];
99 test (ref $a eq "Oscalar"); # 23
103 test (ref $b eq "Oscalar"); # 24
104 test ( $a eq "087"); # 25
105 test ( $b eq "88"); # 26
106 test (ref $a eq "Oscalar"); # 27
109 $dummy=bless \$dummy; # Now cache of method should be reloaded
115 test (ref $b eq "Oscalar"); # 28
116 test ( $a eq "087"); # 29
117 test ( $b eq "88"); # 30
118 test (ref $a eq "Oscalar"); # 31
121 eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ];
125 test (ref $a eq "Oscalar"); # 32
129 test (ref $b eq "Oscalar"); # 33
130 test ( $a eq "087"); # 34
131 test ( $b eq "88"); # 35
132 test (ref $a eq "Oscalar"); # 36
135 $dummy=bless \$dummy; # Now cache of method should be reloaded
140 test (ref $b eq "Oscalar"); # 37
141 test ( $a eq "087"); # 38
142 test ( $b eq "90"); # 39
143 test (ref $a eq "Oscalar"); # 40
148 test (ref $b eq "Oscalar"); # 41
149 test ( $a eq "087"); # 42
150 test ( $b eq "89"); # 43
151 test (ref $a eq "Oscalar"); # 44
156 eval q[ package Oscalar; use overload ('=' => sub {$main::copies++;
158 local $new=$ {$_[0]};
163 test (ref $b eq "Oscalar"); # 46
164 test ( $a eq "087"); # 47
165 test ( $b eq "087"); # 48
166 test (ref $a eq "Oscalar"); # 49
170 test (ref $b eq "Oscalar"); # 50
171 test ( $a eq "087"); # 51
172 test ( $b eq "89"); # 52
173 test (ref $a eq "Oscalar"); # 53
174 test ($copies == 0); # 54
178 test (ref $b eq "Oscalar"); # 55
179 test ( $a eq "087"); # 56
180 test ( $b eq "90"); # 57
181 test (ref $a eq "Oscalar"); # 58
182 test ($copies == 0); # 59
187 test (ref $b eq "Oscalar"); # 60
188 test ( $a eq "087"); # 61
189 test ( $b eq "88"); # 62
190 test (ref $a eq "Oscalar"); # 63
191 test ($copies == 0); # 64
196 test (ref $b eq "Oscalar") || print ref $b,"=ref(b)\n"; # 65
197 test ( $a eq "087"); # 66
198 test ( $b eq "89"); # 67
199 test (ref $a eq "Oscalar"); # 68
200 test ($copies == 1); # 69
202 eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1];
204 $c=new Oscalar; # Cause rehash
209 test (ref $b eq "Oscalar"); # 70
210 test ( $a eq "087"); # 71
211 test ( $b eq "90"); # 72
212 test (ref $a eq "Oscalar"); # 73
213 test ($copies == 2); # 74
217 test (ref $b eq "Oscalar"); # 75
218 test ( $b eq "360"); # 76
219 test ($copies == 2); # 77
222 test (ref $b eq "Oscalar"); # 78
223 test ( $b eq "-360"); # 79
224 test ($copies == 2); # 80
228 test (ref $b eq "Oscalar"); # 81
229 test ( $b eq "360"); # 82
230 test ($copies == 2); # 83
234 test (ref $b eq "Oscalar"); # 84
235 test ( $b eq "360"); # 85
236 test ($copies == 2); # 86
238 eval q[package Oscalar;
239 use overload ('x' => sub {new Oscalar ( $_[2] ? "_.$_[1]._" x $ {$_[0]}
240 : "_.${$_[0]}._" x $_[1])}) ];
244 test ($a eq "_.yy.__.yy.__.yy._"); # 87
246 eval q[package Oscalar;
247 use overload ('.' => sub {new Oscalar ( $_[2] ?
248 "_.$_[1].__.$ {$_[0]}._"
249 : "_.$ {$_[0]}.__.$_[1]._")}) ];
253 test ("b${a}c" eq "_._.b.__.xx._.__.c._"); # 88
255 # Here we test blessing to a package updates hash
257 eval "package Oscalar; no overload '.'";
259 test ("b${a}" eq "_.b.__.xx._"); # 89
262 test ("b${a}c" eq "bxxc"); # 90
264 test ("b${a}c" eq "bxxc"); # 91
266 # Last test is number 90.