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 :-) To fix this:
62 test ($b eq "087"); # 3
63 test (ref $a eq "Oscalar"); # 4
65 test ($a eq "087"); # 6
69 test (ref $c eq "Oscalar"); # 7
70 test (!($c eq $a)); # 8
71 test ($c eq "94"); # 9
75 test (ref $a eq "Oscalar"); # 10
79 test (ref $b eq "Oscalar"); # 11
80 test ( $a eq "087"); # 12
81 test ( $b eq "88"); # 13
82 test (ref $a eq "Oscalar"); # 14
87 test (ref $c eq "Oscalar"); # 15
88 test ( $a eq "087"); # 16
89 test ( $c eq "1"); # 17
90 test (ref $a eq "Oscalar"); # 18
95 test (ref $b eq "Oscalar"); # 19
96 test ( $a eq "087"); # 20
97 test ( $b eq "88"); # 21
98 test (ref $a eq "Oscalar"); # 22
100 eval q[ package Oscalar; use overload ('++' => sub { $ {$_[0]}++;$_[0] } ) ];
104 test (ref $a eq "Oscalar"); # 23
108 test (ref $b eq "Oscalar"); # 24
109 test ( $a eq "087"); # 25
110 test ( $b eq "88"); # 26
111 test (ref $a eq "Oscalar"); # 27
114 $dummy=bless \$dummy; # Now cache of method should be reloaded
120 test (ref $b eq "Oscalar"); # 28
121 test ( $a eq "087"); # 29
122 test ( $b eq "88"); # 30
123 test (ref $a eq "Oscalar"); # 31
126 eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ];
130 test (ref $a eq "Oscalar"); # 32
134 test (ref $b eq "Oscalar"); # 33
135 test ( $a eq "087"); # 34
136 test ( $b eq "88"); # 35
137 test (ref $a eq "Oscalar"); # 36
140 $dummy=bless \$dummy; # Now cache of method should be reloaded
145 test (ref $b eq "Oscalar"); # 37
146 test ( $a eq "087"); # 38
147 test ( $b eq "90"); # 39
148 test (ref $a eq "Oscalar"); # 40
153 test (ref $b eq "Oscalar"); # 41
154 test ( $a eq "087"); # 42
155 test ( $b eq "89"); # 43
156 test (ref $a eq "Oscalar"); # 44
161 eval q[ package Oscalar; use overload ('=' => sub {$main::copies++;
163 local $new=$ {$_[0]};
168 test (ref $b eq "Oscalar"); # 46
169 test ( $a eq "087"); # 47
170 test ( $b eq "087"); # 48
171 test (ref $a eq "Oscalar"); # 49
175 test (ref $b eq "Oscalar"); # 50
176 test ( $a eq "087"); # 51
177 test ( $b eq "89"); # 52
178 test (ref $a eq "Oscalar"); # 53
179 test ($copies == 0); # 54
183 test (ref $b eq "Oscalar"); # 55
184 test ( $a eq "087"); # 56
185 test ( $b eq "90"); # 57
186 test (ref $a eq "Oscalar"); # 58
187 test ($copies == 0); # 59
192 test (ref $b eq "Oscalar"); # 60
193 test ( $a eq "087"); # 61
194 test ( $b eq "88"); # 62
195 test (ref $a eq "Oscalar"); # 63
196 test ($copies == 0); # 64
201 test (ref $b eq "Oscalar") || print ref $b,"=ref(b)\n"; # 65
202 test ( $a eq "087"); # 66
203 test ( $b eq "89"); # 67
204 test (ref $a eq "Oscalar"); # 68
205 test ($copies == 1); # 69
207 eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1];
209 $c=new Oscalar; # Cause rehash
214 test (ref $b eq "Oscalar"); # 70
215 test ( $a eq "087"); # 71
216 test ( $b eq "90"); # 72
217 test (ref $a eq "Oscalar"); # 73
218 test ($copies == 2); # 74
222 test (ref $b eq "Oscalar"); # 75
223 test ( $b eq "360"); # 76
224 test ($copies == 2); # 77
227 test (ref $b eq "Oscalar"); # 78
228 test ( $b eq "-360"); # 79
229 test ($copies == 2); # 80
233 test (ref $b eq "Oscalar"); # 81
234 test ( $b eq "360"); # 82
235 test ($copies == 2); # 83
239 test (ref $b eq "Oscalar"); # 84
240 test ( $b eq "360"); # 85
241 test ($copies == 2); # 86
243 eval q[package Oscalar;
244 use overload ('x' => sub {new Oscalar ( $_[2] ? "_.$_[1]._" x $ {$_[0]}
245 : "_.${$_[0]}._" x $_[1])}) ];
249 test ($a eq "_.yy.__.yy.__.yy._"); # 87
251 eval q[package Oscalar;
252 use overload ('.' => sub {new Oscalar ( $_[2] ?
253 "_.$_[1].__.$ {$_[0]}._"
254 : "_.$ {$_[0]}.__.$_[1]._")}) ];
258 test ("b${a}c" eq "_._.b.__.xx._.__.c._"); # 88
260 # Check inheritance of overloading;
266 $aI = new OscalarI "$a";
267 test (ref $aI eq "OscalarI"); # 89
268 test ("$aI" eq "xx"); # 90
269 test ($aI eq "xx"); # 91
270 test ("b${aI}c" eq "_._.b.__.xx._.__.c._"); # 92
272 # Here we test blessing to a package updates hash
274 eval "package Oscalar; no overload '.'";
276 test ("b${a}" eq "_.b.__.xx._"); # 93
279 test ("b${a}c" eq "bxxc"); # 94
281 test ("b${a}c" eq "bxxc"); # 95
283 # Negative overloading:
286 test($@ =~ /no method found/); # 96
291 sub { *{"Oscalar::$AUTOLOAD"} = sub {"_!_" . shift() . "_!_"} ;
292 goto &{"Oscalar::$AUTOLOAD"}};
294 eval "package Oscalar; sub comple; use overload '~' => 'comple'";
296 $na = eval { ~$a }; # Hash was not updated
297 test($@ =~ /no method found/); # 97
301 $na = eval { ~$a }; # Hash updated
302 warn "`$na', $@" if $@;
304 test($na eq '_!_xx_!_'); # 99
308 $na = eval { ~$aI }; # Hash was not updated
309 test($@ =~ /no method found/); # 100
317 test($na eq '_!_xx_!_'); # 102
319 eval "package Oscalar; sub rshft; use overload '>>' => 'rshft'";
321 $na = eval { $aI >> 1 }; # Hash was not updated
322 test($@ =~ /no method found/); # 103
328 $na = eval { $aI >> 1 };
332 test($na eq '_!_xx_!_'); # 105
334 # warn overload::Method($a, '0+'), "\n";
335 test (overload::Method($a, '0+') eq \&Oscalar::numify); # 106
336 test (overload::Method($aI,'0+') eq \&Oscalar::numify); # 107
337 test (overload::Overloaded($aI)); # 108
338 test (!overload::Overloaded('overload')); # 109
340 test (! defined overload::Method($aI, '<<')); # 110
341 test (! defined overload::Method($a, '<')); # 111
343 test (overload::StrVal($aI) =~ /^OscalarI=SCALAR\(0x[\da-fA-F]+\)$/); # 112
344 test (overload::StrVal(\$aI) eq "@{[\$aI]}"); # 113
346 # Check overloading by methods (specified deep in the ISA tree).
350 sub Oscalar::lshft {"_<<_" . shift() . "_<<_"}
351 eval "package OscalarI; use overload '<<' => 'lshft', '|' => 'lshft'";
356 bless $aII, 'OscalarII';
357 bless \$fake, 'OscalarI'; # update the hash
358 test(($aI | 3) eq '_<<_xx_<<_'); # 114
360 test(($aII << 3) eq '_<<_087_<<_'); # 115