This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix example #4 in perlXStut
[perl5.git] / t / pragma / overload.t
CommitLineData
8ebc5c01
PP
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use Config;
9
10package Oscalar;
11use overload (
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]} :
23 ${$_[0]}/$_[1]},
24'%' => sub {new Oscalar
25 $_[2]? $_[1]%${$_[0]} : ${$_[0]}%$_[1]},
26'**' => sub {new Oscalar
27 $_[2]? $_[1]**${$_[0]} : ${$_[0]}-$_[1]},
28
29qw(
30"" stringify
310+ numify) # Order of arguments unsignificant
32);
33
34sub new {
35 my $foo = $_[1];
36 bless \$foo, $_[0];
37}
38
39sub stringify { "${$_[0]}" }
40sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead
41 # comparing to direct compilation based on
42 # stringify
43
44package main;
45
46$test = 0;
47$| = 1;
48print "1..",&last,"\n";
49
50sub test {
51 $test++; if (shift) {print "ok $test\n";1} else {print "not ok $test\n";0}
52}
53
54$a = new Oscalar "087";
55$b= "$a";
56
57# All test numbers in comments are off by 1.
58# So much for hard-wiring them in :-) To fix this:
59test(1); # 1
60
61test ($b eq $a); # 2
62test ($b eq "087"); # 3
63test (ref $a eq "Oscalar"); # 4
64test ($a eq $a); # 5
65test ($a eq "087"); # 6
66
67$c = $a + 7;
68
69test (ref $c eq "Oscalar"); # 7
70test (!($c eq $a)); # 8
71test ($c eq "94"); # 9
72
73$b=$a;
74
75test (ref $a eq "Oscalar"); # 10
76
77$b++;
78
79test (ref $b eq "Oscalar"); # 11
80test ( $a eq "087"); # 12
81test ( $b eq "88"); # 13
82test (ref $a eq "Oscalar"); # 14
83
84$c=$b;
85$c-=$a;
86
87test (ref $c eq "Oscalar"); # 15
88test ( $a eq "087"); # 16
89test ( $c eq "1"); # 17
90test (ref $a eq "Oscalar"); # 18
91
92$b=1;
93$b+=$a;
94
95test (ref $b eq "Oscalar"); # 19
96test ( $a eq "087"); # 20
97test ( $b eq "88"); # 21
98test (ref $a eq "Oscalar"); # 22
99
100eval q[ package Oscalar; use overload ('++' => sub { $ {$_[0]}++;$_[0] } ) ];
101
102$b=$a;
103
104test (ref $a eq "Oscalar"); # 23
105
106$b++;
107
108test (ref $b eq "Oscalar"); # 24
109test ( $a eq "087"); # 25
110test ( $b eq "88"); # 26
111test (ref $a eq "Oscalar"); # 27
112
113package Oscalar;
114$dummy=bless \$dummy; # Now cache of method should be reloaded
115package main;
116
117$b=$a;
118$b++;
119
120test (ref $b eq "Oscalar"); # 28
121test ( $a eq "087"); # 29
122test ( $b eq "88"); # 30
123test (ref $a eq "Oscalar"); # 31
124
125
126eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ];
127
128$b=$a;
129
130test (ref $a eq "Oscalar"); # 32
131
132$b++;
133
134test (ref $b eq "Oscalar"); # 33
135test ( $a eq "087"); # 34
136test ( $b eq "88"); # 35
137test (ref $a eq "Oscalar"); # 36
138
139package Oscalar;
140$dummy=bless \$dummy; # Now cache of method should be reloaded
141package main;
142
143$b++;
144
145test (ref $b eq "Oscalar"); # 37
146test ( $a eq "087"); # 38
147test ( $b eq "90"); # 39
148test (ref $a eq "Oscalar"); # 40
149
150$b=$a;
151$b++;
152
153test (ref $b eq "Oscalar"); # 41
154test ( $a eq "087"); # 42
155test ( $b eq "89"); # 43
156test (ref $a eq "Oscalar"); # 44
157
158
159test ($b? 1:0); # 45
160
161eval q[ package Oscalar; use overload ('=' => sub {$main::copies++;
162 package Oscalar;
163 local $new=$ {$_[0]};
164 bless \$new } ) ];
165
166$b=new Oscalar "$a";
167
168test (ref $b eq "Oscalar"); # 46
169test ( $a eq "087"); # 47
170test ( $b eq "087"); # 48
171test (ref $a eq "Oscalar"); # 49
172
173$b++;
174
175test (ref $b eq "Oscalar"); # 50
176test ( $a eq "087"); # 51
177test ( $b eq "89"); # 52
178test (ref $a eq "Oscalar"); # 53
179test ($copies == 0); # 54
180
181$b+=1;
182
183test (ref $b eq "Oscalar"); # 55
184test ( $a eq "087"); # 56
185test ( $b eq "90"); # 57
186test (ref $a eq "Oscalar"); # 58
187test ($copies == 0); # 59
188
189$b=$a;
190$b+=1;
191
192test (ref $b eq "Oscalar"); # 60
193test ( $a eq "087"); # 61
194test ( $b eq "88"); # 62
195test (ref $a eq "Oscalar"); # 63
196test ($copies == 0); # 64
197
198$b=$a;
199$b++;
200
201test (ref $b eq "Oscalar") || print ref $b,"=ref(b)\n"; # 65
202test ( $a eq "087"); # 66
203test ( $b eq "89"); # 67
204test (ref $a eq "Oscalar"); # 68
205test ($copies == 1); # 69
206
207eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1];
208 $_[0] } ) ];
209$c=new Oscalar; # Cause rehash
210
211$b=$a;
212$b+=1;
213
214test (ref $b eq "Oscalar"); # 70
215test ( $a eq "087"); # 71
216test ( $b eq "90"); # 72
217test (ref $a eq "Oscalar"); # 73
218test ($copies == 2); # 74
219
220$b+=$b;
221
222test (ref $b eq "Oscalar"); # 75
223test ( $b eq "360"); # 76
224test ($copies == 2); # 77
225$b=-$b;
226
227test (ref $b eq "Oscalar"); # 78
228test ( $b eq "-360"); # 79
229test ($copies == 2); # 80
230
231$b=abs($b);
232
233test (ref $b eq "Oscalar"); # 81
234test ( $b eq "360"); # 82
235test ($copies == 2); # 83
236
237$b=abs($b);
238
239test (ref $b eq "Oscalar"); # 84
240test ( $b eq "360"); # 85
241test ($copies == 2); # 86
242
243eval q[package Oscalar;
244 use overload ('x' => sub {new Oscalar ( $_[2] ? "_.$_[1]._" x $ {$_[0]}
245 : "_.${$_[0]}._" x $_[1])}) ];
246
247$a=new Oscalar "yy";
248$a x= 3;
249test ($a eq "_.yy.__.yy.__.yy._"); # 87
250
251eval q[package Oscalar;
252 use overload ('.' => sub {new Oscalar ( $_[2] ?
253 "_.$_[1].__.$ {$_[0]}._"
254 : "_.$ {$_[0]}.__.$_[1]._")}) ];
255
256$a=new Oscalar "xx";
257
258test ("b${a}c" eq "_._.b.__.xx._.__.c._"); # 88
259
260# Check inheritance of overloading;
261{
262 package OscalarI;
263 @ISA = 'Oscalar';
264}
265
266$aI = new OscalarI "$a";
267test (ref $aI eq "OscalarI"); # 89
268test ("$aI" eq "xx"); # 90
269test ($aI eq "xx"); # 91
270test ("b${aI}c" eq "_._.b.__.xx._.__.c._"); # 92
271
272# Here we test blessing to a package updates hash
273
274eval "package Oscalar; no overload '.'";
275
276test ("b${a}" eq "_.b.__.xx._"); # 93
277$x="1";
278bless \$x, Oscalar;
279test ("b${a}c" eq "bxxc"); # 94
280new Oscalar 1;
281test ("b${a}c" eq "bxxc"); # 95
282
283# Negative overloading:
284
285$na = eval { ~$a };
286test($@ =~ /no method found/); # 96
287
288# Check AUTOLOADING:
289
290*Oscalar::AUTOLOAD =
291 sub { *{"Oscalar::$AUTOLOAD"} = sub {"_!_" . shift() . "_!_"} ;
292 goto &{"Oscalar::$AUTOLOAD"}};
293
294eval "package Oscalar; use overload '~' => 'comple'";
295
296$na = eval { ~$a }; # Hash was not updated
297test($@ =~ /no method found/); # 97
298
299bless \$x, Oscalar;
300
301$na = eval { ~$a }; # Hash updated
302test !$@; # 98
303test($na eq '_!_xx_!_'); # 99
304
305$na = 0;
306
307$na = eval { ~$aI }; # Hash was not updated
308test($@ =~ /no method found/); # 100
309
310bless \$x, OscalarI;
311
312$na = eval { ~$aI };
313print $@;
314
315test !$@; # 101
316test($na eq '_!_xx_!_'); # 102
317
318eval "package Oscalar; use overload '>>' => 'rshft'";
319
320$na = eval { $aI >> 1 }; # Hash was not updated
321test($@ =~ /no method found/); # 103
322
323bless \$x, OscalarI;
324
325$na = 0;
326
327$na = eval { $aI >> 1 };
328print $@;
329
330test !$@; # 104
331test($na eq '_!_xx_!_'); # 105
332
333test (overload::Method($a, '0+') eq \&Oscalar::numify); # 106
334test (overload::Method($aI,'0+') eq \&Oscalar::numify); # 107
335test (overload::Overloaded($aI)); # 108
336test (!overload::Overloaded('overload')); # 109
337
338test (! defined overload::Method($aI, '<<')); # 110
339test (! defined overload::Method($a, '<')); # 111
340
341test (overload::StrVal($aI) =~ /^OscalarI=SCALAR\(0x[\da-fA-F]+\)$/); # 112
342test (overload::StrVal(\$aI) eq "@{[\$aI]}"); # 113
343
344# Last test is:
345sub last {113}