12 # Anonymous subroutines:
13 '+' => sub {new Oscalar $ {$_[0]}+$_[1]},
14 '-' => sub {new Oscalar
15 $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]},
16 '<=>' => sub {new Oscalar
17 $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]},
18 'cmp' => sub {new Oscalar
19 $_[2]? ($_[1] cmp ${$_[0]}) : (${$_[0]} cmp $_[1])},
20 '*' => sub {new Oscalar ${$_[0]}*$_[1]},
21 '/' => sub {new Oscalar
22 $_[2]? $_[1]/${$_[0]} :
24 '%' => sub {new Oscalar
25 $_[2]? $_[1]%${$_[0]} : ${$_[0]}%$_[1]},
26 '**' => sub {new Oscalar
27 $_[2]? $_[1]**${$_[0]} : ${$_[0]}-$_[1]},
31 0+ numify) # Order of arguments unsignificant
39 sub stringify { "${$_[0]}" }
40 sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead
41 # comparing to direct compilation based on
48 print "1..",&last,"\n";
51 $test++; if (shift) {print "ok $test\n";1} else {print "not ok $test\n";0}
54 $a = new Oscalar "087";
57 # All test numbers in comments are off by 1.
58 # So much for hard-wiring them in :-)
60 test ($b eq "087"); # 3
61 test (ref $a eq "Oscalar"); # 4
63 test ($a eq "087"); # 6
67 test (ref $c eq "Oscalar"); # 7
68 test (!($c eq $a)); # 8
69 test ($c eq "94"); # 9
73 test (ref $a eq "Oscalar"); # 10
77 test (ref $b eq "Oscalar"); # 11
78 test ( $a eq "087"); # 12
79 test ( $b eq "88"); # 13
80 test (ref $a eq "Oscalar"); # 14
85 test (ref $c eq "Oscalar"); # 15
86 test ( $a eq "087"); # 16
87 test ( $c eq "1"); # 17
88 test (ref $a eq "Oscalar"); # 18
93 test (ref $b eq "Oscalar"); # 19
94 test ( $a eq "087"); # 20
95 test ( $b eq "88"); # 21
96 test (ref $a eq "Oscalar"); # 22
98 eval q[ package Oscalar; use overload ('++' => sub { $ {$_[0]}++;$_[0] } ) ];
102 test (ref $a eq "Oscalar"); # 23
106 test (ref $b eq "Oscalar"); # 24
107 test ( $a eq "087"); # 25
108 test ( $b eq "88"); # 26
109 test (ref $a eq "Oscalar"); # 27
112 $dummy=bless \$dummy; # Now cache of method should be reloaded
118 test (ref $b eq "Oscalar"); # 28
119 test ( $a eq "087"); # 29
120 test ( $b eq "88"); # 30
121 test (ref $a eq "Oscalar"); # 31
124 eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ];
128 test (ref $a eq "Oscalar"); # 32
132 test (ref $b eq "Oscalar"); # 33
133 test ( $a eq "087"); # 34
134 test ( $b eq "88"); # 35
135 test (ref $a eq "Oscalar"); # 36
138 $dummy=bless \$dummy; # Now cache of method should be reloaded
143 test (ref $b eq "Oscalar"); # 37
144 test ( $a eq "087"); # 38
145 test ( $b eq "90"); # 39
146 test (ref $a eq "Oscalar"); # 40
151 test (ref $b eq "Oscalar"); # 41
152 test ( $a eq "087"); # 42
153 test ( $b eq "89"); # 43
154 test (ref $a eq "Oscalar"); # 44
159 eval q[ package Oscalar; use overload ('=' => sub {$main::copies++;
161 local $new=$ {$_[0]};
166 test (ref $b eq "Oscalar"); # 46
167 test ( $a eq "087"); # 47
168 test ( $b eq "087"); # 48
169 test (ref $a eq "Oscalar"); # 49
173 test (ref $b eq "Oscalar"); # 50
174 test ( $a eq "087"); # 51
175 test ( $b eq "89"); # 52
176 test (ref $a eq "Oscalar"); # 53
177 test ($copies == 0); # 54
181 test (ref $b eq "Oscalar"); # 55
182 test ( $a eq "087"); # 56
183 test ( $b eq "90"); # 57
184 test (ref $a eq "Oscalar"); # 58
185 test ($copies == 0); # 59
190 test (ref $b eq "Oscalar"); # 60
191 test ( $a eq "087"); # 61
192 test ( $b eq "88"); # 62
193 test (ref $a eq "Oscalar"); # 63
194 test ($copies == 0); # 64
199 test (ref $b eq "Oscalar") || print ref $b,"=ref(b)\n"; # 65
200 test ( $a eq "087"); # 66
201 test ( $b eq "89"); # 67
202 test (ref $a eq "Oscalar"); # 68
203 test ($copies == 1); # 69
205 eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1];
207 $c=new Oscalar; # Cause rehash
212 test (ref $b eq "Oscalar"); # 70
213 test ( $a eq "087"); # 71
214 test ( $b eq "90"); # 72
215 test (ref $a eq "Oscalar"); # 73
216 test ($copies == 2); # 74
220 test (ref $b eq "Oscalar"); # 75
221 test ( $b eq "360"); # 76
222 test ($copies == 2); # 77
225 test (ref $b eq "Oscalar"); # 78
226 test ( $b eq "-360"); # 79
227 test ($copies == 2); # 80
231 test (ref $b eq "Oscalar"); # 81
232 test ( $b eq "360"); # 82
233 test ($copies == 2); # 83
237 test (ref $b eq "Oscalar"); # 84
238 test ( $b eq "360"); # 85
239 test ($copies == 2); # 86
241 eval q[package Oscalar;
242 use overload ('x' => sub {new Oscalar ( $_[2] ? "_.$_[1]._" x $ {$_[0]}
243 : "_.${$_[0]}._" x $_[1])}) ];
247 test ($a eq "_.yy.__.yy.__.yy._"); # 87
249 eval q[package Oscalar;
250 use overload ('.' => sub {new Oscalar ( $_[2] ?
251 "_.$_[1].__.$ {$_[0]}._"
252 : "_.$ {$_[0]}.__.$_[1]._")}) ];
256 test ("b${a}c" eq "_._.b.__.xx._.__.c._"); # 88
258 # Here we test blessing to a package updates hash
260 eval "package Oscalar; no overload '.'";
262 test ("b${a}" eq "_.b.__.xx._"); # 89
265 test ("b${a}c" eq "bxxc"); # 90
267 test ("b${a}c" eq "bxxc"); # 91
269 # Last test is number 90.