3 BEGIN { unshift @INC, './lib', '../lib';
4 require Config; import Config;
10 # Anonymous subroutines:
11 '+' => sub {new Oscalar ${$_[0]}+$_[1]},
12 '-' => sub {new Oscalar
13 $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]},
14 '<=>' => sub {new Oscalar
15 $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]},
16 'cmp' => sub {new Oscalar
17 $_[2]? ($_[1] cmp ${$_[0]}) : (${$_[0]} cmp $_[1])},
18 '*' => sub {new Oscalar ${$_[0]}*$_[1]},
19 '/' => sub {new Oscalar
20 $_[2]? $_[1]/${$_[0]} :
22 '%' => sub {new Oscalar
23 $_[2]? $_[1]%${$_[0]} : ${$_[0]}%$_[1]},
24 '**' => sub {new Oscalar
25 $_[2]? $_[1]**${$_[0]} : ${$_[0]}-$_[1]},
29 0+ numify) # Order of arguments unsignificant
37 sub stringify { "${$_[0]}" }
38 sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead
39 # comparing to direct compilation based on
46 print "1..",&last,"\n";
49 $test++; if (shift) {print "ok $test\n";1} else {print "not ok $test\n";0}
52 $a = new Oscalar "087";
55 test (!defined ref $b); # 1
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 $Oscalar::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 $Oscalar::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 $Oscalar::OVERLOAD{'='} = sub {$copies++; package Oscalar; local $new=${$_[0]};bless \$new};
160 test (ref $b eq "Oscalar"); # 46
161 test ( $a eq "087"); # 47
162 test ( $b eq "087"); # 48
163 test (ref $a eq "Oscalar"); # 49
167 test (ref $b eq "Oscalar"); # 50
168 test ( $a eq "087"); # 51
169 test ( $b eq "89"); # 52
170 test (ref $a eq "Oscalar"); # 53
171 test ($copies == 0); # 54
175 test (ref $b eq "Oscalar"); # 55
176 test ( $a eq "087"); # 56
177 test ( $b eq "90"); # 57
178 test (ref $a eq "Oscalar"); # 58
179 test ($copies == 0); # 59
184 test (ref $b eq "Oscalar"); # 60
185 test ( $a eq "087"); # 61
186 test ( $b eq "88"); # 62
187 test (ref $a eq "Oscalar"); # 63
188 test ($copies == 0); # 64
193 test (ref $b eq "Oscalar") || print ref $b,"=ref(b)\n"; # 65
194 test ( $a eq "087"); # 66
195 test ( $b eq "89"); # 67
196 test (ref $a eq "Oscalar"); # 68
197 test ($copies == 1); # 69
199 $Oscalar::OVERLOAD{'+='} = sub {${$_[0]}+=3*$_[1];$_[0]};
200 $c=new Oscalar; # Cause rehash
205 test (ref $b eq "Oscalar"); # 70
206 test ( $a eq "087"); # 71
207 test ( $b eq "90"); # 72
208 test (ref $a eq "Oscalar"); # 73
209 test ($copies == 2); # 74
213 test (ref $b eq "Oscalar"); # 75
214 test ( $b eq "360"); # 76
215 test ($copies == 2); # 77
218 test (ref $b eq "Oscalar"); # 78
219 test ( $b eq "-360"); # 79
220 test ($copies == 2); # 80
224 test (ref $b eq "Oscalar"); # 81
225 test ( $b eq "360"); # 82
226 test ($copies == 2); # 83
230 test (ref $b eq "Oscalar"); # 84
231 test ( $b eq "360"); # 85
232 test ($copies == 2); # 86
234 $Oscalar::OVERLOAD{'x'} = sub {new Oscalar ($_[2]? "_.$_[1]._" x ${$_[0]}:
235 "_.${$_[0]}._" x $_[1])};
239 test ($a eq "_.yy.__.yy.__.yy._"); # 87
241 $Oscalar::OVERLOAD{'.'} = sub {new Oscalar ($_[2]? "_.$_[1].__.${$_[0]}._":
242 "_.${$_[0]}.__.$_[1]._")};
246 test ("b${a}c" eq "_._.b.__.xx._.__.c._"); # 88
248 # Here we test blessing to a package updates hash
250 delete $Oscalar::OVERLOAD{'.'};
252 test ("b${a}" eq "_.b.__.xx._"); # 89
255 test ("b${a}c" eq "bxxc"); # 90
257 test ("b${a}c" eq "bxxc"); # 91