Commit | Line | Data |
---|---|---|
3340ac37 | 1 | #!./perl -T |
8ebc5c01 | 2 | |
3 | BEGIN { | |
4 | chdir 't' if -d 't'; | |
20822f61 | 5 | @INC = '../lib'; |
78cd8b71 | 6 | require Config; |
98641f60 | 7 | if (($Config::Config{'extensions'} !~ m!\bList/Util\b!) ){ |
78cd8b71 NC |
8 | print "1..0 # Skip -- Perl configured without List::Util module\n"; |
9 | exit 0; | |
10 | } | |
8ebc5c01 | 11 | } |
12 | ||
8ebc5c01 | 13 | package Oscalar; |
14 | use overload ( | |
15 | # Anonymous subroutines: | |
16 | '+' => sub {new Oscalar $ {$_[0]}+$_[1]}, | |
17 | '-' => sub {new Oscalar | |
18 | $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]}, | |
19 | '<=>' => sub {new Oscalar | |
20 | $_[2]? $_[1]-${$_[0]} : ${$_[0]}-$_[1]}, | |
21 | 'cmp' => sub {new Oscalar | |
22 | $_[2]? ($_[1] cmp ${$_[0]}) : (${$_[0]} cmp $_[1])}, | |
23 | '*' => sub {new Oscalar ${$_[0]}*$_[1]}, | |
24 | '/' => sub {new Oscalar | |
25 | $_[2]? $_[1]/${$_[0]} : | |
26 | ${$_[0]}/$_[1]}, | |
27 | '%' => sub {new Oscalar | |
28 | $_[2]? $_[1]%${$_[0]} : ${$_[0]}%$_[1]}, | |
29 | '**' => sub {new Oscalar | |
30 | $_[2]? $_[1]**${$_[0]} : ${$_[0]}-$_[1]}, | |
31 | ||
32 | qw( | |
33 | "" stringify | |
b0bf6df7 | 34 | 0+ numify) # Order of arguments insignificant |
8ebc5c01 | 35 | ); |
36 | ||
37 | sub new { | |
38 | my $foo = $_[1]; | |
39 | bless \$foo, $_[0]; | |
40 | } | |
41 | ||
42 | sub stringify { "${$_[0]}" } | |
43 | sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead | |
44 | # comparing to direct compilation based on | |
45 | # stringify | |
46 | ||
47 | package main; | |
48 | ||
8ebc5c01 | 49 | $| = 1; |
25222ff9 | 50 | BEGIN { require './test.pl' } |
9426e1a5 | 51 | plan tests => 4980; |
b1fbf5c3 | 52 | |
a02ec77a | 53 | use Scalar::Util qw(tainted); |
8ebc5c01 | 54 | |
55 | $a = new Oscalar "087"; | |
56 | $b= "$a"; | |
57 | ||
b1fbf5c3 NC |
58 | is($b, $a); |
59 | is($b, "087"); | |
60 | is(ref $a, "Oscalar"); | |
61 | is($a, $a); | |
62 | is($a, "087"); | |
8ebc5c01 | 63 | |
64 | $c = $a + 7; | |
65 | ||
b1fbf5c3 NC |
66 | is(ref $c, "Oscalar"); |
67 | isnt($c, $a); | |
68 | is($c, "94"); | |
8ebc5c01 | 69 | |
70 | $b=$a; | |
71 | ||
b1fbf5c3 | 72 | is(ref $a, "Oscalar"); |
8ebc5c01 | 73 | |
74 | $b++; | |
75 | ||
b1fbf5c3 NC |
76 | is(ref $b, "Oscalar"); |
77 | is($a, "087"); | |
78 | is($b, "88"); | |
79 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 80 | |
81 | $c=$b; | |
82 | $c-=$a; | |
83 | ||
b1fbf5c3 NC |
84 | is(ref $c, "Oscalar"); |
85 | is($a, "087"); | |
86 | is($c, "1"); | |
87 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 88 | |
89 | $b=1; | |
90 | $b+=$a; | |
91 | ||
b1fbf5c3 NC |
92 | is(ref $b, "Oscalar"); |
93 | is($a, "087"); | |
94 | is($b, "88"); | |
95 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 96 | |
97 | eval q[ package Oscalar; use overload ('++' => sub { $ {$_[0]}++;$_[0] } ) ]; | |
98 | ||
99 | $b=$a; | |
100 | ||
b1fbf5c3 | 101 | is(ref $a, "Oscalar"); |
8ebc5c01 | 102 | |
103 | $b++; | |
104 | ||
b1fbf5c3 NC |
105 | is(ref $b, "Oscalar"); |
106 | is($a, "087"); | |
107 | is($b, "88"); | |
108 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 109 | |
110 | package Oscalar; | |
111 | $dummy=bless \$dummy; # Now cache of method should be reloaded | |
112 | package main; | |
113 | ||
114 | $b=$a; | |
115 | $b++; | |
116 | ||
b1fbf5c3 NC |
117 | is(ref $b, "Oscalar"); |
118 | is($a, "087"); | |
119 | is($b, "88"); | |
120 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 121 | |
32251b26 | 122 | undef $b; # Destroying updates tables too... |
8ebc5c01 | 123 | |
124 | eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ]; | |
125 | ||
126 | $b=$a; | |
127 | ||
b1fbf5c3 | 128 | is(ref $a, "Oscalar"); |
8ebc5c01 | 129 | |
130 | $b++; | |
131 | ||
b1fbf5c3 NC |
132 | is(ref $b, "Oscalar"); |
133 | is($a, "087"); | |
134 | is($b, "88"); | |
135 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 136 | |
137 | package Oscalar; | |
138 | $dummy=bless \$dummy; # Now cache of method should be reloaded | |
139 | package main; | |
140 | ||
141 | $b++; | |
142 | ||
b1fbf5c3 NC |
143 | is(ref $b, "Oscalar"); |
144 | is($a, "087"); | |
145 | is($b, "90"); | |
146 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 147 | |
148 | $b=$a; | |
149 | $b++; | |
150 | ||
b1fbf5c3 NC |
151 | is(ref $b, "Oscalar"); |
152 | is($a, "087"); | |
153 | is($b, "89"); | |
154 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 155 | |
156 | ||
b1fbf5c3 | 157 | ok($b? 1:0); |
8ebc5c01 | 158 | |
159 | eval q[ package Oscalar; use overload ('=' => sub {$main::copies++; | |
160 | package Oscalar; | |
161 | local $new=$ {$_[0]}; | |
162 | bless \$new } ) ]; | |
163 | ||
164 | $b=new Oscalar "$a"; | |
165 | ||
b1fbf5c3 NC |
166 | is(ref $b, "Oscalar"); |
167 | is($a, "087"); | |
168 | is($b, "087"); | |
169 | is(ref $a, "Oscalar"); | |
8ebc5c01 | 170 | |
171 | $b++; | |
172 | ||
b1fbf5c3 NC |
173 | is(ref $b, "Oscalar"); |
174 | is($a, "087"); | |
175 | is($b, "89"); | |
176 | is(ref $a, "Oscalar"); | |
177 | is($copies, undef); | |
8ebc5c01 | 178 | |
179 | $b+=1; | |
180 | ||
b1fbf5c3 NC |
181 | is(ref $b, "Oscalar"); |
182 | is($a, "087"); | |
183 | is($b, "90"); | |
184 | is(ref $a, "Oscalar"); | |
185 | is($copies, undef); | |
8ebc5c01 | 186 | |
187 | $b=$a; | |
188 | $b+=1; | |
189 | ||
b1fbf5c3 NC |
190 | is(ref $b, "Oscalar"); |
191 | is($a, "087"); | |
192 | is($b, "88"); | |
193 | is(ref $a, "Oscalar"); | |
194 | is($copies, undef); | |
8ebc5c01 | 195 | |
196 | $b=$a; | |
197 | $b++; | |
198 | ||
b1fbf5c3 NC |
199 | is(ref $b, "Oscalar"); |
200 | is($a, "087"); | |
201 | is($b, "89"); | |
202 | is(ref $a, "Oscalar"); | |
203 | is($copies, 1); | |
8ebc5c01 | 204 | |
205 | eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1]; | |
206 | $_[0] } ) ]; | |
207 | $c=new Oscalar; # Cause rehash | |
208 | ||
209 | $b=$a; | |
210 | $b+=1; | |
211 | ||
b1fbf5c3 NC |
212 | is(ref $b, "Oscalar"); |
213 | is($a, "087"); | |
214 | is($b, "90"); | |
215 | is(ref $a, "Oscalar"); | |
216 | is($copies, 2); | |
8ebc5c01 | 217 | |
218 | $b+=$b; | |
219 | ||
b1fbf5c3 NC |
220 | is(ref $b, "Oscalar"); |
221 | is($b, "360"); | |
222 | is($copies, 2); | |
8ebc5c01 | 223 | $b=-$b; |
224 | ||
b1fbf5c3 NC |
225 | is(ref $b, "Oscalar"); |
226 | is($b, "-360"); | |
227 | is($copies, 2); | |
8ebc5c01 | 228 | |
229 | $b=abs($b); | |
230 | ||
b1fbf5c3 NC |
231 | is(ref $b, "Oscalar"); |
232 | is($b, "360"); | |
233 | is($copies, 2); | |
8ebc5c01 | 234 | |
235 | $b=abs($b); | |
236 | ||
b1fbf5c3 NC |
237 | is(ref $b, "Oscalar"); |
238 | is($b, "360"); | |
239 | is($copies, 2); | |
8ebc5c01 | 240 | |
241 | eval q[package Oscalar; | |
242 | use overload ('x' => sub {new Oscalar ( $_[2] ? "_.$_[1]._" x $ {$_[0]} | |
243 | : "_.${$_[0]}._" x $_[1])}) ]; | |
244 | ||
245 | $a=new Oscalar "yy"; | |
246 | $a x= 3; | |
b1fbf5c3 | 247 | is($a, "_.yy.__.yy.__.yy._"); |
8ebc5c01 | 248 | |
249 | eval q[package Oscalar; | |
250 | use overload ('.' => sub {new Oscalar ( $_[2] ? | |
251 | "_.$_[1].__.$ {$_[0]}._" | |
252 | : "_.$ {$_[0]}.__.$_[1]._")}) ]; | |
253 | ||
254 | $a=new Oscalar "xx"; | |
255 | ||
b1fbf5c3 | 256 | is("b${a}c", "_._.b.__.xx._.__.c._"); |
8ebc5c01 | 257 | |
258 | # Check inheritance of overloading; | |
259 | { | |
260 | package OscalarI; | |
261 | @ISA = 'Oscalar'; | |
262 | } | |
263 | ||
264 | $aI = new OscalarI "$a"; | |
b1fbf5c3 NC |
265 | is(ref $aI, "OscalarI"); |
266 | is("$aI", "xx"); | |
267 | is($aI, "xx"); | |
268 | is("b${aI}c", "_._.b.__.xx._.__.c._"); | |
8ebc5c01 | 269 | |
270 | # Here we test blessing to a package updates hash | |
271 | ||
272 | eval "package Oscalar; no overload '.'"; | |
273 | ||
b1fbf5c3 | 274 | is("b${a}", "_.b.__.xx._"); |
8ebc5c01 | 275 | $x="1"; |
276 | bless \$x, Oscalar; | |
b1fbf5c3 | 277 | is("b${a}c", "bxxc"); |
8ebc5c01 | 278 | new Oscalar 1; |
b1fbf5c3 | 279 | is("b${a}c", "bxxc"); |
8ebc5c01 | 280 | |
281 | # Negative overloading: | |
282 | ||
283 | $na = eval { ~$a }; | |
b1fbf5c3 | 284 | like($@, qr/no method found/); |
8ebc5c01 | 285 | |
286 | # Check AUTOLOADING: | |
287 | ||
288 | *Oscalar::AUTOLOAD = | |
289 | sub { *{"Oscalar::$AUTOLOAD"} = sub {"_!_" . shift() . "_!_"} ; | |
290 | goto &{"Oscalar::$AUTOLOAD"}}; | |
291 | ||
44a8e56a | 292 | eval "package Oscalar; sub comple; use overload '~' => 'comple'"; |
8ebc5c01 | 293 | |
294 | $na = eval { ~$a }; # Hash was not updated | |
b1fbf5c3 | 295 | like($@, qr/no method found/); |
8ebc5c01 | 296 | |
297 | bless \$x, Oscalar; | |
298 | ||
299 | $na = eval { ~$a }; # Hash updated | |
44a8e56a | 300 | warn "`$na', $@" if $@; |
b1fbf5c3 NC |
301 | ok !$@; |
302 | is($na, '_!_xx_!_'); | |
8ebc5c01 | 303 | |
304 | $na = 0; | |
305 | ||
306 | $na = eval { ~$aI }; # Hash was not updated | |
fdb7e2a2 | 307 | like($@, qr/no method found/); |
8ebc5c01 | 308 | |
309 | bless \$x, OscalarI; | |
310 | ||
311 | $na = eval { ~$aI }; | |
312 | print $@; | |
313 | ||
fdb7e2a2 NC |
314 | ok(!$@); |
315 | is($na, '_!_xx_!_'); | |
8ebc5c01 | 316 | |
44a8e56a | 317 | eval "package Oscalar; sub rshft; use overload '>>' => 'rshft'"; |
8ebc5c01 | 318 | |
319 | $na = eval { $aI >> 1 }; # Hash was not updated | |
fdb7e2a2 | 320 | like($@, qr/no method found/); |
8ebc5c01 | 321 | |
322 | bless \$x, OscalarI; | |
323 | ||
324 | $na = 0; | |
325 | ||
326 | $na = eval { $aI >> 1 }; | |
327 | print $@; | |
328 | ||
fdb7e2a2 NC |
329 | ok(!$@); |
330 | is($na, '_!_xx_!_'); | |
8ebc5c01 | 331 | |
44a8e56a | 332 | # warn overload::Method($a, '0+'), "\n"; |
fdb7e2a2 NC |
333 | is(overload::Method($a, '0+'), \&Oscalar::numify); |
334 | is(overload::Method($aI,'0+'), \&Oscalar::numify); | |
335 | ok(overload::Overloaded($aI)); | |
336 | ok(!overload::Overloaded('overload')); | |
8ebc5c01 | 337 | |
fdb7e2a2 NC |
338 | ok(! defined overload::Method($aI, '<<')); |
339 | ok(! defined overload::Method($a, '<')); | |
8ebc5c01 | 340 | |
fdb7e2a2 NC |
341 | like (overload::StrVal($aI), qr/^OscalarI=SCALAR\(0x[\da-fA-F]+\)$/); |
342 | is(overload::StrVal(\$aI), "@{[\$aI]}"); | |
8ebc5c01 | 343 | |
44a8e56a | 344 | # Check overloading by methods (specified deep in the ISA tree). |
345 | { | |
346 | package OscalarII; | |
347 | @ISA = 'OscalarI'; | |
348 | sub Oscalar::lshft {"_<<_" . shift() . "_<<_"} | |
349 | eval "package OscalarI; use overload '<<' => 'lshft', '|' => 'lshft'"; | |
350 | } | |
351 | ||
352 | $aaII = "087"; | |
353 | $aII = \$aaII; | |
354 | bless $aII, 'OscalarII'; | |
355 | bless \$fake, 'OscalarI'; # update the hash | |
fdb7e2a2 | 356 | is(($aI | 3), '_<<_xx_<<_'); |
44a8e56a | 357 | # warn $aII << 3; |
fdb7e2a2 | 358 | is(($aII << 3), '_<<_087_<<_'); |
44a8e56a | 359 | |
b3ac6de7 IZ |
360 | { |
361 | BEGIN { $int = 7; overload::constant 'integer' => sub {$int++; shift}; } | |
362 | $out = 2**10; | |
363 | } | |
fdb7e2a2 NC |
364 | is($int, 9); |
365 | is($out, 1024); | |
7898bf0b RGS |
366 | is($int, 9); |
367 | { | |
368 | BEGIN { overload::constant 'integer' => sub {$int++; shift()+1}; } | |
369 | eval q{$out = 42}; | |
370 | } | |
371 | is($int, 10); | |
372 | is($out, 43); | |
b3ac6de7 IZ |
373 | |
374 | $foo = 'foo'; | |
375 | $foo1 = 'f\'o\\o'; | |
376 | { | |
377 | BEGIN { $q = $qr = 7; | |
378 | overload::constant 'q' => sub {$q++; push @q, shift, ($_[1] || 'none'); shift}, | |
379 | 'qr' => sub {$qr++; push @qr, shift, ($_[1] || 'none'); shift}; } | |
380 | $out = 'foo'; | |
381 | $out1 = 'f\'o\\o'; | |
382 | $out2 = "a\a$foo,\,"; | |
383 | /b\b$foo.\./; | |
384 | } | |
385 | ||
fdb7e2a2 NC |
386 | is($out, 'foo'); |
387 | is($out, $foo); | |
388 | is($out1, 'f\'o\\o'); | |
389 | is($out1, $foo1); | |
390 | is($out2, "a\afoo,\,"); | |
391 | is("@q", "foo q f'o\\\\o q a\\a qq ,\\, qq"); | |
392 | is($q, 11); | |
393 | is("@qr", "b\\b qq .\\. qq"); | |
394 | is($qr, 9); | |
b3ac6de7 IZ |
395 | |
396 | { | |
397 | $_ = '!<b>!foo!<-.>!'; | |
398 | BEGIN { overload::constant 'q' => sub {push @q1, shift, ($_[1] || 'none'); "_<" . (shift) . ">_"}, | |
399 | 'qr' => sub {push @qr1, shift, ($_[1] || 'none'); "!<" . (shift) . ">!"}; } | |
400 | $out = 'foo'; | |
401 | $out1 = 'f\'o\\o'; | |
402 | $out2 = "a\a$foo,\,"; | |
403 | $res = /b\b$foo.\./; | |
404 | $a = <<EOF; | |
405 | oups | |
406 | EOF | |
407 | $b = <<'EOF'; | |
408 | oups1 | |
409 | EOF | |
410 | $c = bareword; | |
411 | m'try it'; | |
412 | s'first part'second part'; | |
413 | s/yet another/tail here/; | |
c2e66d9e | 414 | tr/A-Z/a-z/; |
b3ac6de7 IZ |
415 | } |
416 | ||
fdb7e2a2 NC |
417 | is($out, '_<foo>_'); |
418 | is($out1, '_<f\'o\\o>_'); | |
419 | is($out2, "_<a\a>_foo_<,\,>_"); | |
420 | is("@q1", "foo q f'o\\\\o q a\\a qq ,\\, qq oups | |
b3ac6de7 | 421 | qq oups1 |
fdb7e2a2 NC |
422 | q second part q tail here s A-Z tr a-z tr"); |
423 | is("@qr1", "b\\b qq .\\. qq try it q first part q yet another qq"); | |
424 | is($res, 1); | |
425 | is($a, "_<oups | |
426 | >_"); | |
427 | is($b, "_<oups1 | |
428 | >_"); | |
429 | is($c, "bareword"); | |
b3ac6de7 | 430 | |
ee239bfe IZ |
431 | { |
432 | package symbolic; # Primitive symbolic calculator | |
433 | use overload nomethod => \&wrap, '""' => \&str, '0+' => \&num, | |
434 | '=' => \&cpy, '++' => \&inc, '--' => \&dec; | |
435 | ||
436 | sub new { shift; bless ['n', @_] } | |
437 | sub cpy { | |
438 | my $self = shift; | |
439 | bless [@$self], ref $self; | |
440 | } | |
441 | sub inc { $_[0] = bless ['++', $_[0], 1]; } | |
442 | sub dec { $_[0] = bless ['--', $_[0], 1]; } | |
443 | sub wrap { | |
444 | my ($obj, $other, $inv, $meth) = @_; | |
445 | if ($meth eq '++' or $meth eq '--') { | |
446 | @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference | |
447 | return $obj; | |
448 | } | |
449 | ($obj, $other) = ($other, $obj) if $inv; | |
450 | bless [$meth, $obj, $other]; | |
451 | } | |
452 | sub str { | |
453 | my ($meth, $a, $b) = @{+shift}; | |
454 | $a = 'u' unless defined $a; | |
455 | if (defined $b) { | |
456 | "[$meth $a $b]"; | |
457 | } else { | |
458 | "[$meth $a]"; | |
459 | } | |
460 | } | |
461 | my %subr = ( 'n' => sub {$_[0]} ); | |
462 | foreach my $op (split " ", $overload::ops{with_assign}) { | |
463 | $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}"; | |
464 | } | |
465 | my @bins = qw(binary 3way_comparison num_comparison str_comparison); | |
466 | foreach my $op (split " ", "@overload::ops{ @bins }") { | |
467 | $subr{$op} = eval "sub {shift() $op shift()}"; | |
468 | } | |
469 | foreach my $op (split " ", "@overload::ops{qw(unary func)}") { | |
470 | $subr{$op} = eval "sub {$op shift()}"; | |
471 | } | |
472 | $subr{'++'} = $subr{'+'}; | |
473 | $subr{'--'} = $subr{'-'}; | |
474 | ||
475 | sub num { | |
476 | my ($meth, $a, $b) = @{+shift}; | |
477 | my $subr = $subr{$meth} | |
478 | or die "Do not know how to ($meth) in symbolic"; | |
479 | $a = $a->num if ref $a eq __PACKAGE__; | |
480 | $b = $b->num if ref $b eq __PACKAGE__; | |
481 | $subr->($a,$b); | |
482 | } | |
483 | sub TIESCALAR { my $pack = shift; $pack->new(@_) } | |
484 | sub FETCH { shift } | |
485 | sub nop { } # Around a bug | |
486 | sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; } | |
487 | sub STORE { | |
488 | my $obj = shift; | |
489 | $#$obj = 1; | |
a1063b2d | 490 | $obj->[1] = shift; |
ee239bfe IZ |
491 | } |
492 | } | |
493 | ||
494 | { | |
495 | my $foo = new symbolic 11; | |
496 | my $baz = $foo++; | |
fdb7e2a2 NC |
497 | is((sprintf "%d", $foo), '12'); |
498 | is((sprintf "%d", $baz), '11'); | |
ee239bfe IZ |
499 | my $bar = $foo; |
500 | $baz = ++$foo; | |
fdb7e2a2 NC |
501 | is((sprintf "%d", $foo), '13'); |
502 | is((sprintf "%d", $bar), '12'); | |
503 | is((sprintf "%d", $baz), '13'); | |
ee239bfe IZ |
504 | my $ban = $foo; |
505 | $baz = ($foo += 1); | |
fdb7e2a2 NC |
506 | is((sprintf "%d", $foo), '14'); |
507 | is((sprintf "%d", $bar), '12'); | |
508 | is((sprintf "%d", $baz), '14'); | |
509 | is((sprintf "%d", $ban), '13'); | |
ee239bfe IZ |
510 | $baz = 0; |
511 | $baz = $foo++; | |
fdb7e2a2 NC |
512 | is((sprintf "%d", $foo), '15'); |
513 | is((sprintf "%d", $baz), '14'); | |
514 | is("$foo", '[++ [+= [++ [++ [n 11] 1] 1] 1] 1]'); | |
ee239bfe IZ |
515 | } |
516 | ||
517 | { | |
518 | my $iter = new symbolic 2; | |
519 | my $side = new symbolic 1; | |
520 | my $cnt = $iter; | |
521 | ||
522 | while ($cnt) { | |
523 | $cnt = $cnt - 1; # The "simple" way | |
524 | $side = (sqrt(1 + $side**2) - 1)/$side; | |
525 | } | |
526 | my $pi = $side*(2**($iter+2)); | |
fdb7e2a2 NC |
527 | is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]'); |
528 | is((sprintf "%f", $pi), '3.182598'); | |
ee239bfe IZ |
529 | } |
530 | ||
531 | { | |
532 | my $iter = new symbolic 2; | |
533 | my $side = new symbolic 1; | |
534 | my $cnt = $iter; | |
535 | ||
536 | while ($cnt--) { | |
537 | $side = (sqrt(1 + $side**2) - 1)/$side; | |
538 | } | |
539 | my $pi = $side*(2**($iter+2)); | |
fdb7e2a2 NC |
540 | is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]'); |
541 | is((sprintf "%f", $pi), '3.182598'); | |
ee239bfe IZ |
542 | } |
543 | ||
544 | { | |
545 | my ($a, $b); | |
546 | symbolic->vars($a, $b); | |
547 | my $c = sqrt($a**2 + $b**2); | |
548 | $a = 3; $b = 4; | |
fdb7e2a2 | 549 | is((sprintf "%d", $c), '5'); |
ee239bfe | 550 | $a = 12; $b = 5; |
fdb7e2a2 | 551 | is((sprintf "%d", $c), '13'); |
ee239bfe IZ |
552 | } |
553 | ||
554 | { | |
555 | package symbolic1; # Primitive symbolic calculator | |
556 | # Mutator inc/dec | |
557 | use overload nomethod => \&wrap, '""' => \&str, '0+' => \&num, '=' => \&cpy; | |
558 | ||
559 | sub new { shift; bless ['n', @_] } | |
560 | sub cpy { | |
561 | my $self = shift; | |
562 | bless [@$self], ref $self; | |
563 | } | |
564 | sub wrap { | |
565 | my ($obj, $other, $inv, $meth) = @_; | |
566 | if ($meth eq '++' or $meth eq '--') { | |
567 | @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference | |
568 | return $obj; | |
569 | } | |
570 | ($obj, $other) = ($other, $obj) if $inv; | |
571 | bless [$meth, $obj, $other]; | |
572 | } | |
573 | sub str { | |
574 | my ($meth, $a, $b) = @{+shift}; | |
575 | $a = 'u' unless defined $a; | |
576 | if (defined $b) { | |
577 | "[$meth $a $b]"; | |
578 | } else { | |
579 | "[$meth $a]"; | |
580 | } | |
581 | } | |
582 | my %subr = ( 'n' => sub {$_[0]} ); | |
583 | foreach my $op (split " ", $overload::ops{with_assign}) { | |
584 | $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}"; | |
585 | } | |
586 | my @bins = qw(binary 3way_comparison num_comparison str_comparison); | |
587 | foreach my $op (split " ", "@overload::ops{ @bins }") { | |
588 | $subr{$op} = eval "sub {shift() $op shift()}"; | |
589 | } | |
590 | foreach my $op (split " ", "@overload::ops{qw(unary func)}") { | |
591 | $subr{$op} = eval "sub {$op shift()}"; | |
592 | } | |
593 | $subr{'++'} = $subr{'+'}; | |
594 | $subr{'--'} = $subr{'-'}; | |
595 | ||
596 | sub num { | |
597 | my ($meth, $a, $b) = @{+shift}; | |
598 | my $subr = $subr{$meth} | |
599 | or die "Do not know how to ($meth) in symbolic"; | |
600 | $a = $a->num if ref $a eq __PACKAGE__; | |
601 | $b = $b->num if ref $b eq __PACKAGE__; | |
602 | $subr->($a,$b); | |
603 | } | |
604 | sub TIESCALAR { my $pack = shift; $pack->new(@_) } | |
605 | sub FETCH { shift } | |
606 | sub nop { } # Around a bug | |
607 | sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; } | |
608 | sub STORE { | |
609 | my $obj = shift; | |
610 | $#$obj = 1; | |
a1063b2d | 611 | $obj->[1] = shift; |
ee239bfe IZ |
612 | } |
613 | } | |
614 | ||
615 | { | |
616 | my $foo = new symbolic1 11; | |
617 | my $baz = $foo++; | |
fdb7e2a2 NC |
618 | is((sprintf "%d", $foo), '12'); |
619 | is((sprintf "%d", $baz), '11'); | |
ee239bfe IZ |
620 | my $bar = $foo; |
621 | $baz = ++$foo; | |
fdb7e2a2 NC |
622 | is((sprintf "%d", $foo), '13'); |
623 | is((sprintf "%d", $bar), '12'); | |
624 | is((sprintf "%d", $baz), '13'); | |
ee239bfe IZ |
625 | my $ban = $foo; |
626 | $baz = ($foo += 1); | |
fdb7e2a2 NC |
627 | is((sprintf "%d", $foo), '14'); |
628 | is((sprintf "%d", $bar), '12'); | |
629 | is((sprintf "%d", $baz), '14'); | |
630 | is((sprintf "%d", $ban), '13'); | |
ee239bfe IZ |
631 | $baz = 0; |
632 | $baz = $foo++; | |
fdb7e2a2 NC |
633 | is((sprintf "%d", $foo), '15'); |
634 | is((sprintf "%d", $baz), '14'); | |
635 | is("$foo", '[++ [+= [++ [++ [n 11] 1] 1] 1] 1]'); | |
ee239bfe IZ |
636 | } |
637 | ||
638 | { | |
639 | my $iter = new symbolic1 2; | |
640 | my $side = new symbolic1 1; | |
641 | my $cnt = $iter; | |
642 | ||
643 | while ($cnt) { | |
644 | $cnt = $cnt - 1; # The "simple" way | |
645 | $side = (sqrt(1 + $side**2) - 1)/$side; | |
646 | } | |
647 | my $pi = $side*(2**($iter+2)); | |
fdb7e2a2 NC |
648 | is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]'); |
649 | is((sprintf "%f", $pi), '3.182598'); | |
ee239bfe IZ |
650 | } |
651 | ||
652 | { | |
653 | my $iter = new symbolic1 2; | |
654 | my $side = new symbolic1 1; | |
655 | my $cnt = $iter; | |
656 | ||
657 | while ($cnt--) { | |
658 | $side = (sqrt(1 + $side**2) - 1)/$side; | |
659 | } | |
660 | my $pi = $side*(2**($iter+2)); | |
fdb7e2a2 NC |
661 | is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]'); |
662 | is((sprintf "%f", $pi), '3.182598'); | |
ee239bfe IZ |
663 | } |
664 | ||
665 | { | |
666 | my ($a, $b); | |
667 | symbolic1->vars($a, $b); | |
668 | my $c = sqrt($a**2 + $b**2); | |
669 | $a = 3; $b = 4; | |
fdb7e2a2 | 670 | is((sprintf "%d", $c), '5'); |
ee239bfe | 671 | $a = 12; $b = 5; |
fdb7e2a2 | 672 | is((sprintf "%d", $c), '13'); |
ee239bfe IZ |
673 | } |
674 | ||
675 | { | |
676 | package two_face; # Scalars with separate string and | |
677 | # numeric values. | |
678 | sub new { my $p = shift; bless [@_], $p } | |
679 | use overload '""' => \&str, '0+' => \&num, fallback => 1; | |
680 | sub num {shift->[1]} | |
681 | sub str {shift->[0]} | |
682 | } | |
683 | ||
684 | { | |
685 | my $seven = new two_face ("vii", 7); | |
fdb7e2a2 | 686 | is((sprintf "seven=$seven, seven=%d, eight=%d", $seven, $seven+1), |
ee239bfe | 687 | 'seven=vii, seven=7, eight=8'); |
fdb7e2a2 | 688 | is(scalar ($seven =~ /i/), '1'); |
ee239bfe | 689 | } |
b3ac6de7 | 690 | |
d0ecd44c IZ |
691 | { |
692 | package sorting; | |
693 | use overload 'cmp' => \∁ | |
694 | sub new { my ($p, $v) = @_; bless \$v, $p } | |
695 | sub comp { my ($x,$y) = @_; ($$x * 3 % 10) <=> ($$y * 3 % 10) or $$x cmp $$y } | |
696 | } | |
697 | { | |
698 | my @arr = map sorting->new($_), 0..12; | |
699 | my @sorted1 = sort @arr; | |
700 | my @sorted2 = map $$_, @sorted1; | |
7bf00c86 | 701 | is("@sorted2", '0 10 7 4 1 11 8 5 12 2 9 6 3'); |
d0ecd44c | 702 | } |
f5284f61 IZ |
703 | { |
704 | package iterator; | |
705 | use overload '<>' => \&iter; | |
706 | sub new { my ($p, $v) = @_; bless \$v, $p } | |
707 | sub iter { my ($x) = @_; return undef if $$x < 0; return $$x--; } | |
708 | } | |
72b16652 | 709 | |
9426e1a5 | 710 | { |
f5284f61 IZ |
711 | my $iter = iterator->new(5); |
712 | my $acc = ''; | |
713 | my $out; | |
714 | $acc .= " $out" while $out = <${iter}>; | |
7bf00c86 | 715 | is($acc, ' 5 4 3 2 1 0'); |
f5284f61 | 716 | $iter = iterator->new(5); |
7bf00c86 | 717 | is(scalar <${iter}>, '5'); |
f5284f61 | 718 | $acc = ''; |
b04ef359 | 719 | $acc .= " $out" while $out = <$iter>; |
7bf00c86 | 720 | is($acc, ' 4 3 2 1 0'); |
f5284f61 IZ |
721 | } |
722 | { | |
723 | package deref; | |
724 | use overload '%{}' => \&hderef, '&{}' => \&cderef, | |
725 | '*{}' => \&gderef, '${}' => \&sderef, '@{}' => \&aderef; | |
726 | sub new { my ($p, $v) = @_; bless \$v, $p } | |
727 | sub deref { | |
728 | my ($self, $key) = (shift, shift); | |
729 | my $class = ref $self; | |
730 | bless $self, 'deref::dummy'; # Disable overloading of %{} | |
731 | my $out = $self->{$key}; | |
732 | bless $self, $class; # Restore overloading | |
733 | $out; | |
734 | } | |
735 | sub hderef {shift->deref('h')} | |
736 | sub aderef {shift->deref('a')} | |
737 | sub cderef {shift->deref('c')} | |
738 | sub gderef {shift->deref('g')} | |
739 | sub sderef {shift->deref('s')} | |
740 | } | |
741 | { | |
742 | my $deref = bless { h => { foo => 5 , fake => 23 }, | |
743 | c => sub {return shift() + 34}, | |
744 | 's' => \123, | |
745 | a => [11..13], | |
746 | g => \*srt, | |
747 | }, 'deref'; | |
748 | # Hash: | |
749 | my @cont = sort %$deref; | |
b0bf6df7 | 750 | if ("\t" eq "\011") { # ASCII |
7bf00c86 | 751 | is("@cont", '23 5 fake foo'); |
f70c35af | 752 | } |
b0bf6df7 | 753 | else { # EBCDIC alpha-numeric sort order |
7bf00c86 | 754 | is("@cont", 'fake foo 23 5'); |
f70c35af | 755 | } |
f5284f61 | 756 | my @keys = sort keys %$deref; |
7bf00c86 | 757 | is("@keys", 'fake foo'); |
f5284f61 | 758 | my @val = sort values %$deref; |
7bf00c86 NC |
759 | is("@val", '23 5'); |
760 | is($deref->{foo}, 5); | |
761 | is(defined $deref->{bar}, ''); | |
f5284f61 IZ |
762 | my $key; |
763 | @keys = (); | |
764 | push @keys, $key while $key = each %$deref; | |
765 | @keys = sort @keys; | |
7bf00c86 NC |
766 | is("@keys", 'fake foo'); |
767 | is(exists $deref->{bar}, ''); | |
768 | is(exists $deref->{foo}, 1); | |
f5284f61 | 769 | # Code: |
7bf00c86 NC |
770 | is($deref->(5), 39); |
771 | is(&$deref(6), 40); | |
f5284f61 | 772 | sub xxx_goto { goto &$deref } |
7bf00c86 | 773 | is(xxx_goto(7), 41); |
f5284f61 IZ |
774 | my $srt = bless { c => sub {$b <=> $a} |
775 | }, 'deref'; | |
776 | *srt = \&$srt; | |
777 | my @sorted = sort srt 11, 2, 5, 1, 22; | |
7bf00c86 | 778 | is("@sorted", '22 11 5 2 1'); |
f5284f61 | 779 | # Scalar |
7bf00c86 | 780 | is($$deref, 123); |
c6e96bcb GS |
781 | # Code |
782 | @sorted = sort $srt 11, 2, 5, 1, 22; | |
7bf00c86 | 783 | is("@sorted", '22 11 5 2 1'); |
f5284f61 | 784 | # Array |
7bf00c86 NC |
785 | is("@$deref", '11 12 13'); |
786 | is($#$deref, '2'); | |
f5284f61 | 787 | my $l = @$deref; |
7bf00c86 NC |
788 | is($l, 3); |
789 | is($deref->[2], '13'); | |
f5284f61 | 790 | $l = pop @$deref; |
7bf00c86 | 791 | is($l, 13); |
f5284f61 | 792 | $l = 1; |
7bf00c86 | 793 | is($deref->[$l], '12'); |
f5284f61 IZ |
794 | # Repeated dereference |
795 | my $double = bless { h => $deref, | |
796 | }, 'deref'; | |
7bf00c86 | 797 | is($double->{foo}, 5); |
f5284f61 IZ |
798 | } |
799 | ||
800 | { | |
801 | package two_refs; | |
802 | use overload '%{}' => \&gethash, '@{}' => sub { ${shift()} }; | |
803 | sub new { | |
804 | my $p = shift; | |
805 | bless \ [@_], $p; | |
806 | } | |
807 | sub gethash { | |
808 | my %h; | |
809 | my $self = shift; | |
810 | tie %h, ref $self, $self; | |
811 | \%h; | |
812 | } | |
813 | ||
814 | sub TIEHASH { my $p = shift; bless \ shift, $p } | |
815 | my %fields; | |
816 | my $i = 0; | |
817 | $fields{$_} = $i++ foreach qw{zero one two three}; | |
818 | sub STORE { | |
819 | my $self = ${shift()}; | |
820 | my $key = $fields{shift()}; | |
821 | defined $key or die "Out of band access"; | |
822 | $$self->[$key] = shift; | |
823 | } | |
824 | sub FETCH { | |
825 | my $self = ${shift()}; | |
826 | my $key = $fields{shift()}; | |
827 | defined $key or die "Out of band access"; | |
828 | $$self->[$key]; | |
829 | } | |
830 | } | |
831 | ||
832 | my $bar = new two_refs 3,4,5,6; | |
833 | $bar->[2] = 11; | |
7bf00c86 | 834 | is($bar->{two}, 11); |
f5284f61 | 835 | $bar->{three} = 13; |
7bf00c86 | 836 | is($bar->[3], 13); |
f5284f61 IZ |
837 | |
838 | { | |
839 | package two_refs_o; | |
840 | @ISA = ('two_refs'); | |
841 | } | |
842 | ||
843 | $bar = new two_refs_o 3,4,5,6; | |
844 | $bar->[2] = 11; | |
7bf00c86 | 845 | is($bar->{two}, 11); |
f5284f61 | 846 | $bar->{three} = 13; |
7bf00c86 | 847 | is($bar->[3], 13); |
f5284f61 IZ |
848 | |
849 | { | |
850 | package two_refs1; | |
851 | use overload '%{}' => sub { ${shift()}->[1] }, | |
852 | '@{}' => sub { ${shift()}->[0] }; | |
853 | sub new { | |
854 | my $p = shift; | |
855 | my $a = [@_]; | |
856 | my %h; | |
857 | tie %h, $p, $a; | |
858 | bless \ [$a, \%h], $p; | |
859 | } | |
860 | sub gethash { | |
861 | my %h; | |
862 | my $self = shift; | |
863 | tie %h, ref $self, $self; | |
864 | \%h; | |
865 | } | |
866 | ||
867 | sub TIEHASH { my $p = shift; bless \ shift, $p } | |
868 | my %fields; | |
869 | my $i = 0; | |
870 | $fields{$_} = $i++ foreach qw{zero one two three}; | |
871 | sub STORE { | |
872 | my $a = ${shift()}; | |
873 | my $key = $fields{shift()}; | |
874 | defined $key or die "Out of band access"; | |
875 | $a->[$key] = shift; | |
876 | } | |
877 | sub FETCH { | |
878 | my $a = ${shift()}; | |
879 | my $key = $fields{shift()}; | |
880 | defined $key or die "Out of band access"; | |
881 | $a->[$key]; | |
882 | } | |
883 | } | |
884 | ||
885 | $bar = new two_refs_o 3,4,5,6; | |
886 | $bar->[2] = 11; | |
7bf00c86 | 887 | is($bar->{two}, 11); |
f5284f61 | 888 | $bar->{three} = 13; |
7bf00c86 | 889 | is($bar->[3], 13); |
f5284f61 IZ |
890 | |
891 | { | |
892 | package two_refs1_o; | |
893 | @ISA = ('two_refs1'); | |
894 | } | |
895 | ||
896 | $bar = new two_refs1_o 3,4,5,6; | |
897 | $bar->[2] = 11; | |
7bf00c86 | 898 | is($bar->{two}, 11); |
f5284f61 | 899 | $bar->{three} = 13; |
7bf00c86 | 900 | is($bar->[3], 13); |
f5284f61 | 901 | |
fe7ac86a IZ |
902 | { |
903 | package B; | |
904 | use overload bool => sub { ${+shift} }; | |
905 | } | |
906 | ||
907 | my $aaa; | |
908 | { my $bbbb = 0; $aaa = bless \$bbbb, B } | |
909 | ||
7bf00c86 | 910 | is !$aaa, 1; |
fe7ac86a IZ |
911 | |
912 | unless ($aaa) { | |
7bf00c86 | 913 | pass(); |
fe7ac86a | 914 | } else { |
7bf00c86 | 915 | fail(); |
fe7ac86a IZ |
916 | } |
917 | ||
1426bbf4 MG |
918 | # check that overload isn't done twice by join |
919 | { my $c = 0; | |
920 | package Join; | |
921 | use overload '""' => sub { $c++ }; | |
922 | my $x = join '', bless([]), 'pq', bless([]); | |
7bf00c86 | 923 | main::is $x, '0pq1'; |
1426bbf4 | 924 | }; |
fe7ac86a | 925 | |
4498a751 PM |
926 | # Test module-specific warning |
927 | { | |
928 | # check the Odd number of arguments for overload::constant warning | |
929 | my $a = "" ; | |
0a911d86 | 930 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
4498a751 | 931 | $x = eval ' overload::constant "integer" ; ' ; |
7bf00c86 | 932 | is($a, ""); |
4498a751 PM |
933 | use warnings 'overload' ; |
934 | $x = eval ' overload::constant "integer" ; ' ; | |
7bf00c86 | 935 | like($a, qr/^Odd number of arguments for overload::constant at/); |
4498a751 PM |
936 | } |
937 | ||
938 | { | |
939 | # check the `$_[0]' is not an overloadable type warning | |
940 | my $a = "" ; | |
0a911d86 | 941 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
4498a751 | 942 | $x = eval ' overload::constant "fred" => sub {} ; ' ; |
7bf00c86 | 943 | is($a, ""); |
4498a751 PM |
944 | use warnings 'overload' ; |
945 | $x = eval ' overload::constant "fred" => sub {} ; ' ; | |
7bf00c86 | 946 | like($a, qr/^`fred' is not an overloadable type at/); |
4498a751 PM |
947 | } |
948 | ||
949 | { | |
950 | # check the `$_[1]' is not a code reference warning | |
951 | my $a = "" ; | |
0a911d86 | 952 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
4498a751 | 953 | $x = eval ' overload::constant "integer" => 1; ' ; |
7bf00c86 | 954 | is($a, ""); |
4498a751 PM |
955 | use warnings 'overload' ; |
956 | $x = eval ' overload::constant "integer" => 1; ' ; | |
7bf00c86 | 957 | like($a, qr/^`1' is not a code reference at/); |
4498a751 PM |
958 | } |
959 | ||
78f67eb5 JH |
960 | { |
961 | my $c = 0; | |
962 | package ov_int1; | |
963 | use overload '""' => sub { 3+shift->[0] }, | |
964 | '0+' => sub { 10+shift->[0] }, | |
965 | 'int' => sub { 100+shift->[0] }; | |
966 | sub new {my $p = shift; bless [shift], $p} | |
967 | ||
968 | package ov_int2; | |
969 | use overload '""' => sub { 5+shift->[0] }, | |
970 | '0+' => sub { 30+shift->[0] }, | |
971 | 'int' => sub { 'ov_int1'->new(1000+shift->[0]) }; | |
972 | sub new {my $p = shift; bless [shift], $p} | |
973 | ||
974 | package noov_int; | |
975 | use overload '""' => sub { 2+shift->[0] }, | |
976 | '0+' => sub { 9+shift->[0] }; | |
977 | sub new {my $p = shift; bless [shift], $p} | |
978 | ||
979 | package main; | |
980 | ||
981 | my $x = new noov_int 11; | |
982 | my $int_x = int $x; | |
7bf00c86 | 983 | main::is("$int_x", 20); |
78f67eb5 JH |
984 | $x = new ov_int1 31; |
985 | $int_x = int $x; | |
7bf00c86 | 986 | main::is("$int_x", 131); |
78f67eb5 JH |
987 | $x = new ov_int2 51; |
988 | $int_x = int $x; | |
7bf00c86 | 989 | main::is("$int_x", 1054); |
78f67eb5 JH |
990 | } |
991 | ||
b0bf6df7 | 992 | # make sure that we don't infinitely recurse |
1554e226 DC |
993 | { |
994 | my $c = 0; | |
995 | package Recurse; | |
996 | use overload '""' => sub { shift }, | |
997 | '0+' => sub { shift }, | |
998 | 'bool' => sub { shift }, | |
999 | fallback => 1; | |
1000 | my $x = bless([]); | |
7bf00c86 NC |
1001 | # For some reason beyond me these have to be oks rather than likes. |
1002 | main::ok("$x" =~ /Recurse=ARRAY/); | |
1003 | main::ok($x); | |
1004 | main::ok($x+0 =~ qr/Recurse=ARRAY/); | |
11e3e2e4 | 1005 | } |
78f67eb5 | 1006 | |
1dc13c17 SC |
1007 | # BugID 20010422.003 |
1008 | package Foo; | |
1009 | ||
1010 | use overload | |
1011 | 'bool' => sub { return !$_[0]->is_zero() || undef; } | |
1012 | ; | |
1013 | ||
1014 | sub is_zero | |
1015 | { | |
1016 | my $self = shift; | |
1017 | return $self->{var} == 0; | |
1018 | } | |
1019 | ||
1020 | sub new | |
1021 | { | |
1022 | my $class = shift; | |
1023 | my $self = {}; | |
1024 | $self->{var} = shift; | |
1025 | bless $self,$class; | |
1026 | } | |
1027 | ||
1028 | package main; | |
1029 | ||
1030 | use strict; | |
1031 | ||
1032 | my $r = Foo->new(8); | |
1033 | $r = Foo->new(0); | |
78f67eb5 | 1034 | |
11e3e2e4 | 1035 | is(($r || 0), 0); |
1554e226 | 1036 | |
6050d10e JP |
1037 | package utf8_o; |
1038 | ||
1039 | use overload | |
1040 | '""' => sub { return $_[0]->{var}; } | |
1041 | ; | |
1042 | ||
1043 | sub new | |
1044 | { | |
1045 | my $class = shift; | |
1046 | my $self = {}; | |
1047 | $self->{var} = shift; | |
1048 | bless $self,$class; | |
1049 | } | |
1050 | ||
1051 | package main; | |
1052 | ||
1053 | ||
1054 | my $utfvar = new utf8_o 200.2.1; | |
11e3e2e4 NC |
1055 | is("$utfvar", 200.2.1); # 223 - stringify |
1056 | is("a$utfvar", "a".200.2.1); # 224 - overload via sv_2pv_flags | |
6050d10e | 1057 | |
446eaa42 | 1058 | # 225..227 -- more %{} tests. Hangs in 5.6.0, okay in later releases. |
41cb1005 JH |
1059 | # Basically this example implements strong encapsulation: if Hderef::import() |
1060 | # were to eval the overload code in the caller's namespace, the privatisation | |
1061 | # would be quite transparent. | |
1062 | package Hderef; | |
1063 | use overload '%{}' => sub { (caller(0))[0] eq 'Foo' ? $_[0] : die "zap" }; | |
1064 | package Foo; | |
1065 | @Foo::ISA = 'Hderef'; | |
1066 | sub new { bless {}, shift } | |
1067 | sub xet { @_ == 2 ? $_[0]->{$_[1]} : | |
1068 | @_ == 3 ? ($_[0]->{$_[1]} = $_[2]) : undef } | |
1069 | package main; | |
1070 | my $a = Foo->new; | |
1071 | $a->xet('b', 42); | |
11e3e2e4 NC |
1072 | is ($a->xet('b'), 42); |
1073 | ok (!defined eval { $a->{b} }); | |
1074 | like ($@, qr/zap/); | |
41cb1005 | 1075 | |
29ddfe35 RD |
1076 | { |
1077 | package t229; | |
1078 | use overload '=' => sub { 42 }, | |
1079 | '++' => sub { my $x = ${$_[0]}; $_[0] }; | |
1080 | sub new { my $x = 42; bless \$x } | |
1081 | ||
1082 | my $warn; | |
1083 | { | |
1084 | local $SIG{__WARN__} = sub { $warn++ }; | |
1085 | my $x = t229->new; | |
1086 | my $y = $x; | |
1087 | eval { $y++ }; | |
1088 | } | |
11e3e2e4 | 1089 | main::ok (!$warn); |
61f33854 RGS |
1090 | } |
1091 | ||
1092 | { | |
1093 | my ($int, $out1, $out2); | |
1094 | { | |
1095 | BEGIN { $int = 0; overload::constant 'integer' => sub {$int++; 17}; } | |
1096 | $out1 = 0; | |
1097 | $out2 = 1; | |
1098 | } | |
11e3e2e4 NC |
1099 | is($int, 2, "#24313"); # 230 |
1100 | is($out1, 17, "#24313"); # 231 | |
1101 | is($out2, 17, "#24313"); # 232 | |
29ddfe35 RD |
1102 | } |
1103 | ||
0bdaccee NC |
1104 | { |
1105 | package Numify; | |
1106 | use overload (qw(0+ numify fallback 1)); | |
1107 | ||
1108 | sub new { | |
1109 | my $val = $_[1]; | |
1110 | bless \$val, $_[0]; | |
1111 | } | |
1112 | ||
1113 | sub numify { ${$_[0]} } | |
1114 | } | |
1115 | ||
d411a6a9 RD |
1116 | { |
1117 | package perl31793; | |
1b1d102f RD |
1118 | use overload cmp => sub { 0 }; |
1119 | package perl31793_fb; | |
76c43448 | 1120 | use overload cmp => sub { 0 }, fallback => 1; |
d411a6a9 RD |
1121 | package main; |
1122 | my $o = bless [], 'perl31793'; | |
1b1d102f | 1123 | my $of = bless [], 'perl31793_fb'; |
d411a6a9 | 1124 | my $no = bless [], 'no_overload'; |
11e3e2e4 NC |
1125 | like(overload::StrVal(\"scalar"), qr/^SCALAR\(0x[0-9a-f]+\)$/); |
1126 | like(overload::StrVal([]), qr/^ARRAY\(0x[0-9a-f]+\)$/); | |
1127 | like(overload::StrVal({}), qr/^HASH\(0x[0-9a-f]+\)$/); | |
1128 | like(overload::StrVal(sub{1}), qr/^CODE\(0x[0-9a-f]+\)$/); | |
1129 | like(overload::StrVal(\*GLOB), qr/^GLOB\(0x[0-9a-f]+\)$/); | |
1130 | like(overload::StrVal(\$o), qr/^REF\(0x[0-9a-f]+\)$/); | |
0fc92fc6 | 1131 | like(overload::StrVal(qr/a/), qr/^Regexp=REGEXP\(0x[0-9a-f]+\)$/); |
11e3e2e4 NC |
1132 | like(overload::StrVal($o), qr/^perl31793=ARRAY\(0x[0-9a-f]+\)$/); |
1133 | like(overload::StrVal($of), qr/^perl31793_fb=ARRAY\(0x[0-9a-f]+\)$/); | |
1134 | like(overload::StrVal($no), qr/^no_overload=ARRAY\(0x[0-9a-f]+\)$/); | |
d411a6a9 RD |
1135 | } |
1136 | ||
b0bf6df7 | 1137 | # These are all check that overloaded values rather than reference addresses |
0bdaccee NC |
1138 | # are what is getting tested. |
1139 | my ($two, $one, $un, $deux) = map {new Numify $_} 2, 1, 1, 2; | |
1140 | my ($ein, $zwei) = (1, 2); | |
1141 | ||
1142 | my %map = (one => 1, un => 1, ein => 1, deux => 2, two => 2, zwei => 2); | |
1143 | foreach my $op (qw(<=> == != < <= > >=)) { | |
1144 | foreach my $l (keys %map) { | |
1145 | foreach my $r (keys %map) { | |
1146 | my $ocode = "\$$l $op \$$r"; | |
1147 | my $rcode = "$map{$l} $op $map{$r}"; | |
1148 | ||
1149 | my $got = eval $ocode; | |
1150 | die if $@; | |
1151 | my $expect = eval $rcode; | |
1152 | die if $@; | |
11e3e2e4 | 1153 | is ($got, $expect, $ocode) or print "# $rcode\n"; |
0bdaccee NC |
1154 | } |
1155 | } | |
1156 | } | |
131b3ad0 DM |
1157 | { |
1158 | # check that overloading works in regexes | |
1159 | { | |
1160 | package Foo493; | |
1161 | use overload | |
1162 | '""' => sub { "^$_[0][0]\$" }, | |
1163 | '.' => sub { | |
1164 | bless [ | |
1165 | $_[2] | |
1166 | ? (ref $_[1] ? $_[1][0] : $_[1]) . ':' .$_[0][0] | |
1167 | : $_[0][0] . ':' . (ref $_[1] ? $_[1][0] : $_[1]) | |
1168 | ], 'Foo493' | |
1169 | }; | |
1170 | } | |
1171 | ||
1172 | my $a = bless [ "a" ], 'Foo493'; | |
11e3e2e4 NC |
1173 | like('a', qr/$a/); |
1174 | like('x:a', qr/x$a/); | |
1175 | like('x:a:=', qr/x$a=$/); | |
1176 | like('x:a:a:=', qr/x$a$a=$/); | |
131b3ad0 DM |
1177 | |
1178 | } | |
1179 | ||
705c898c | 1180 | { |
d4b87e75 BM |
1181 | { |
1182 | package QRonly; | |
1183 | use overload qr => sub { qr/x/ }, fallback => 1; | |
1184 | } | |
1185 | { | |
1186 | my $x = bless [], "QRonly"; | |
1187 | ||
1188 | # like tries to be too clever, and decides that $x-stringified | |
1189 | # doesn't look like a regex | |
1190 | ok("x" =~ $x, "qr-only matches"); | |
e4eea578 | 1191 | ok("y" !~ $x, "qr-only doesn't match what it shouldn't"); |
d4b87e75 | 1192 | ok("xx" =~ /x$x/, "qr-only matches with concat"); |
e4eea578 | 1193 | like("$x", qr/^QRonly=ARRAY/, "qr-only doesn't have string overload"); |
d4b87e75 BM |
1194 | |
1195 | my $qr = bless qr/y/, "QRonly"; | |
1196 | ok("x" =~ $qr, "qr with qr-overload uses overload"); | |
e4eea578 | 1197 | ok("y" !~ $qr, "qr with qr-overload uses overload"); |
d4b87e75 BM |
1198 | is("$qr", "".qr/y/, "qr with qr-overload stringify"); |
1199 | ||
1200 | my $rx = $$qr; | |
1201 | ok("y" =~ $rx, "bare rx with qr-overload doesn't overload match"); | |
e4eea578 | 1202 | ok("x" !~ $rx, "bare rx with qr-overload doesn't overload match"); |
d4b87e75 BM |
1203 | is("$rx", "".qr/y/, "bare rx with qr-overload stringify"); |
1204 | } | |
1205 | { | |
1206 | package QRandSTR; | |
1207 | use overload qr => sub { qr/x/ }, q/""/ => sub { "y" }; | |
1208 | } | |
1209 | { | |
1210 | my $x = bless [], "QRandSTR"; | |
1211 | ok("x" =~ $x, "qr+str uses qr for match"); | |
e4eea578 | 1212 | ok("y" !~ $x, "qr+str uses qr for match"); |
d4b87e75 BM |
1213 | ok("xx" =~ /x$x/, "qr+str uses qr for match with concat"); |
1214 | is("$x", "y", "qr+str uses str for stringify"); | |
1215 | ||
1216 | my $qr = bless qr/z/, "QRandSTR"; | |
1217 | is("$qr", "y", "qr with qr+str uses str for stringify"); | |
1218 | ok("xx" =~ /x$x/, "qr with qr+str uses qr for match"); | |
1219 | ||
1220 | my $rx = $$qr; | |
1221 | ok("z" =~ $rx, "bare rx with qr+str doesn't overload match"); | |
1222 | is("$rx", "".qr/z/, "bare rx with qr+str doesn't overload stringify"); | |
1223 | } | |
1224 | { | |
1225 | package QRany; | |
1226 | use overload qr => sub { $_[0]->(@_) }; | |
1227 | ||
1228 | package QRself; | |
1229 | use overload qr => sub { $_[0] }; | |
1230 | } | |
1231 | { | |
1232 | my $rx = bless sub { ${ qr/x/ } }, "QRany"; | |
e4eea578 RGS |
1233 | ok("x" =~ $rx, "qr overload accepts a bare rx"); |
1234 | ok("y" !~ $rx, "qr overload accepts a bare rx"); | |
d4b87e75 BM |
1235 | |
1236 | my $str = bless sub { "x" }, "QRany"; | |
1237 | ok(!eval { "x" =~ $str }, "qr overload doesn't accept a string"); | |
e4eea578 | 1238 | like($@, qr/^Overloaded qr did not return a REGEXP/, "correct error"); |
d4b87e75 BM |
1239 | |
1240 | my $oqr = bless qr/z/, "QRandSTR"; | |
1241 | my $oqro = bless sub { $oqr }, "QRany"; | |
e4eea578 | 1242 | ok("z" =~ $oqro, "qr overload doesn't recurse"); |
d4b87e75 BM |
1243 | |
1244 | my $qrs = bless qr/z/, "QRself"; | |
e4eea578 | 1245 | ok("z" =~ $qrs, "qr overload can return self"); |
d4b87e75 BM |
1246 | } |
1247 | { | |
1248 | package STRonly; | |
1249 | use overload q/""/ => sub { "x" }; | |
1250 | ||
1251 | package STRonlyFB; | |
1252 | use overload q/""/ => sub { "x" }, fallback => 1; | |
1253 | } | |
1254 | { | |
1255 | my $fb = bless [], "STRonlyFB"; | |
e4eea578 RGS |
1256 | ok("x" =~ $fb, "qr falls back to \"\""); |
1257 | ok("y" !~ $fb, "qr falls back to \"\""); | |
d4b87e75 BM |
1258 | |
1259 | my $nofb = bless [], "STRonly"; | |
e4eea578 RGS |
1260 | ok("x" =~ $nofb, "qr falls back even without fallback"); |
1261 | ok("y" !~ $nofb, "qr falls back even without fallback"); | |
d4b87e75 BM |
1262 | } |
1263 | } | |
1264 | ||
1265 | { | |
705c898c RH |
1266 | my $twenty_three = 23; |
1267 | # Check that constant overloading propagates into evals | |
1268 | BEGIN { overload::constant integer => sub { 23 } } | |
11e3e2e4 | 1269 | is(eval "17", $twenty_three); |
705c898c | 1270 | } |
dd2eae66 NC |
1271 | |
1272 | { | |
1273 | package Sklorsh; | |
1274 | use overload | |
1275 | bool => sub { shift->is_cool }; | |
1276 | ||
1277 | sub is_cool { | |
1278 | $_[0]->{name} eq 'cool'; | |
1279 | } | |
1280 | ||
1281 | sub delete { | |
1282 | undef %{$_[0]}; | |
1283 | bless $_[0], 'Brap'; | |
1284 | return 1; | |
1285 | } | |
1286 | ||
1287 | sub delete_with_self { | |
1288 | my $self = shift; | |
1289 | undef %$self; | |
1290 | bless $self, 'Brap'; | |
1291 | return 1; | |
1292 | } | |
1293 | ||
1294 | package Brap; | |
1295 | ||
1296 | 1; | |
1297 | ||
1298 | package main; | |
1299 | ||
1300 | my $obj; | |
1301 | $obj = bless {name => 'cool'}, 'Sklorsh'; | |
1302 | $obj->delete; | |
b0bf6df7 | 1303 | ok(eval {if ($obj) {1}; 1}, $@ || 'reblessed into nonexistent namespace'); |
dd2eae66 NC |
1304 | |
1305 | $obj = bless {name => 'cool'}, 'Sklorsh'; | |
1306 | $obj->delete_with_self; | |
1307 | ok (eval {if ($obj) {1}; 1}, $@); | |
1308 | ||
1309 | my $a = $b = {name => 'hot'}; | |
1310 | bless $b, 'Sklorsh'; | |
1311 | is(ref $a, 'Sklorsh'); | |
1312 | is(ref $b, 'Sklorsh'); | |
1313 | ok(!$b, "Expect overloaded boolean"); | |
1314 | ok(!$a, "Expect overloaded boolean"); | |
1315 | } | |
edbe35ea VP |
1316 | |
1317 | { | |
1318 | package Flrbbbbb; | |
1319 | use overload | |
1320 | bool => sub { shift->{truth} eq 'yes' }, | |
1321 | '0+' => sub { shift->{truth} eq 'yes' ? '1' : '0' }, | |
1322 | '!' => sub { shift->{truth} eq 'no' }, | |
1323 | fallback => 1; | |
1324 | ||
1325 | sub new { my $class = shift; bless { truth => shift }, $class } | |
1326 | ||
1327 | package main; | |
1328 | ||
1329 | my $yes = Flrbbbbb->new('yes'); | |
1330 | my $x; | |
1331 | $x = 1 if $yes; is($x, 1); | |
1332 | $x = 2 unless $yes; is($x, 1); | |
1333 | $x = 3 if !$yes; is($x, 1); | |
1334 | $x = 4 unless !$yes; is($x, 4); | |
1335 | ||
1336 | my $no = Flrbbbbb->new('no'); | |
1337 | $x = 0; | |
1338 | $x = 1 if $no; is($x, 0); | |
1339 | $x = 2 unless $no; is($x, 2); | |
1340 | $x = 3 if !$no; is($x, 3); | |
1341 | $x = 4 unless !$no; is($x, 3); | |
1342 | ||
1343 | $x = 0; | |
1344 | $x = 1 if !$no && $yes; is($x, 1); | |
1345 | $x = 2 unless !$no && $yes; is($x, 1); | |
1346 | $x = 3 if $no || !$yes; is($x, 1); | |
1347 | $x = 4 unless $no || !$yes; is($x, 4); | |
1348 | ||
1349 | $x = 0; | |
1350 | $x = 1 if !$no || !$yes; is($x, 1); | |
1351 | $x = 2 unless !$no || !$yes; is($x, 1); | |
1352 | $x = 3 if !$no && !$yes; is($x, 1); | |
1353 | $x = 4 unless !$no && !$yes; is($x, 4); | |
1354 | } | |
1355 | ||
ea940d6b | 1356 | { |
2372186a NC |
1357 | use Scalar::Util 'weaken'; |
1358 | ||
1359 | package Shklitza; | |
1360 | use overload '""' => sub {"CLiK KLAK"}; | |
1361 | ||
1362 | package Ksshfwoom; | |
2372186a NC |
1363 | |
1364 | package main; | |
1365 | ||
1366 | my ($obj, $ref); | |
1367 | $obj = bless do {my $a; \$a}, 'Shklitza'; | |
1368 | $ref = $obj; | |
1369 | ||
ea940d6b CBW |
1370 | is ("$obj", "CLiK KLAK"); |
1371 | is ("$ref", "CLiK KLAK"); | |
2372186a NC |
1372 | |
1373 | weaken $ref; | |
ea940d6b | 1374 | is ("$ref", "CLiK KLAK"); |
2372186a NC |
1375 | |
1376 | bless $obj, 'Ksshfwoom'; | |
1377 | ||
cd75d542 NC |
1378 | like ($obj, qr/^Ksshfwoom=/); |
1379 | like ($ref, qr/^Ksshfwoom=/); | |
2372186a NC |
1380 | |
1381 | undef $obj; | |
1382 | is ($ref, undef); | |
1383 | } | |
6dd85743 AF |
1384 | |
1385 | { | |
1386 | package bit; | |
1387 | # bit operations have overloadable assignment variants too | |
1388 | ||
1389 | sub new { bless \$_[1], $_[0] } | |
1390 | ||
1391 | use overload | |
1392 | "&=" => sub { bit->new($_[0]->val . ' & ' . $_[1]->val) }, | |
1393 | "^=" => sub { bit->new($_[0]->val . ' ^ ' . $_[1]->val) }, | |
1394 | "|" => sub { bit->new($_[0]->val . ' | ' . $_[1]->val) }, # |= by fallback | |
1395 | ; | |
1396 | ||
1397 | sub val { ${$_[0]} } | |
1398 | ||
1399 | package main; | |
1400 | ||
1401 | my $a = bit->new(my $va = 'a'); | |
1402 | my $b = bit->new(my $vb = 'b'); | |
1403 | ||
1404 | $a &= $b; | |
1405 | is($a->val, 'a & b', "overloaded &= works"); | |
1406 | ||
1407 | my $c = bit->new(my $vc = 'c'); | |
1408 | ||
1409 | $b ^= $c; | |
1410 | is($b->val, 'b ^ c', "overloaded ^= works"); | |
1411 | ||
1412 | my $d = bit->new(my $vd = 'd'); | |
1413 | ||
1414 | $c |= $d; | |
1415 | is($c->val, 'c | d', "overloaded |= (by fallback) works"); | |
1416 | } | |
d11ee47c RD |
1417 | |
1418 | { | |
2ab54efd | 1419 | # comparison operators with nomethod (bug 41546) |
d11ee47c RD |
1420 | my $warning = ""; |
1421 | my $method; | |
1422 | ||
1423 | package nomethod_false; | |
1424 | use overload nomethod => sub { $method = 'nomethod'; 0 }; | |
1425 | ||
1426 | package nomethod_true; | |
1427 | use overload nomethod => sub { $method= 'nomethod'; 'true' }; | |
1428 | ||
1429 | package main; | |
1430 | local $^W = 1; | |
1431 | local $SIG{__WARN__} = sub { $warning = $_[0] }; | |
1432 | ||
1433 | my $f = bless [], 'nomethod_false'; | |
1434 | ($warning, $method) = ("", ""); | |
1435 | is($f eq 'whatever', 0, 'nomethod makes eq return 0'); | |
1436 | is($method, 'nomethod'); | |
1437 | ||
1438 | my $t = bless [], 'nomethod_true'; | |
1439 | ($warning, $method) = ("", ""); | |
1440 | is($t eq 'whatever', 'true', 'nomethod makes eq return "true"'); | |
1441 | is($method, 'nomethod'); | |
1442 | is($warning, "", 'nomethod eq need not return number'); | |
1443 | ||
1444 | eval q{ | |
1445 | package nomethod_false; | |
1446 | use overload cmp => sub { $method = 'cmp'; 0 }; | |
1447 | }; | |
1448 | $f = bless [], 'nomethod_false'; | |
1449 | ($warning, $method) = ("", ""); | |
1450 | ok($f eq 'whatever', 'eq falls back to cmp (nomethod not called)'); | |
1451 | is($method, 'cmp'); | |
1452 | ||
1453 | eval q{ | |
1454 | package nomethod_true; | |
1455 | use overload cmp => sub { $method = 'cmp'; 'true' }; | |
1456 | }; | |
1457 | $t = bless [], 'nomethod_true'; | |
1458 | ($warning, $method) = ("", ""); | |
1459 | ok($t eq 'whatever', 'eq falls back to cmp (nomethod not called)'); | |
1460 | is($method, 'cmp'); | |
1461 | like($warning, qr/isn't numeric/, 'cmp should return number'); | |
1462 | ||
1463 | } | |
2c615c57 NC |
1464 | |
1465 | { | |
2ab54efd MB |
1466 | # nomethod called for '!' after attempted fallback |
1467 | my $nomethod_called = 0; | |
1468 | ||
1469 | package nomethod_not; | |
1470 | use overload nomethod => sub { $nomethod_called = 'yes'; }; | |
1471 | ||
1472 | package main; | |
1473 | my $o = bless [], 'nomethod_not'; | |
1474 | my $res = ! $o; | |
1475 | ||
1476 | is($nomethod_called, 'yes', "nomethod() is called for '!'"); | |
1477 | is($res, 'yes', "nomethod(..., '!') return value propagates"); | |
1478 | } | |
1479 | ||
1480 | { | |
2c615c57 | 1481 | # Subtle bug pre 5.10, as a side effect of the overloading flag being |
b0bf6df7 | 1482 | # stored on the reference rather than the referent. Despite the fact that |
2c615c57 | 1483 | # objects can only be accessed via references (even internally), the |
b0bf6df7 | 1484 | # referent actually knows that it's blessed, not the references. So taking |
2c615c57 NC |
1485 | # a new, unrelated, reference to it gives an object. However, the |
1486 | # overloading-or-not flag was on the reference prior to 5.10, and taking | |
1487 | # a new reference didn't (use to) copy it. | |
1488 | ||
1489 | package kayo; | |
1490 | ||
1491 | use overload '""' => sub {${$_[0]}}; | |
1492 | ||
1493 | sub Pie { | |
1494 | return "$_[0], $_[1]"; | |
1495 | } | |
1496 | ||
1497 | package main; | |
1498 | ||
1499 | my $class = 'kayo'; | |
1500 | my $string = 'bam'; | |
1501 | my $crunch_eth = bless \$string, $class; | |
1502 | ||
1503 | is("$crunch_eth", $string); | |
1504 | is ($crunch_eth->Pie("Meat"), "$string, Meat"); | |
1505 | ||
1506 | my $wham_eth = \$string; | |
1507 | ||
1508 | is("$wham_eth", $string, | |
1509 | 'This reference did not have overloading in 5.8.8 and earlier'); | |
1510 | is ($crunch_eth->Pie("Apple"), "$string, Apple"); | |
1511 | ||
1512 | my $class = ref $wham_eth; | |
1513 | $class =~ s/=.*//; | |
1514 | ||
1515 | # Bless it back into its own class! | |
1516 | bless $wham_eth, $class; | |
1517 | ||
1518 | is("$wham_eth", $string); | |
1519 | is ($crunch_eth->Pie("Blackbird"), "$string, Blackbird"); | |
1520 | } | |
c781a409 RD |
1521 | |
1522 | { | |
1523 | package numify_int; | |
1524 | use overload "0+" => sub { $_[0][0] += 1; 42 }; | |
1525 | package numify_self; | |
1526 | use overload "0+" => sub { $_[0][0]++; $_[0] }; | |
1527 | package numify_other; | |
1528 | use overload "0+" => sub { $_[0][0]++; $_[0][1] = bless [], 'numify_int' }; | |
e28bb1d5 | 1529 | package numify_by_fallback; |
800401ee | 1530 | use overload fallback => 1; |
c781a409 RD |
1531 | |
1532 | package main; | |
1533 | my $o = bless [], 'numify_int'; | |
1534 | is(int($o), 42, 'numifies to integer'); | |
1535 | is($o->[0], 1, 'int() numifies only once'); | |
1536 | ||
1537 | my $aref = []; | |
93521010 | 1538 | my $num_val = int($aref); |
c781a409 RD |
1539 | my $r = bless $aref, 'numify_self'; |
1540 | is(int($r), $num_val, 'numifies to self'); | |
1541 | is($r->[0], 1, 'int() numifies once when returning self'); | |
1542 | ||
1543 | my $s = bless [], 'numify_other'; | |
1544 | is(int($s), 42, 'numifies to numification of other object'); | |
1545 | is($s->[0], 1, 'int() numifies once when returning other object'); | |
1546 | is($s->[1][0], 1, 'returned object numifies too'); | |
e28bb1d5 RD |
1547 | |
1548 | my $m = bless $aref, 'numify_by_fallback'; | |
1549 | is(int($m), $num_val, 'numifies to usual reference value'); | |
800401ee JH |
1550 | is(abs($m), $num_val, 'numifies to usual reference value'); |
1551 | is(-$m, -$num_val, 'numifies to usual reference value'); | |
1552 | is(0+$m, $num_val, 'numifies to usual reference value'); | |
1553 | is($m+0, $num_val, 'numifies to usual reference value'); | |
1554 | is($m+$m, 2*$num_val, 'numifies to usual reference value'); | |
1555 | is(0-$m, -$num_val, 'numifies to usual reference value'); | |
1556 | is(1*$m, $num_val, 'numifies to usual reference value'); | |
9fa8ecf2 | 1557 | is(int($m/1), $num_val, 'numifies to usual reference value'); |
800401ee JH |
1558 | is($m%100, $num_val%100, 'numifies to usual reference value'); |
1559 | is($m**1, $num_val, 'numifies to usual reference value'); | |
1560 | ||
1561 | is(abs($aref), $num_val, 'abs() of ref'); | |
1562 | is(-$aref, -$num_val, 'negative of ref'); | |
1563 | is(0+$aref, $num_val, 'ref addition'); | |
1564 | is($aref+0, $num_val, 'ref addition'); | |
1565 | is($aref+$aref, 2*$num_val, 'ref addition'); | |
1566 | is(0-$aref, -$num_val, 'subtraction of ref'); | |
1567 | is(1*$aref, $num_val, 'multiplicaton of ref'); | |
9fa8ecf2 | 1568 | is(int($aref/1), $num_val, 'division of ref'); |
800401ee JH |
1569 | is($aref%100, $num_val%100, 'modulo of ref'); |
1570 | is($aref**1, $num_val, 'exponentiation of ref'); | |
c781a409 | 1571 | } |
800401ee | 1572 | |
49c95d58 RD |
1573 | { |
1574 | package CopyConstructorFallback; | |
1575 | use overload | |
1576 | '++' => sub { "$_[0]"; $_[0] }, | |
1577 | fallback => 1; | |
1578 | sub new { bless {} => shift } | |
1579 | ||
1580 | package main; | |
1581 | ||
1582 | my $o = CopyConstructorFallback->new; | |
1583 | my $x = $o++; # would segfault | |
1584 | my $y = ++$o; | |
1585 | is($x, $o, "copy constructor falls back to assignment (postinc)"); | |
1586 | is($y, $o, "copy constructor falls back to assignment (preinc)"); | |
1587 | } | |
1588 | ||
6f1401dc DM |
1589 | # only scalar 'x' should currently overload |
1590 | ||
1591 | { | |
1592 | package REPEAT; | |
1593 | ||
1594 | my ($x,$n, $nm); | |
1595 | ||
1596 | use overload | |
1597 | 'x' => sub { $x++; 1 }, | |
1598 | '0+' => sub { $n++; 1 }, | |
1599 | 'nomethod' => sub { $nm++; 1 }, | |
1600 | 'fallback' => 0, | |
1601 | ; | |
1602 | ||
1603 | my $s = bless {}; | |
1604 | ||
1605 | package main; | |
1606 | ||
1607 | my @a; | |
1608 | my $count = 3; | |
1609 | ||
1610 | ($x,$n,$nm) = (0,0,0); | |
1611 | @a = ((1,2,$s) x $count); | |
1612 | is("$x-$n-$nm", "0-0-0", 'repeat 1'); | |
1613 | ||
1614 | ($x,$n,$nm) = (0,0,0); | |
1615 | @a = ((1,$s,3) x $count); | |
1616 | is("$x-$n-$nm", "0-0-0", 'repeat 2'); | |
1617 | ||
1618 | ($x,$n,$nm) = (0,0,0); | |
1619 | @a = ((1,2,3) x $s); | |
1620 | is("$x-$n-$nm", "0-1-0", 'repeat 3'); | |
1621 | } | |
1622 | ||
1623 | ||
1624 | ||
1625 | # RT #57012: magic items need to have mg_get() called before testing for | |
1626 | # overload. Lack of this means that overloaded values returned by eg a | |
1627 | # tied array didn't call overload methods. | |
1628 | # We test here both a tied array and scalar, since the implementation of | |
1629 | # tied arrays (and hashes) is such that in rvalue context, mg_get is | |
1630 | # called prior to executing the op, while it isn't for a tied scalar. | |
a02ec77a DM |
1631 | # We also check that return values are correctly tainted. |
1632 | # We try against two overload packages; one has all expected methods, the | |
1633 | # other uses only fallback methods. | |
6f1401dc DM |
1634 | |
1635 | { | |
1636 | ||
a02ec77a DM |
1637 | # @tests holds a list of test cases. Each elem is an array ref with |
1638 | # the following entries: | |
1639 | # | |
1640 | # * the value that the overload method should return | |
1641 | # | |
1642 | # * the expression to be evaled. %s is replaced with the | |
1643 | # variable being tested ($ta[0], $ts, or $plain) | |
1644 | # | |
1645 | # * a string listing what functions we expect to be called. | |
1646 | # Each method appends its name in parentheses, so "(=)(+)" means | |
1647 | # we expect the copy constructor and then the add method to be | |
1648 | # called. | |
1649 | # | |
1650 | # * like above, but what should be called for the fallback-only test | |
1651 | # (in this case, nomethod() identifies itself as "(NM:*)" where * | |
1652 | # is the op). If this value is undef, fallback tests are skipped. | |
1653 | # | |
1654 | # * An array ref of expected counts of calls to FETCH/STORE. | |
1655 | # The first three values are: | |
1656 | # 1. the expected number of FETCHs for a tied array | |
1657 | # 2. the expected number of FETCHs for a tied scalar | |
1658 | # 3. the expected number of STOREs | |
1659 | # If there are a further three elements present, then | |
1660 | # these represent the expected counts for the fallback | |
1661 | # version of the tests. If absent, they are assumed to | |
1662 | # be the same as for the full method test | |
1663 | # | |
1664 | # * Under the taint version of the tests, whether we expect | |
1665 | # the result to be tainted (for example comparison ops | |
1666 | # like '==' don't return a tainted value, even if their | |
1667 | # args are. | |
1668 | my @tests; | |
1669 | ||
6f1401dc DM |
1670 | my %subs; |
1671 | my $funcs; | |
1672 | my $use_int; | |
1673 | ||
1674 | BEGIN { | |
1675 | # A note on what methods to expect to be called, and | |
1676 | # how many times FETCH/STORE is called: | |
1677 | # | |
1678 | # Mutating ops (+=, ++ etc) trigger a copy ('='), since | |
7f1e438c | 1679 | # the code can't distinguish between something that's been copied: |
6f1401dc DM |
1680 | # $a = foo->new(0); $b = $a; refcnt($$b) == 2 |
1681 | # and overloaded objects stored in ties which will have extra | |
1682 | # refcounts due to the tied_obj magic and entries on the tmps | |
1683 | # stack when returning from FETCH etc. So we always copy. | |
1684 | ||
1685 | # This accounts for a '=', and an extra STORE. | |
1686 | # We also have a FETCH returning the final value from the eval, | |
1687 | # plus a FETCH in the overload subs themselves: ($_[0][0]) | |
7f1e438c | 1688 | # triggers one. However, tied aggregates have a mechanism to prevent |
6f1401dc DM |
1689 | # multiple fetches between STOREs, which means that the tied |
1690 | # hash skips doing a FETCH during '='. | |
1691 | ||
a02ec77a DM |
1692 | for (qw(+ - * / % ** << >> & | ^)) { |
1693 | my $op = $_; | |
1694 | $op = '%%' if $op eq '%'; | |
1695 | my $e = "%s $op= 3"; | |
6f1401dc DM |
1696 | $subs{"$_="} = $e; |
1697 | # ARRAY FETCH: initial, sub+=, eval-return, | |
1698 | # SCALAR FETCH: initial, sub=, sub+=, eval-return, | |
1699 | # STORE: copy, mutator | |
a02ec77a DM |
1700 | push @tests, [ 18, $e, "(=)($_=)", "(=)(NM:$_=)", [ 3, 4, 2 ], 1 ]; |
1701 | ||
1702 | $subs{$_} = | |
1703 | "do { my \$arg = %s; \$_[2] ? (3 $op \$arg) : (\$arg $op 3) }"; | |
6f1401dc DM |
1704 | # ARRAY FETCH: initial |
1705 | # SCALAR FETCH: initial eval-return, | |
a02ec77a DM |
1706 | push @tests, [ 18, "%s $op 3", "($_)", "(NM:$_)", [ 1, 2, 0 ], 1 ]; |
1707 | push @tests, [ 18, "3 $op %s", "($_)", "(NM:$_)", [ 1, 2, 0 ], 1 ]; | |
6f1401dc | 1708 | } |
a02ec77a DM |
1709 | |
1710 | # these use string fallback rather than nomethod | |
1711 | for (qw(x .)) { | |
1712 | my $op = $_; | |
1713 | my $e = "%s $op= 3"; | |
1714 | $subs{"$_="} = $e; | |
1715 | # For normal case: | |
1716 | # ARRAY FETCH: initial, sub+=, eval-return, | |
1717 | # SCALAR FETCH: initial, sub=, sub+=, eval-return, | |
1718 | # STORE: copy, mutator | |
1719 | # for fallback, we just stringify, so eval-return and copy skipped | |
c5aa2872 DM |
1720 | |
1721 | push @tests, [ 18, $e, "(=)($_=)", '("")', | |
1722 | [ 3, 4, 2, 2, 3, 1 ], 1 ]; | |
a02ec77a DM |
1723 | |
1724 | $subs{$_} = | |
1725 | "do { my \$arg = %s; \$_[2] ? (3 $op \$arg) : (\$arg $op 3) }"; | |
1726 | # ARRAY FETCH: initial | |
1727 | # SCALAR FETCH: initial eval-return, | |
3bc4ee4c DM |
1728 | # with fallback, we just stringify, so eval-return skipped, |
1729 | # but an extra FETCH happens in sub"", except for 'x', | |
1730 | # which passes a copy of the RV to sub"", avoiding the | |
1731 | # second FETCH | |
1732 | ||
1733 | push @tests, [ 18, "%s $op 3", "($_)", '("")', | |
1734 | [ 1, 2, 0, 1, ($_ eq '.' ? 2 : 1), 0 ], 1 ]; | |
1735 | next if $_ eq 'x'; # repeat only overloads on LHS | |
1736 | push @tests, [ 18, "3 $op %s", "($_)", '("")', | |
1737 | [ 1, 2, 0, 1, 2, 0 ], 1 ]; | |
a02ec77a DM |
1738 | } |
1739 | ||
6f1401dc DM |
1740 | for (qw(++ --)) { |
1741 | my $pre = "$_%s"; | |
1742 | my $post = "%s$_"; | |
1743 | $subs{$_} = $pre; | |
a02ec77a | 1744 | push @tests, |
6f1401dc DM |
1745 | # ARRAY FETCH: initial, sub+=, eval-return, |
1746 | # SCALAR FETCH: initial, sub=, sub+=, eval-return, | |
1747 | # STORE: copy, mutator | |
a02ec77a | 1748 | [ 18, $pre, "(=)($_)(\"\")", "(=)(NM:$_)(\"\")", [ 3, 4, 2 ], 1 ], |
6f1401dc DM |
1749 | # ARRAY FETCH: initial, sub+= |
1750 | # SCALAR FETCH: initial, sub=, sub+= | |
1751 | # STORE: copy, mutator | |
a02ec77a | 1752 | [ 18, $post, "(=)($_)(\"\")", "(=)(NM:$_)(\"\")", [ 2, 3, 2 ], 1 ]; |
6f1401dc DM |
1753 | } |
1754 | ||
1755 | # For the non-mutator ops, we have a initial FETCH, | |
1756 | # an extra FETCH within the sub itself for the scalar option, | |
1757 | # and no STOREs | |
1758 | ||
a02ec77a | 1759 | for (qw(< <= > >= == != lt le gt ge eq ne)) { |
6f1401dc DM |
1760 | my $e = "%s $_ 3"; |
1761 | $subs{$_} = $e; | |
a02ec77a DM |
1762 | push @tests, [ 3, $e, "($_)", "(NM:$_)", [ 1, 2, 0 ], 0 ]; |
1763 | } | |
1764 | for (qw(<=> cmp)) { | |
1765 | my $e = "%s $_ 3"; | |
1766 | $subs{$_} = $e; | |
1767 | push @tests, [ 3, $e, "($_)", "(NM:$_)", [ 1, 2, 0 ], 1 ]; | |
6f1401dc DM |
1768 | } |
1769 | for (qw(atan2)) { | |
1770 | my $e = "$_ %s, 3"; | |
1771 | $subs{$_} = $e; | |
a02ec77a | 1772 | push @tests, [ 18, $e, "($_)", "(NM:$_)", [ 1, 2, 0 ], 1 ]; |
6f1401dc | 1773 | } |
a02ec77a | 1774 | for (qw(cos sin exp abs log sqrt int ~)) { |
6f1401dc DM |
1775 | my $e = "$_(%s)"; |
1776 | $subs{$_} = $e; | |
a02ec77a DM |
1777 | push @tests, [ 1.23, $e, "($_)", |
1778 | ($_ eq 'int' ? '(0+)' : "(NM:$_)") , [ 1, 2, 0 ], 1 ]; | |
1779 | } | |
1780 | for (qw(!)) { | |
1781 | my $e = "$_(%s)"; | |
1782 | $subs{$_} = $e; | |
1783 | push @tests, [ 1.23, $e, "($_)", '(0+)', [ 1, 2, 0 ], 0 ]; | |
6f1401dc DM |
1784 | } |
1785 | for (qw(-)) { | |
1786 | my $e = "$_(%s)"; | |
1787 | $subs{neg} = $e; | |
a02ec77a | 1788 | push @tests, [ 18, $e, '(neg)', '(NM:neg)', [ 1, 2, 0 ], 1 ]; |
6f1401dc DM |
1789 | } |
1790 | my $e = '(%s) ? 1 : 0'; | |
1791 | $subs{bool} = $e; | |
a02ec77a | 1792 | push @tests, [ 18, $e, '(bool)', '(0+)', [ 1, 2, 0 ], 0 ]; |
6f1401dc DM |
1793 | |
1794 | # note: this is testing unary qr, not binary =~ | |
a02ec77a | 1795 | $subs{qr} = '(qr/%s/)'; |
f3ec07c7 | 1796 | push @tests, [ "abc", '"abc" =~ (%s)', '(qr)', '("")', [ 1, 2, 0 ], 0 ]; |
6f1401dc DM |
1797 | |
1798 | $e = '"abc" ~~ (%s)'; | |
1799 | $subs{'~~'} = $e; | |
a02ec77a | 1800 | push @tests, [ "abc", $e, '(~~)', '(NM:~~)', [ 1, 1, 0 ], 0 ]; |
6f1401dc DM |
1801 | |
1802 | $subs{'-X'} = 'do { my $f = (%s);' | |
1803 | . '$_[1] eq "r" ? (-r ($f)) :' | |
1804 | . '$_[1] eq "e" ? (-e ($f)) :' | |
1805 | . '$_[1] eq "f" ? (-f ($f)) :' | |
1806 | . '$_[1] eq "l" ? (-l ($f)) :' | |
1807 | . '$_[1] eq "t" ? (-t ($f)) :' | |
1808 | . '$_[1] eq "T" ? (-T ($f)) : 0;}'; | |
7f1e438c | 1809 | # Note - we don't care what these file tests return, as |
6f1401dc DM |
1810 | # long as the tied and untied versions return the same value. |
1811 | # The flags below are chosen to test all uses of tryAMAGICftest_MG | |
1812 | for (qw(r e f l t T)) { | |
40c852de | 1813 | push @tests, [ 'TEST', "-$_ (%s)", '(-X)', '("")', [ 1, 2, 0 ], 0 ]; |
6f1401dc DM |
1814 | } |
1815 | ||
1816 | $subs{'${}'} = '%s'; | |
a02ec77a | 1817 | push @tests, [ do {my $s=99; \$s}, '${%s}', '(${})', undef, [ 1, 1, 0 ], 0 ]; |
6f1401dc DM |
1818 | |
1819 | # we skip testing '@{}' here because too much of this test | |
a02ec77a | 1820 | # framework involves array dereferences! |
6f1401dc DM |
1821 | |
1822 | $subs{'%{}'} = '%s'; | |
a02ec77a DM |
1823 | push @tests, [ {qw(a 1 b 2 c 3)}, 'join "", sort keys %%{%s}', |
1824 | '(%{})', undef, [ 1, 2, 0 ], 0 ]; | |
6f1401dc DM |
1825 | |
1826 | $subs{'&{}'} = '%s'; | |
a02ec77a DM |
1827 | push @tests, [ sub {99}, 'do {&{%s} for 1,2}', |
1828 | '(&{})(&{})', undef, [ 2, 2, 0 ], 0 ]; | |
6f1401dc DM |
1829 | |
1830 | our $RT57012A = 88; | |
1831 | our $RT57012B; | |
1832 | $subs{'*{}'} = '%s'; | |
a02ec77a DM |
1833 | push @tests, [ \*RT57012A, '*RT57012B = *{%s}; our $RT57012B', |
1834 | '(*{})', undef, [ 1, 1, 0 ], 0 ]; | |
6f1401dc | 1835 | |
9426e1a5 DM |
1836 | my $iter_text = ("some random text\n" x 100) . $^X; |
1837 | open my $iter_fh, '<', \$iter_text | |
1838 | or die "open of \$iter_text gave ($!)\n"; | |
1839 | $subs{'<>'} = '<$iter_fh>'; | |
1840 | push @tests, [ $iter_fh, '<%s>', '(<>)', undef, [ 1, 1, 0 ], 1 ]; | |
6f1401dc | 1841 | |
895b760f DM |
1842 | # eval should do tie, overload on its arg before checking taint */ |
1843 | push @tests, [ '1;', 'eval q(eval %s); $@ =~ /Insecure/', | |
1844 | '("")', '("")', [ 1, 2, 0 ], 0 ]; | |
1845 | ||
1846 | ||
6f1401dc DM |
1847 | for my $sub (keys %subs) { |
1848 | my $term = $subs{$sub}; | |
1849 | my $t = sprintf $term, '$_[0][0]'; | |
a02ec77a | 1850 | my $e ="sub { \$funcs .= '($sub)'; my \$r; if (\$use_int) {" |
6f1401dc | 1851 | . "use integer; \$r = ($t) } else { \$r = ($t) } \$r }"; |
a02ec77a DM |
1852 | $subs{$sub} = eval $e; |
1853 | die "Compiling sub gave error:\n<$e>\n<$@>\n" if $@; | |
6f1401dc DM |
1854 | } |
1855 | } | |
1856 | ||
1857 | my $fetches; | |
1858 | my $stores; | |
1859 | ||
1860 | package RT57012_OV; | |
1861 | ||
6f1401dc DM |
1862 | use overload |
1863 | %subs, | |
a02ec77a DM |
1864 | "=" => sub { $funcs .= '(=)'; bless [ $_[0][0] ] }, |
1865 | '0+' => sub { $funcs .= '(0+)'; 0 + $_[0][0] }, | |
1866 | '""' => sub { $funcs .= '("")'; "$_[0][0]" }, | |
1867 | ; | |
1868 | ||
1869 | package RT57012_OV_FB; # only contains fallback conversion functions | |
1870 | ||
1871 | use overload | |
1872 | "=" => sub { $funcs .= '(=)'; bless [ $_[0][0] ] }, | |
1873 | '0+' => sub { $funcs .= '(0+)'; 0 + $_[0][0] }, | |
1874 | '""' => sub { $funcs .= '("")'; "$_[0][0]" }, | |
1875 | "nomethod" => sub { | |
1876 | $funcs .= "(NM:$_[3])"; | |
1877 | my $e = defined($_[1]) | |
1878 | ? $_[3] eq 'atan2' | |
1879 | ? $_[2] | |
1880 | ? "atan2(\$_[1],\$_[0][0])" | |
1881 | : "atan2(\$_[0][0],\$_[1])" | |
1882 | : $_[2] | |
1883 | ? "\$_[1] $_[3] \$_[0][0]" | |
1884 | : "\$_[0][0] $_[3] \$_[1]" | |
1885 | : $_[3] eq 'neg' | |
1886 | ? "-\$_[0][0]" | |
1887 | : "$_[3](\$_[0][0])"; | |
1888 | my $r; | |
1889 | if ($use_int) { | |
1890 | use integer; $r = eval $e; | |
1891 | } | |
1892 | else { | |
1893 | $r = eval $e; | |
1894 | } | |
1895 | ::diag("eval of nomethod <$e> gave <$@>") if $@; | |
1896 | $r; | |
1897 | } | |
1898 | ||
6f1401dc DM |
1899 | ; |
1900 | ||
1901 | package RT57012_TIE_S; | |
1902 | ||
1903 | my $tie_val; | |
a02ec77a | 1904 | sub TIESCALAR { bless [ bless [ $tie_val ], $_[1] ] } |
6f1401dc DM |
1905 | sub FETCH { $fetches++; $_[0][0] } |
1906 | sub STORE { $stores++; $_[0][0] = $_[1] } | |
1907 | ||
1908 | package RT57012_TIE_A; | |
1909 | ||
1910 | sub TIEARRAY { bless [] } | |
1911 | sub FETCH { $fetches++; $_[0][0] } | |
1912 | sub STORE { $stores++; $_[0][$_[1]] = $_[2] } | |
1913 | ||
1914 | package main; | |
1915 | ||
a02ec77a DM |
1916 | for my $test (@tests) { |
1917 | my ($val, $sub_term, $exp_funcs, $exp_fb_funcs, | |
1918 | $exp_counts, $exp_taint) = @$test; | |
1919 | ||
1920 | my $tainted_val; | |
1921 | { | |
1922 | # create tainted version of $val (unless its a ref) | |
1923 | my $t = substr($^X,0,0); | |
1924 | my $t0 = $t."0"; | |
1925 | my $val1 = $val; # use a copy to avoid stringifying original | |
1926 | $tainted_val = ref($val1) ? $val : | |
1927 | ($val1 =~ /^[\d\.]+$/) ? $val+$t0 : $val.$t; | |
1928 | } | |
1929 | $tie_val = $tainted_val; | |
6f1401dc | 1930 | |
6f1401dc DM |
1931 | for my $int ('', 'use integer; ') { |
1932 | $use_int = ($int ne ''); | |
a02ec77a DM |
1933 | my $plain = $tainted_val; |
1934 | my $plain_term = $int . sprintf $sub_term, '$plain'; | |
1935 | my $exp = eval $plain_term; | |
1936 | diag("eval of plain_term <$plain_term> gave <$@>") if $@; | |
1937 | is(tainted($exp), $exp_taint, | |
1938 | "<$plain_term> taint of expected return"); | |
1939 | ||
1940 | for my $ov_pkg (qw(RT57012_OV RT57012_OV_FB)) { | |
a02ec77a DM |
1941 | next if $ov_pkg eq 'RT57012_OV_FB' |
1942 | and not defined $exp_fb_funcs; | |
1943 | my ($exp_fetch_a, $exp_fetch_s, $exp_store) = | |
1944 | ($ov_pkg eq 'RT57012_OV' || @$exp_counts < 4) | |
1945 | ? @$exp_counts[0,1,2] | |
1946 | : @$exp_counts[3,4,5]; | |
1947 | ||
1948 | tie my $ts, 'RT57012_TIE_S', $ov_pkg; | |
6f1401dc | 1949 | tie my @ta, 'RT57012_TIE_A'; |
a02ec77a DM |
1950 | $ta[0] = bless [ $tainted_val ], $ov_pkg; |
1951 | my $oload = bless [ $tainted_val ], $ov_pkg; | |
1952 | ||
9426e1a5 DM |
1953 | for my $var ('$ta[0]', '$ts', '$oload', |
1954 | ($sub_term eq '<%s>' ? '${ts}' : ()) | |
1955 | ) { | |
a02ec77a DM |
1956 | |
1957 | $funcs = ''; | |
1958 | $fetches = 0; | |
1959 | $stores = 0; | |
1960 | ||
1961 | my $res_term = $int . sprintf $sub_term, $var; | |
1962 | my $desc = "<$res_term> $ov_pkg" ; | |
1963 | my $res = eval $res_term; | |
1964 | diag("eval of res_term $desc gave <$@>") if $@; | |
7f1e438c | 1965 | # uniquely, the inc/dec ops return the original |
a02ec77a DM |
1966 | # ref rather than a copy, so stringify it to |
1967 | # find out if its tainted | |
1968 | $res = "$res" if $res_term =~ /\+\+|--/; | |
1969 | is(tainted($res), $exp_taint, | |
1970 | "$desc taint of result return"); | |
a02ec77a DM |
1971 | is($res, $exp, "$desc return value"); |
1972 | my $fns =($ov_pkg eq 'RT57012_OV_FB') | |
1973 | ? $exp_fb_funcs : $exp_funcs; | |
1974 | if ($var eq '$oload' && $res_term !~ /oload(\+\+|--)/) { | |
1975 | # non-tied overloading doesn't trigger a copy | |
1976 | # except for post inc/dec | |
1977 | $fns =~ s/^\(=\)//; | |
1978 | } | |
1979 | is($funcs, $fns, "$desc methods called"); | |
1980 | next if $var eq '$oload'; | |
1981 | my $exp_fetch = ($var eq '$ts') ? | |
1982 | $exp_fetch_s : $exp_fetch_a; | |
1983 | is($fetches, $exp_fetch, "$desc FETCH count"); | |
1984 | is($stores, $exp_store, "$desc STORE count"); | |
1985 | ||
1986 | } | |
1987 | ||
6f1401dc DM |
1988 | } |
1989 | } | |
1990 | } | |
1991 | } | |
1992 | ||
25222ff9 FC |
1993 | # Test overload from the main package |
1994 | fresh_perl_is | |
1995 | '$^W = 1; use overload q\""\ => sub {"ning"}; print bless []', | |
1996 | 'ning', | |
1997 | { switches => ['-wl'], stderr => 1 }, | |
1998 | 'use overload from the main package' | |
1999 | ; | |
2000 | ||
56f08af2 FC |
2001 | { |
2002 | package blessed_methods; | |
2003 | use overload '+' => sub {}; | |
2004 | bless overload::Method __PACKAGE__,'+'; | |
2005 | eval { overload::Method __PACKAGE__,'+' }; | |
2006 | ::is($@, '', 'overload::Method and blessed overload methods'); | |
2007 | } | |
2008 | ||
bf5522a1 MB |
2009 | { |
2010 | # fallback to 'cmp' and '<=>' with heterogeneous operands | |
2011 | # [perl #71286] | |
2012 | my $not_found = 'no method found'; | |
2013 | my $used = 0; | |
2014 | package CmpBase; | |
2015 | sub new { | |
2016 | my $n = $_[1] || 0; | |
2017 | bless \$n, ref $_[0] || $_[0]; | |
2018 | } | |
2019 | sub cmp { | |
2020 | $used = \$_[0]; | |
2021 | (${$_[0]} <=> ${$_[1]}) * ($_[2] ? -1 : 1); | |
2022 | } | |
2023 | ||
2024 | package NCmp; | |
2025 | use base 'CmpBase'; | |
2026 | use overload '<=>' => 'cmp'; | |
2027 | ||
2028 | package SCmp; | |
2029 | use base 'CmpBase'; | |
2030 | use overload 'cmp' => 'cmp'; | |
2031 | ||
2032 | package main; | |
2033 | my $n = NCmp->new(5); | |
2034 | my $s = SCmp->new(3); | |
2035 | my $res; | |
2036 | ||
2037 | eval { $res = $n > $s; }; | |
2038 | $res = $not_found if $@ =~ /$not_found/; | |
2039 | is($res, 1, 'A>B using A<=> when B overloaded, no B<=>'); | |
2040 | ||
2041 | eval { $res = $s < $n; }; | |
2042 | $res = $not_found if $@ =~ /$not_found/; | |
2043 | is($res, 1, 'A<B using B<=> when A overloaded, no A<=>'); | |
2044 | ||
2045 | eval { $res = $s lt $n; }; | |
2046 | $res = $not_found if $@ =~ /$not_found/; | |
2047 | is($res, 1, 'A lt B using A:cmp when B overloaded, no B:cmp'); | |
2048 | ||
2049 | eval { $res = $n gt $s; }; | |
2050 | $res = $not_found if $@ =~ /$not_found/; | |
2051 | is($res, 1, 'A gt B using B:cmp when A overloaded, no A:cmp'); | |
2052 | ||
2053 | my $o = NCmp->new(9); | |
2054 | $res = $n < $o; | |
2055 | is($used, \$n, 'A < B uses <=> from A in preference to B'); | |
2056 | ||
2057 | my $t = SCmp->new(7); | |
2058 | $res = $s lt $t; | |
2059 | is($used, \$s, 'A lt B uses cmp from A in preference to B'); | |
2060 | } | |
2061 | ||
2062 | { | |
2063 | # Combinatorial testing of 'fallback' and 'nomethod' | |
2064 | # [perl #71286] | |
2065 | package NuMB; | |
2066 | use overload '0+' => sub { ${$_[0]}; }, | |
2067 | '""' => 'str'; | |
2068 | sub new { | |
2069 | my $self = shift; | |
2070 | my $n = @_ ? shift : 0; | |
2071 | bless my $obj = \$n, ref $self || $self; | |
2072 | } | |
2073 | sub str { | |
2074 | no strict qw/refs/; | |
2075 | my $s = "(${$_[0]} "; | |
2076 | $s .= "nomethod, " if defined ${ref($_[0]).'::(nomethod'}; | |
2077 | my $fb = ${ref($_[0]).'::()'}; | |
2078 | $s .= "fb=" . (defined $fb ? 0 + $fb : 'undef') . ")"; | |
2079 | } | |
2080 | sub nomethod { "${$_[0]}.nomethod"; } | |
2081 | ||
2082 | # create classes for tests | |
2083 | package main; | |
2084 | my @falls = (0, 'undef', 1); | |
2085 | my @nomethods = ('', 'nomethod'); | |
2086 | my $not_found = 'no method found'; | |
2087 | for my $fall (@falls) { | |
2088 | for my $nomethod (@nomethods) { | |
2089 | my $nomethod_decl = $nomethod | |
2090 | ? $nomethod . "=>'nomethod'," : ''; | |
2091 | eval qq{ | |
2092 | package NuMB$fall$nomethod; | |
2093 | use base qw/NuMB/; | |
2094 | use overload $nomethod_decl | |
2095 | fallback => $fall; | |
2096 | }; | |
2097 | } | |
2098 | } | |
2099 | ||
2100 | # operation and precedence of 'fallback' and 'nomethod' | |
2101 | # for all combinations with 2 overloaded operands | |
2102 | for my $nomethod2 (@nomethods) { | |
2103 | for my $nomethod1 (@nomethods) { | |
2104 | for my $fall2 (@falls) { | |
2105 | my $pack2 = "NuMB$fall2$nomethod2"; | |
2106 | for my $fall1 (@falls) { | |
2107 | my $pack1 = "NuMB$fall1$nomethod1"; | |
2108 | my ($test, $out, $exp); | |
2109 | eval qq{ | |
2110 | my \$x = $pack1->new(2); | |
2111 | my \$y = $pack2->new(3); | |
2112 | \$test = "\$x" . ' * ' . "\$y"; | |
2113 | \$out = \$x * \$y; | |
2114 | }; | |
2115 | $out = $not_found if $@ =~ /$not_found/; | |
2116 | $exp = $nomethod1 ? '2.nomethod' : | |
2117 | $nomethod2 ? '3.nomethod' : | |
2118 | $fall1 eq '1' && $fall2 eq '1' ? 6 | |
2119 | : $not_found; | |
2120 | is($out, $exp, "$test --> $exp"); | |
2121 | } | |
2122 | } | |
2123 | } | |
2124 | } | |
2125 | ||
2126 | # operation of 'fallback' and 'nomethod' | |
2127 | # where the other operand is not overloaded | |
2128 | for my $nomethod (@nomethods) { | |
2129 | for my $fall (@falls) { | |
2130 | my ($test, $out, $exp); | |
2131 | eval qq{ | |
2132 | my \$x = NuMB$fall$nomethod->new(2); | |
2133 | \$test = "\$x" . ' * 3'; | |
2134 | \$out = \$x * 3; | |
2135 | }; | |
2136 | $out = $not_found if $@ =~ /$not_found/; | |
2137 | $exp = $nomethod ? '2.nomethod' : | |
2138 | $fall eq '1' ? 6 | |
2139 | : $not_found; | |
2140 | is($out, $exp, "$test --> $exp"); | |
2141 | ||
2142 | eval qq{ | |
2143 | my \$x = NuMB$fall$nomethod->new(2); | |
2144 | \$test = '3 * ' . "\$x"; | |
2145 | \$out = 3 * \$x; | |
2146 | }; | |
2147 | $out = $not_found if $@ =~ /$not_found/; | |
2148 | is($out, $exp, "$test --> $exp"); | |
2149 | } | |
2150 | } | |
2151 | } | |
2152 | ||
bff33ce0 DM |
2153 | # since 5.6 overloaded <> was leaving an extra arg on the stack! |
2154 | ||
2155 | { | |
2156 | package Iter1; | |
2157 | use overload '<>' => sub { 11 }; | |
2158 | package main; | |
2159 | my $a = bless [], 'Iter1'; | |
2160 | my $x; | |
2161 | my @a = (10, ($x = <$a>), 12); | |
2162 | is ($a[0], 10, 'Iter1: a[0]'); | |
2163 | is ($a[1], 11, 'Iter1: a[1]'); | |
2164 | is ($a[2], 12, 'Iter1: a[2]'); | |
2165 | @a = (10, ($x .= <$a>), 12); | |
2166 | is ($a[0], 10, 'Iter1: a[0] concat'); | |
2167 | is ($a[1], 1111, 'Iter1: a[1] concat'); | |
2168 | is ($a[2], 12, 'Iter1: a[2] concat'); | |
2169 | } | |
bf5522a1 MB |
2170 | |
2171 | ||
800401ee | 2172 | # EOF |