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; |
9b513004 | 50 | BEGIN { require './test.pl'; require './charset_tools.pl' } |
7896dde7 | 51 | plan tests => 5338; |
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"); | |
bb4f579a | 134 | is($b, "89"); |
b1fbf5c3 | 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"); | |
bb4f579a | 145 | is($b, "91"); |
b1fbf5c3 | 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 | |
69815d08 | 205 | eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1]; |
8ebc5c01 | 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 | |
bb4f579a DM |
270 | # Here we test that both "no overload" and |
271 | # blessing to a package update hash | |
8ebc5c01 | 272 | |
273 | eval "package Oscalar; no overload '.'"; | |
274 | ||
bb4f579a | 275 | is("b${a}", "bxx"); |
8ebc5c01 | 276 | $x="1"; |
277 | bless \$x, Oscalar; | |
b1fbf5c3 | 278 | is("b${a}c", "bxxc"); |
8ebc5c01 | 279 | new Oscalar 1; |
b1fbf5c3 | 280 | is("b${a}c", "bxxc"); |
8ebc5c01 | 281 | |
282 | # Negative overloading: | |
283 | ||
284 | $na = eval { ~$a }; | |
b1fbf5c3 | 285 | like($@, qr/no method found/); |
8ebc5c01 | 286 | |
287 | # Check AUTOLOADING: | |
288 | ||
289 | *Oscalar::AUTOLOAD = | |
290 | sub { *{"Oscalar::$AUTOLOAD"} = sub {"_!_" . shift() . "_!_"} ; | |
291 | goto &{"Oscalar::$AUTOLOAD"}}; | |
292 | ||
44a8e56a | 293 | eval "package Oscalar; sub comple; use overload '~' => 'comple'"; |
8ebc5c01 | 294 | |
bb4f579a DM |
295 | $na = eval { ~$a }; |
296 | is($@, ''); | |
8ebc5c01 | 297 | |
298 | bless \$x, Oscalar; | |
299 | ||
300 | $na = eval { ~$a }; # Hash updated | |
1f874cb6 | 301 | warn "'$na', $@" if $@; |
b1fbf5c3 NC |
302 | ok !$@; |
303 | is($na, '_!_xx_!_'); | |
8ebc5c01 | 304 | |
305 | $na = 0; | |
306 | ||
bb4f579a | 307 | $na = eval { ~$aI }; |
aaa63dae | 308 | is($@, ''); |
8ebc5c01 | 309 | |
310 | bless \$x, OscalarI; | |
311 | ||
312 | $na = eval { ~$aI }; | |
313 | print $@; | |
314 | ||
fdb7e2a2 NC |
315 | ok(!$@); |
316 | is($na, '_!_xx_!_'); | |
8ebc5c01 | 317 | |
44a8e56a | 318 | eval "package Oscalar; sub rshft; use overload '>>' => 'rshft'"; |
8ebc5c01 | 319 | |
bb4f579a DM |
320 | $na = eval { $aI >> 1 }; |
321 | is($@, ''); | |
8ebc5c01 | 322 | |
323 | bless \$x, OscalarI; | |
324 | ||
325 | $na = 0; | |
326 | ||
327 | $na = eval { $aI >> 1 }; | |
328 | print $@; | |
329 | ||
fdb7e2a2 NC |
330 | ok(!$@); |
331 | is($na, '_!_xx_!_'); | |
8ebc5c01 | 332 | |
44a8e56a | 333 | # warn overload::Method($a, '0+'), "\n"; |
fdb7e2a2 NC |
334 | is(overload::Method($a, '0+'), \&Oscalar::numify); |
335 | is(overload::Method($aI,'0+'), \&Oscalar::numify); | |
336 | ok(overload::Overloaded($aI)); | |
337 | ok(!overload::Overloaded('overload')); | |
8ebc5c01 | 338 | |
fdb7e2a2 NC |
339 | ok(! defined overload::Method($aI, '<<')); |
340 | ok(! defined overload::Method($a, '<')); | |
8ebc5c01 | 341 | |
fdb7e2a2 NC |
342 | like (overload::StrVal($aI), qr/^OscalarI=SCALAR\(0x[\da-fA-F]+\)$/); |
343 | is(overload::StrVal(\$aI), "@{[\$aI]}"); | |
8ebc5c01 | 344 | |
44a8e56a | 345 | # Check overloading by methods (specified deep in the ISA tree). |
346 | { | |
347 | package OscalarII; | |
348 | @ISA = 'OscalarI'; | |
349 | sub Oscalar::lshft {"_<<_" . shift() . "_<<_"} | |
350 | eval "package OscalarI; use overload '<<' => 'lshft', '|' => 'lshft'"; | |
351 | } | |
352 | ||
353 | $aaII = "087"; | |
354 | $aII = \$aaII; | |
355 | bless $aII, 'OscalarII'; | |
356 | bless \$fake, 'OscalarI'; # update the hash | |
fdb7e2a2 | 357 | is(($aI | 3), '_<<_xx_<<_'); |
44a8e56a | 358 | # warn $aII << 3; |
fdb7e2a2 | 359 | is(($aII << 3), '_<<_087_<<_'); |
44a8e56a | 360 | |
b3ac6de7 IZ |
361 | { |
362 | BEGIN { $int = 7; overload::constant 'integer' => sub {$int++; shift}; } | |
363 | $out = 2**10; | |
364 | } | |
fdb7e2a2 NC |
365 | is($int, 9); |
366 | is($out, 1024); | |
7898bf0b RGS |
367 | is($int, 9); |
368 | { | |
369 | BEGIN { overload::constant 'integer' => sub {$int++; shift()+1}; } | |
370 | eval q{$out = 42}; | |
371 | } | |
372 | is($int, 10); | |
373 | is($out, 43); | |
b3ac6de7 IZ |
374 | |
375 | $foo = 'foo'; | |
376 | $foo1 = 'f\'o\\o'; | |
377 | { | |
378 | BEGIN { $q = $qr = 7; | |
379 | overload::constant 'q' => sub {$q++; push @q, shift, ($_[1] || 'none'); shift}, | |
380 | 'qr' => sub {$qr++; push @qr, shift, ($_[1] || 'none'); shift}; } | |
381 | $out = 'foo'; | |
382 | $out1 = 'f\'o\\o'; | |
383 | $out2 = "a\a$foo,\,"; | |
384 | /b\b$foo.\./; | |
385 | } | |
386 | ||
fdb7e2a2 NC |
387 | is($out, 'foo'); |
388 | is($out, $foo); | |
389 | is($out1, 'f\'o\\o'); | |
390 | is($out1, $foo1); | |
391 | is($out2, "a\afoo,\,"); | |
392 | is("@q", "foo q f'o\\\\o q a\\a qq ,\\, qq"); | |
393 | is($q, 11); | |
394 | is("@qr", "b\\b qq .\\. qq"); | |
395 | is($qr, 9); | |
b3ac6de7 IZ |
396 | |
397 | { | |
398 | $_ = '!<b>!foo!<-.>!'; | |
399 | BEGIN { overload::constant 'q' => sub {push @q1, shift, ($_[1] || 'none'); "_<" . (shift) . ">_"}, | |
400 | 'qr' => sub {push @qr1, shift, ($_[1] || 'none'); "!<" . (shift) . ">!"}; } | |
401 | $out = 'foo'; | |
402 | $out1 = 'f\'o\\o'; | |
403 | $out2 = "a\a$foo,\,"; | |
404 | $res = /b\b$foo.\./; | |
405 | $a = <<EOF; | |
406 | oups | |
407 | EOF | |
408 | $b = <<'EOF'; | |
409 | oups1 | |
410 | EOF | |
411 | $c = bareword; | |
412 | m'try it'; | |
413 | s'first part'second part'; | |
414 | s/yet another/tail here/; | |
c2e66d9e | 415 | tr/A-Z/a-z/; |
b3ac6de7 IZ |
416 | } |
417 | ||
fdb7e2a2 NC |
418 | is($out, '_<foo>_'); |
419 | is($out1, '_<f\'o\\o>_'); | |
420 | is($out2, "_<a\a>_foo_<,\,>_"); | |
421 | is("@q1", "foo q f'o\\\\o q a\\a qq ,\\, qq oups | |
b3ac6de7 | 422 | qq oups1 |
fdb7e2a2 NC |
423 | q second part q tail here s A-Z tr a-z tr"); |
424 | is("@qr1", "b\\b qq .\\. qq try it q first part q yet another qq"); | |
425 | is($res, 1); | |
426 | is($a, "_<oups | |
427 | >_"); | |
428 | is($b, "_<oups1 | |
429 | >_"); | |
430 | is($c, "bareword"); | |
b3ac6de7 | 431 | |
ee239bfe IZ |
432 | { |
433 | package symbolic; # Primitive symbolic calculator | |
434 | use overload nomethod => \&wrap, '""' => \&str, '0+' => \&num, | |
435 | '=' => \&cpy, '++' => \&inc, '--' => \&dec; | |
436 | ||
437 | sub new { shift; bless ['n', @_] } | |
438 | sub cpy { | |
439 | my $self = shift; | |
440 | bless [@$self], ref $self; | |
441 | } | |
442 | sub inc { $_[0] = bless ['++', $_[0], 1]; } | |
443 | sub dec { $_[0] = bless ['--', $_[0], 1]; } | |
444 | sub wrap { | |
445 | my ($obj, $other, $inv, $meth) = @_; | |
446 | if ($meth eq '++' or $meth eq '--') { | |
447 | @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference | |
448 | return $obj; | |
449 | } | |
450 | ($obj, $other) = ($other, $obj) if $inv; | |
451 | bless [$meth, $obj, $other]; | |
452 | } | |
453 | sub str { | |
454 | my ($meth, $a, $b) = @{+shift}; | |
455 | $a = 'u' unless defined $a; | |
456 | if (defined $b) { | |
457 | "[$meth $a $b]"; | |
458 | } else { | |
459 | "[$meth $a]"; | |
460 | } | |
461 | } | |
462 | my %subr = ( 'n' => sub {$_[0]} ); | |
463 | foreach my $op (split " ", $overload::ops{with_assign}) { | |
464 | $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}"; | |
465 | } | |
466 | my @bins = qw(binary 3way_comparison num_comparison str_comparison); | |
467 | foreach my $op (split " ", "@overload::ops{ @bins }") { | |
468 | $subr{$op} = eval "sub {shift() $op shift()}"; | |
469 | } | |
470 | foreach my $op (split " ", "@overload::ops{qw(unary func)}") { | |
471 | $subr{$op} = eval "sub {$op shift()}"; | |
472 | } | |
473 | $subr{'++'} = $subr{'+'}; | |
474 | $subr{'--'} = $subr{'-'}; | |
475 | ||
476 | sub num { | |
477 | my ($meth, $a, $b) = @{+shift}; | |
478 | my $subr = $subr{$meth} | |
479 | or die "Do not know how to ($meth) in symbolic"; | |
480 | $a = $a->num if ref $a eq __PACKAGE__; | |
481 | $b = $b->num if ref $b eq __PACKAGE__; | |
482 | $subr->($a,$b); | |
483 | } | |
484 | sub TIESCALAR { my $pack = shift; $pack->new(@_) } | |
485 | sub FETCH { shift } | |
486 | sub nop { } # Around a bug | |
487 | sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; } | |
488 | sub STORE { | |
489 | my $obj = shift; | |
490 | $#$obj = 1; | |
a1063b2d | 491 | $obj->[1] = shift; |
ee239bfe IZ |
492 | } |
493 | } | |
494 | ||
495 | { | |
496 | my $foo = new symbolic 11; | |
497 | my $baz = $foo++; | |
fdb7e2a2 NC |
498 | is((sprintf "%d", $foo), '12'); |
499 | is((sprintf "%d", $baz), '11'); | |
ee239bfe IZ |
500 | my $bar = $foo; |
501 | $baz = ++$foo; | |
fdb7e2a2 NC |
502 | is((sprintf "%d", $foo), '13'); |
503 | is((sprintf "%d", $bar), '12'); | |
504 | is((sprintf "%d", $baz), '13'); | |
ee239bfe IZ |
505 | my $ban = $foo; |
506 | $baz = ($foo += 1); | |
fdb7e2a2 NC |
507 | is((sprintf "%d", $foo), '14'); |
508 | is((sprintf "%d", $bar), '12'); | |
509 | is((sprintf "%d", $baz), '14'); | |
510 | is((sprintf "%d", $ban), '13'); | |
ee239bfe IZ |
511 | $baz = 0; |
512 | $baz = $foo++; | |
fdb7e2a2 NC |
513 | is((sprintf "%d", $foo), '15'); |
514 | is((sprintf "%d", $baz), '14'); | |
515 | is("$foo", '[++ [+= [++ [++ [n 11] 1] 1] 1] 1]'); | |
ee239bfe IZ |
516 | } |
517 | ||
518 | { | |
519 | my $iter = new symbolic 2; | |
520 | my $side = new symbolic 1; | |
521 | my $cnt = $iter; | |
522 | ||
523 | while ($cnt) { | |
524 | $cnt = $cnt - 1; # The "simple" way | |
525 | $side = (sqrt(1 + $side**2) - 1)/$side; | |
526 | } | |
527 | my $pi = $side*(2**($iter+2)); | |
fdb7e2a2 NC |
528 | is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]'); |
529 | is((sprintf "%f", $pi), '3.182598'); | |
ee239bfe IZ |
530 | } |
531 | ||
532 | { | |
533 | my $iter = new symbolic 2; | |
534 | my $side = new symbolic 1; | |
535 | my $cnt = $iter; | |
536 | ||
537 | while ($cnt--) { | |
538 | $side = (sqrt(1 + $side**2) - 1)/$side; | |
539 | } | |
540 | my $pi = $side*(2**($iter+2)); | |
fdb7e2a2 NC |
541 | is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]'); |
542 | is((sprintf "%f", $pi), '3.182598'); | |
ee239bfe IZ |
543 | } |
544 | ||
545 | { | |
546 | my ($a, $b); | |
547 | symbolic->vars($a, $b); | |
548 | my $c = sqrt($a**2 + $b**2); | |
549 | $a = 3; $b = 4; | |
fdb7e2a2 | 550 | is((sprintf "%d", $c), '5'); |
ee239bfe | 551 | $a = 12; $b = 5; |
fdb7e2a2 | 552 | is((sprintf "%d", $c), '13'); |
ee239bfe IZ |
553 | } |
554 | ||
555 | { | |
556 | package symbolic1; # Primitive symbolic calculator | |
557 | # Mutator inc/dec | |
558 | use overload nomethod => \&wrap, '""' => \&str, '0+' => \&num, '=' => \&cpy; | |
559 | ||
560 | sub new { shift; bless ['n', @_] } | |
561 | sub cpy { | |
562 | my $self = shift; | |
563 | bless [@$self], ref $self; | |
564 | } | |
565 | sub wrap { | |
566 | my ($obj, $other, $inv, $meth) = @_; | |
567 | if ($meth eq '++' or $meth eq '--') { | |
568 | @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference | |
569 | return $obj; | |
570 | } | |
571 | ($obj, $other) = ($other, $obj) if $inv; | |
572 | bless [$meth, $obj, $other]; | |
573 | } | |
574 | sub str { | |
575 | my ($meth, $a, $b) = @{+shift}; | |
576 | $a = 'u' unless defined $a; | |
577 | if (defined $b) { | |
578 | "[$meth $a $b]"; | |
579 | } else { | |
580 | "[$meth $a]"; | |
581 | } | |
582 | } | |
583 | my %subr = ( 'n' => sub {$_[0]} ); | |
584 | foreach my $op (split " ", $overload::ops{with_assign}) { | |
585 | $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}"; | |
586 | } | |
587 | my @bins = qw(binary 3way_comparison num_comparison str_comparison); | |
588 | foreach my $op (split " ", "@overload::ops{ @bins }") { | |
589 | $subr{$op} = eval "sub {shift() $op shift()}"; | |
590 | } | |
591 | foreach my $op (split " ", "@overload::ops{qw(unary func)}") { | |
592 | $subr{$op} = eval "sub {$op shift()}"; | |
593 | } | |
594 | $subr{'++'} = $subr{'+'}; | |
595 | $subr{'--'} = $subr{'-'}; | |
596 | ||
597 | sub num { | |
598 | my ($meth, $a, $b) = @{+shift}; | |
599 | my $subr = $subr{$meth} | |
600 | or die "Do not know how to ($meth) in symbolic"; | |
601 | $a = $a->num if ref $a eq __PACKAGE__; | |
602 | $b = $b->num if ref $b eq __PACKAGE__; | |
603 | $subr->($a,$b); | |
604 | } | |
605 | sub TIESCALAR { my $pack = shift; $pack->new(@_) } | |
606 | sub FETCH { shift } | |
7dbf2beb | 607 | sub vars { my $p = shift; tie($_, $p) foreach @_; } |
ee239bfe IZ |
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 | { | |
1f874cb6 | 939 | # check the '$_[0]' is not an overloadable type warning |
4498a751 | 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 {} ; ' ; | |
1f874cb6 | 946 | like($a, qr/^'fred' is not an overloadable type at/); |
4498a751 PM |
947 | } |
948 | ||
949 | { | |
1f874cb6 | 950 | # check the '$_[1]' is not a code reference warning |
4498a751 | 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; ' ; | |
1f874cb6 | 957 | like($a, qr/^'1' is not a code reference at/); |
4498a751 PM |
958 | } |
959 | ||
78f67eb5 | 960 | { |
d4a6f307 DM |
961 | # check the invalid argument warning [perl #74098] |
962 | my $a = "" ; | |
963 | local $SIG{__WARN__} = sub {$a = $_[0]} ; | |
964 | $x = eval ' use overload "~|_|~" => sub{} ' ; | |
bb4f579a | 965 | eval ' no overload "~|_|~" ' ; |
d4a6f307 DM |
966 | is($a, ""); |
967 | use warnings 'overload' ; | |
968 | $x = eval ' use overload "~|_|~" => sub{} ' ; | |
969 | like($a, qr/^overload arg '~\|_\|~' is invalid at \(eval \d+\) line /, | |
970 | 'invalid arg warning'); | |
bb4f579a DM |
971 | undef $a; |
972 | eval ' no overload "~|_|~" ' ; | |
973 | like($a, qr/^overload arg '~\|_\|~' is invalid at \(eval \d+\) line /, | |
974 | 'invalid arg warning'); | |
d4a6f307 DM |
975 | } |
976 | ||
977 | { | |
78f67eb5 JH |
978 | my $c = 0; |
979 | package ov_int1; | |
980 | use overload '""' => sub { 3+shift->[0] }, | |
981 | '0+' => sub { 10+shift->[0] }, | |
982 | 'int' => sub { 100+shift->[0] }; | |
983 | sub new {my $p = shift; bless [shift], $p} | |
984 | ||
985 | package ov_int2; | |
986 | use overload '""' => sub { 5+shift->[0] }, | |
987 | '0+' => sub { 30+shift->[0] }, | |
988 | 'int' => sub { 'ov_int1'->new(1000+shift->[0]) }; | |
989 | sub new {my $p = shift; bless [shift], $p} | |
990 | ||
991 | package noov_int; | |
992 | use overload '""' => sub { 2+shift->[0] }, | |
993 | '0+' => sub { 9+shift->[0] }; | |
994 | sub new {my $p = shift; bless [shift], $p} | |
995 | ||
996 | package main; | |
997 | ||
998 | my $x = new noov_int 11; | |
999 | my $int_x = int $x; | |
7bf00c86 | 1000 | main::is("$int_x", 20); |
78f67eb5 JH |
1001 | $x = new ov_int1 31; |
1002 | $int_x = int $x; | |
7bf00c86 | 1003 | main::is("$int_x", 131); |
78f67eb5 JH |
1004 | $x = new ov_int2 51; |
1005 | $int_x = int $x; | |
7bf00c86 | 1006 | main::is("$int_x", 1054); |
78f67eb5 JH |
1007 | } |
1008 | ||
b0bf6df7 | 1009 | # make sure that we don't infinitely recurse |
1554e226 DC |
1010 | { |
1011 | my $c = 0; | |
1012 | package Recurse; | |
1013 | use overload '""' => sub { shift }, | |
1014 | '0+' => sub { shift }, | |
1015 | 'bool' => sub { shift }, | |
1016 | fallback => 1; | |
1017 | my $x = bless([]); | |
7bf00c86 NC |
1018 | # For some reason beyond me these have to be oks rather than likes. |
1019 | main::ok("$x" =~ /Recurse=ARRAY/); | |
1020 | main::ok($x); | |
1021 | main::ok($x+0 =~ qr/Recurse=ARRAY/); | |
11e3e2e4 | 1022 | } |
78f67eb5 | 1023 | |
126fe723 | 1024 | # BugID 20010422.003 (#6872) |
1dc13c17 SC |
1025 | package Foo; |
1026 | ||
1027 | use overload | |
1028 | 'bool' => sub { return !$_[0]->is_zero() || undef; } | |
1029 | ; | |
1030 | ||
1031 | sub is_zero | |
1032 | { | |
1033 | my $self = shift; | |
1034 | return $self->{var} == 0; | |
1035 | } | |
1036 | ||
1037 | sub new | |
1038 | { | |
1039 | my $class = shift; | |
1040 | my $self = {}; | |
1041 | $self->{var} = shift; | |
1042 | bless $self,$class; | |
1043 | } | |
1044 | ||
1045 | package main; | |
1046 | ||
1047 | use strict; | |
1048 | ||
1049 | my $r = Foo->new(8); | |
1050 | $r = Foo->new(0); | |
78f67eb5 | 1051 | |
11e3e2e4 | 1052 | is(($r || 0), 0); |
1554e226 | 1053 | |
6050d10e JP |
1054 | package utf8_o; |
1055 | ||
1056 | use overload | |
1057 | '""' => sub { return $_[0]->{var}; } | |
1058 | ; | |
1059 | ||
1060 | sub new | |
1061 | { | |
1062 | my $class = shift; | |
1063 | my $self = {}; | |
1064 | $self->{var} = shift; | |
1065 | bless $self,$class; | |
1066 | } | |
1067 | ||
1068 | package main; | |
1069 | ||
1070 | ||
1071 | my $utfvar = new utf8_o 200.2.1; | |
11e3e2e4 NC |
1072 | is("$utfvar", 200.2.1); # 223 - stringify |
1073 | is("a$utfvar", "a".200.2.1); # 224 - overload via sv_2pv_flags | |
6050d10e | 1074 | |
446eaa42 | 1075 | # 225..227 -- more %{} tests. Hangs in 5.6.0, okay in later releases. |
41cb1005 JH |
1076 | # Basically this example implements strong encapsulation: if Hderef::import() |
1077 | # were to eval the overload code in the caller's namespace, the privatisation | |
1078 | # would be quite transparent. | |
1079 | package Hderef; | |
1080 | use overload '%{}' => sub { (caller(0))[0] eq 'Foo' ? $_[0] : die "zap" }; | |
1081 | package Foo; | |
1082 | @Foo::ISA = 'Hderef'; | |
1083 | sub new { bless {}, shift } | |
1084 | sub xet { @_ == 2 ? $_[0]->{$_[1]} : | |
1085 | @_ == 3 ? ($_[0]->{$_[1]} = $_[2]) : undef } | |
1086 | package main; | |
1087 | my $a = Foo->new; | |
1088 | $a->xet('b', 42); | |
11e3e2e4 NC |
1089 | is ($a->xet('b'), 42); |
1090 | ok (!defined eval { $a->{b} }); | |
1091 | like ($@, qr/zap/); | |
41cb1005 | 1092 | |
29ddfe35 RD |
1093 | { |
1094 | package t229; | |
1095 | use overload '=' => sub { 42 }, | |
1096 | '++' => sub { my $x = ${$_[0]}; $_[0] }; | |
1097 | sub new { my $x = 42; bless \$x } | |
1098 | ||
1099 | my $warn; | |
1100 | { | |
1101 | local $SIG{__WARN__} = sub { $warn++ }; | |
1102 | my $x = t229->new; | |
1103 | my $y = $x; | |
1104 | eval { $y++ }; | |
1105 | } | |
11e3e2e4 | 1106 | main::ok (!$warn); |
61f33854 RGS |
1107 | } |
1108 | ||
1109 | { | |
1110 | my ($int, $out1, $out2); | |
1111 | { | |
1112 | BEGIN { $int = 0; overload::constant 'integer' => sub {$int++; 17}; } | |
1113 | $out1 = 0; | |
1114 | $out2 = 1; | |
1115 | } | |
11e3e2e4 NC |
1116 | is($int, 2, "#24313"); # 230 |
1117 | is($out1, 17, "#24313"); # 231 | |
1118 | is($out2, 17, "#24313"); # 232 | |
29ddfe35 RD |
1119 | } |
1120 | ||
0bdaccee | 1121 | { |
d411a6a9 | 1122 | package perl31793; |
1b1d102f RD |
1123 | use overload cmp => sub { 0 }; |
1124 | package perl31793_fb; | |
76c43448 | 1125 | use overload cmp => sub { 0 }, fallback => 1; |
d411a6a9 RD |
1126 | package main; |
1127 | my $o = bless [], 'perl31793'; | |
1b1d102f | 1128 | my $of = bless [], 'perl31793_fb'; |
d411a6a9 | 1129 | my $no = bless [], 'no_overload'; |
11e3e2e4 NC |
1130 | like(overload::StrVal(\"scalar"), qr/^SCALAR\(0x[0-9a-f]+\)$/); |
1131 | like(overload::StrVal([]), qr/^ARRAY\(0x[0-9a-f]+\)$/); | |
1132 | like(overload::StrVal({}), qr/^HASH\(0x[0-9a-f]+\)$/); | |
1133 | like(overload::StrVal(sub{1}), qr/^CODE\(0x[0-9a-f]+\)$/); | |
1134 | like(overload::StrVal(\*GLOB), qr/^GLOB\(0x[0-9a-f]+\)$/); | |
1135 | like(overload::StrVal(\$o), qr/^REF\(0x[0-9a-f]+\)$/); | |
0fc92fc6 | 1136 | like(overload::StrVal(qr/a/), qr/^Regexp=REGEXP\(0x[0-9a-f]+\)$/); |
11e3e2e4 NC |
1137 | like(overload::StrVal($o), qr/^perl31793=ARRAY\(0x[0-9a-f]+\)$/); |
1138 | like(overload::StrVal($of), qr/^perl31793_fb=ARRAY\(0x[0-9a-f]+\)$/); | |
1139 | like(overload::StrVal($no), qr/^no_overload=ARRAY\(0x[0-9a-f]+\)$/); | |
d411a6a9 RD |
1140 | } |
1141 | ||
bb4f579a DM |
1142 | { |
1143 | package Numify; | |
1144 | use overload (qw(0+ numify fallback 1)); | |
1145 | ||
1146 | sub new { | |
1147 | my $val = $_[1]; | |
1148 | bless \$val, $_[0]; | |
1149 | } | |
1150 | ||
1151 | sub numify { ${$_[0]} } | |
1152 | } | |
1153 | ||
1154 | # These all check that overloaded values, rather than reference addresses, | |
1155 | # are what are getting tested. | |
0bdaccee NC |
1156 | my ($two, $one, $un, $deux) = map {new Numify $_} 2, 1, 1, 2; |
1157 | my ($ein, $zwei) = (1, 2); | |
1158 | ||
1159 | my %map = (one => 1, un => 1, ein => 1, deux => 2, two => 2, zwei => 2); | |
1160 | foreach my $op (qw(<=> == != < <= > >=)) { | |
1161 | foreach my $l (keys %map) { | |
1162 | foreach my $r (keys %map) { | |
1163 | my $ocode = "\$$l $op \$$r"; | |
1164 | my $rcode = "$map{$l} $op $map{$r}"; | |
1165 | ||
1166 | my $got = eval $ocode; | |
1167 | die if $@; | |
1168 | my $expect = eval $rcode; | |
1169 | die if $@; | |
11e3e2e4 | 1170 | is ($got, $expect, $ocode) or print "# $rcode\n"; |
0bdaccee NC |
1171 | } |
1172 | } | |
1173 | } | |
131b3ad0 DM |
1174 | { |
1175 | # check that overloading works in regexes | |
1176 | { | |
1177 | package Foo493; | |
1178 | use overload | |
1179 | '""' => sub { "^$_[0][0]\$" }, | |
1180 | '.' => sub { | |
1181 | bless [ | |
1182 | $_[2] | |
1183 | ? (ref $_[1] ? $_[1][0] : $_[1]) . ':' .$_[0][0] | |
1184 | : $_[0][0] . ':' . (ref $_[1] ? $_[1][0] : $_[1]) | |
1185 | ], 'Foo493' | |
1186 | }; | |
1187 | } | |
1188 | ||
1189 | my $a = bless [ "a" ], 'Foo493'; | |
11e3e2e4 NC |
1190 | like('a', qr/$a/); |
1191 | like('x:a', qr/x$a/); | |
1192 | like('x:a:=', qr/x$a=$/); | |
1193 | like('x:a:a:=', qr/x$a$a=$/); | |
131b3ad0 DM |
1194 | |
1195 | } | |
1196 | ||
705c898c | 1197 | { |
d4b87e75 BM |
1198 | { |
1199 | package QRonly; | |
1200 | use overload qr => sub { qr/x/ }, fallback => 1; | |
1201 | } | |
1202 | { | |
1203 | my $x = bless [], "QRonly"; | |
1204 | ||
1205 | # like tries to be too clever, and decides that $x-stringified | |
1206 | # doesn't look like a regex | |
1207 | ok("x" =~ $x, "qr-only matches"); | |
e4eea578 | 1208 | ok("y" !~ $x, "qr-only doesn't match what it shouldn't"); |
9f141731 DM |
1209 | ok("x" =~ /^(??{$x})$/, "qr-only with ?? matches"); |
1210 | ok("y" !~ /^(??{$x})$/, "qr-only with ?? doesn't match what it shouldn't"); | |
d4b87e75 | 1211 | ok("xx" =~ /x$x/, "qr-only matches with concat"); |
e4eea578 | 1212 | like("$x", qr/^QRonly=ARRAY/, "qr-only doesn't have string overload"); |
d4b87e75 BM |
1213 | |
1214 | my $qr = bless qr/y/, "QRonly"; | |
1215 | ok("x" =~ $qr, "qr with qr-overload uses overload"); | |
e4eea578 | 1216 | ok("y" !~ $qr, "qr with qr-overload uses overload"); |
497d0a96 DM |
1217 | ok("x" =~ /^(??{$qr})$/, "qr with qr-overload with ?? uses overload"); |
1218 | ok("y" !~ /^(??{$qr})$/, "qr with qr-overload with ?? uses overload"); | |
d4b87e75 BM |
1219 | is("$qr", "".qr/y/, "qr with qr-overload stringify"); |
1220 | ||
1221 | my $rx = $$qr; | |
1222 | ok("y" =~ $rx, "bare rx with qr-overload doesn't overload match"); | |
e4eea578 | 1223 | ok("x" !~ $rx, "bare rx with qr-overload doesn't overload match"); |
9f141731 DM |
1224 | ok("y" =~ /^(??{$rx})$/, "bare rx with qr-overload with ?? doesn't overload match"); |
1225 | ok("x" !~ /^(??{$rx})$/, "bare rx with qr-overload with ?? doesn't overload match"); | |
d4b87e75 BM |
1226 | is("$rx", "".qr/y/, "bare rx with qr-overload stringify"); |
1227 | } | |
1228 | { | |
1229 | package QRandSTR; | |
1230 | use overload qr => sub { qr/x/ }, q/""/ => sub { "y" }; | |
1231 | } | |
1232 | { | |
1233 | my $x = bless [], "QRandSTR"; | |
1234 | ok("x" =~ $x, "qr+str uses qr for match"); | |
e4eea578 | 1235 | ok("y" !~ $x, "qr+str uses qr for match"); |
d4b87e75 BM |
1236 | ok("xx" =~ /x$x/, "qr+str uses qr for match with concat"); |
1237 | is("$x", "y", "qr+str uses str for stringify"); | |
1238 | ||
1239 | my $qr = bless qr/z/, "QRandSTR"; | |
1240 | is("$qr", "y", "qr with qr+str uses str for stringify"); | |
1241 | ok("xx" =~ /x$x/, "qr with qr+str uses qr for match"); | |
1242 | ||
1243 | my $rx = $$qr; | |
1244 | ok("z" =~ $rx, "bare rx with qr+str doesn't overload match"); | |
1245 | is("$rx", "".qr/z/, "bare rx with qr+str doesn't overload stringify"); | |
1246 | } | |
1247 | { | |
1248 | package QRany; | |
1249 | use overload qr => sub { $_[0]->(@_) }; | |
1250 | ||
1251 | package QRself; | |
1252 | use overload qr => sub { $_[0] }; | |
1253 | } | |
1254 | { | |
1255 | my $rx = bless sub { ${ qr/x/ } }, "QRany"; | |
e4eea578 RGS |
1256 | ok("x" =~ $rx, "qr overload accepts a bare rx"); |
1257 | ok("y" !~ $rx, "qr overload accepts a bare rx"); | |
d4b87e75 BM |
1258 | |
1259 | my $str = bless sub { "x" }, "QRany"; | |
1260 | ok(!eval { "x" =~ $str }, "qr overload doesn't accept a string"); | |
e4eea578 | 1261 | like($@, qr/^Overloaded qr did not return a REGEXP/, "correct error"); |
d4b87e75 BM |
1262 | |
1263 | my $oqr = bless qr/z/, "QRandSTR"; | |
1264 | my $oqro = bless sub { $oqr }, "QRany"; | |
e4eea578 | 1265 | ok("z" =~ $oqro, "qr overload doesn't recurse"); |
d4b87e75 BM |
1266 | |
1267 | my $qrs = bless qr/z/, "QRself"; | |
e4eea578 | 1268 | ok("z" =~ $qrs, "qr overload can return self"); |
d4b87e75 BM |
1269 | } |
1270 | { | |
1271 | package STRonly; | |
1272 | use overload q/""/ => sub { "x" }; | |
1273 | ||
1274 | package STRonlyFB; | |
1275 | use overload q/""/ => sub { "x" }, fallback => 1; | |
1276 | } | |
1277 | { | |
1278 | my $fb = bless [], "STRonlyFB"; | |
e4eea578 RGS |
1279 | ok("x" =~ $fb, "qr falls back to \"\""); |
1280 | ok("y" !~ $fb, "qr falls back to \"\""); | |
d4b87e75 BM |
1281 | |
1282 | my $nofb = bless [], "STRonly"; | |
e4eea578 RGS |
1283 | ok("x" =~ $nofb, "qr falls back even without fallback"); |
1284 | ok("y" !~ $nofb, "qr falls back even without fallback"); | |
d4b87e75 BM |
1285 | } |
1286 | } | |
1287 | ||
1288 | { | |
705c898c RH |
1289 | my $twenty_three = 23; |
1290 | # Check that constant overloading propagates into evals | |
1291 | BEGIN { overload::constant integer => sub { 23 } } | |
11e3e2e4 | 1292 | is(eval "17", $twenty_three); |
705c898c | 1293 | } |
dd2eae66 NC |
1294 | |
1295 | { | |
9a0a8507 FC |
1296 | # Check readonliness of constants, whether shared hash key |
1297 | # scalars or no (brought up in bug #109744) | |
c74b32e1 FC |
1298 | BEGIN { overload::constant integer => sub { "main" }; } |
1299 | eval { ${\5} = 'whatever' }; | |
1300 | like $@, qr/^Modification of a read-only value attempted at /, | |
1301 | 'constant overloading makes read-only constants'; | |
1302 | BEGIN { overload::constant integer => sub { __PACKAGE__ }; } | |
1303 | eval { ${\5} = 'whatever' }; | |
9a0a8507 FC |
1304 | like $@, qr/^Modification of a read-only value attempted at /, |
1305 | '... even with shared hash key scalars'; | |
c74b32e1 FC |
1306 | } |
1307 | ||
1308 | { | |
dd2eae66 NC |
1309 | package Sklorsh; |
1310 | use overload | |
1311 | bool => sub { shift->is_cool }; | |
1312 | ||
1313 | sub is_cool { | |
1314 | $_[0]->{name} eq 'cool'; | |
1315 | } | |
1316 | ||
1317 | sub delete { | |
1318 | undef %{$_[0]}; | |
1319 | bless $_[0], 'Brap'; | |
1320 | return 1; | |
1321 | } | |
1322 | ||
1323 | sub delete_with_self { | |
1324 | my $self = shift; | |
1325 | undef %$self; | |
1326 | bless $self, 'Brap'; | |
1327 | return 1; | |
1328 | } | |
1329 | ||
1330 | package Brap; | |
1331 | ||
1332 | 1; | |
1333 | ||
1334 | package main; | |
1335 | ||
1336 | my $obj; | |
1337 | $obj = bless {name => 'cool'}, 'Sklorsh'; | |
1338 | $obj->delete; | |
b0bf6df7 | 1339 | ok(eval {if ($obj) {1}; 1}, $@ || 'reblessed into nonexistent namespace'); |
dd2eae66 NC |
1340 | |
1341 | $obj = bless {name => 'cool'}, 'Sklorsh'; | |
1342 | $obj->delete_with_self; | |
1343 | ok (eval {if ($obj) {1}; 1}, $@); | |
1344 | ||
1345 | my $a = $b = {name => 'hot'}; | |
1346 | bless $b, 'Sklorsh'; | |
1347 | is(ref $a, 'Sklorsh'); | |
1348 | is(ref $b, 'Sklorsh'); | |
1349 | ok(!$b, "Expect overloaded boolean"); | |
1350 | ok(!$a, "Expect overloaded boolean"); | |
1351 | } | |
edbe35ea VP |
1352 | |
1353 | { | |
1354 | package Flrbbbbb; | |
1355 | use overload | |
1356 | bool => sub { shift->{truth} eq 'yes' }, | |
1357 | '0+' => sub { shift->{truth} eq 'yes' ? '1' : '0' }, | |
1358 | '!' => sub { shift->{truth} eq 'no' }, | |
1359 | fallback => 1; | |
1360 | ||
1361 | sub new { my $class = shift; bless { truth => shift }, $class } | |
1362 | ||
1363 | package main; | |
1364 | ||
1365 | my $yes = Flrbbbbb->new('yes'); | |
1366 | my $x; | |
1367 | $x = 1 if $yes; is($x, 1); | |
1368 | $x = 2 unless $yes; is($x, 1); | |
1369 | $x = 3 if !$yes; is($x, 1); | |
1370 | $x = 4 unless !$yes; is($x, 4); | |
1371 | ||
1372 | my $no = Flrbbbbb->new('no'); | |
1373 | $x = 0; | |
1374 | $x = 1 if $no; is($x, 0); | |
1375 | $x = 2 unless $no; is($x, 2); | |
1376 | $x = 3 if !$no; is($x, 3); | |
1377 | $x = 4 unless !$no; is($x, 3); | |
1378 | ||
1379 | $x = 0; | |
1380 | $x = 1 if !$no && $yes; is($x, 1); | |
1381 | $x = 2 unless !$no && $yes; is($x, 1); | |
1382 | $x = 3 if $no || !$yes; is($x, 1); | |
1383 | $x = 4 unless $no || !$yes; is($x, 4); | |
1384 | ||
1385 | $x = 0; | |
1386 | $x = 1 if !$no || !$yes; is($x, 1); | |
1387 | $x = 2 unless !$no || !$yes; is($x, 1); | |
1388 | $x = 3 if !$no && !$yes; is($x, 1); | |
1389 | $x = 4 unless !$no && !$yes; is($x, 4); | |
1390 | } | |
1391 | ||
ea940d6b | 1392 | { |
2372186a NC |
1393 | use Scalar::Util 'weaken'; |
1394 | ||
1395 | package Shklitza; | |
1396 | use overload '""' => sub {"CLiK KLAK"}; | |
1397 | ||
1398 | package Ksshfwoom; | |
2372186a NC |
1399 | |
1400 | package main; | |
1401 | ||
1402 | my ($obj, $ref); | |
1403 | $obj = bless do {my $a; \$a}, 'Shklitza'; | |
1404 | $ref = $obj; | |
1405 | ||
ea940d6b CBW |
1406 | is ("$obj", "CLiK KLAK"); |
1407 | is ("$ref", "CLiK KLAK"); | |
2372186a NC |
1408 | |
1409 | weaken $ref; | |
ea940d6b | 1410 | is ("$ref", "CLiK KLAK"); |
2372186a NC |
1411 | |
1412 | bless $obj, 'Ksshfwoom'; | |
1413 | ||
cd75d542 NC |
1414 | like ($obj, qr/^Ksshfwoom=/); |
1415 | like ($ref, qr/^Ksshfwoom=/); | |
2372186a NC |
1416 | |
1417 | undef $obj; | |
1418 | is ($ref, undef); | |
1419 | } | |
6dd85743 AF |
1420 | |
1421 | { | |
1422 | package bit; | |
1423 | # bit operations have overloadable assignment variants too | |
1424 | ||
1425 | sub new { bless \$_[1], $_[0] } | |
1426 | ||
1427 | use overload | |
1428 | "&=" => sub { bit->new($_[0]->val . ' & ' . $_[1]->val) }, | |
1429 | "^=" => sub { bit->new($_[0]->val . ' ^ ' . $_[1]->val) }, | |
1430 | "|" => sub { bit->new($_[0]->val . ' | ' . $_[1]->val) }, # |= by fallback | |
1431 | ; | |
1432 | ||
1433 | sub val { ${$_[0]} } | |
1434 | ||
1435 | package main; | |
1436 | ||
1437 | my $a = bit->new(my $va = 'a'); | |
1438 | my $b = bit->new(my $vb = 'b'); | |
1439 | ||
1440 | $a &= $b; | |
1441 | is($a->val, 'a & b', "overloaded &= works"); | |
1442 | ||
1443 | my $c = bit->new(my $vc = 'c'); | |
1444 | ||
1445 | $b ^= $c; | |
1446 | is($b->val, 'b ^ c', "overloaded ^= works"); | |
1447 | ||
1448 | my $d = bit->new(my $vd = 'd'); | |
1449 | ||
1450 | $c |= $d; | |
1451 | is($c->val, 'c | d', "overloaded |= (by fallback) works"); | |
1452 | } | |
d11ee47c RD |
1453 | |
1454 | { | |
2ab54efd | 1455 | # comparison operators with nomethod (bug 41546) |
d11ee47c RD |
1456 | my $warning = ""; |
1457 | my $method; | |
1458 | ||
1459 | package nomethod_false; | |
1460 | use overload nomethod => sub { $method = 'nomethod'; 0 }; | |
1461 | ||
1462 | package nomethod_true; | |
1463 | use overload nomethod => sub { $method= 'nomethod'; 'true' }; | |
1464 | ||
1465 | package main; | |
1466 | local $^W = 1; | |
1467 | local $SIG{__WARN__} = sub { $warning = $_[0] }; | |
1468 | ||
1469 | my $f = bless [], 'nomethod_false'; | |
1470 | ($warning, $method) = ("", ""); | |
1471 | is($f eq 'whatever', 0, 'nomethod makes eq return 0'); | |
1472 | is($method, 'nomethod'); | |
1473 | ||
1474 | my $t = bless [], 'nomethod_true'; | |
1475 | ($warning, $method) = ("", ""); | |
1476 | is($t eq 'whatever', 'true', 'nomethod makes eq return "true"'); | |
1477 | is($method, 'nomethod'); | |
1478 | is($warning, "", 'nomethod eq need not return number'); | |
1479 | ||
1480 | eval q{ | |
1481 | package nomethod_false; | |
1482 | use overload cmp => sub { $method = 'cmp'; 0 }; | |
1483 | }; | |
1484 | $f = bless [], 'nomethod_false'; | |
1485 | ($warning, $method) = ("", ""); | |
1486 | ok($f eq 'whatever', 'eq falls back to cmp (nomethod not called)'); | |
1487 | is($method, 'cmp'); | |
1488 | ||
1489 | eval q{ | |
1490 | package nomethod_true; | |
1491 | use overload cmp => sub { $method = 'cmp'; 'true' }; | |
1492 | }; | |
1493 | $t = bless [], 'nomethod_true'; | |
1494 | ($warning, $method) = ("", ""); | |
1495 | ok($t eq 'whatever', 'eq falls back to cmp (nomethod not called)'); | |
1496 | is($method, 'cmp'); | |
1497 | like($warning, qr/isn't numeric/, 'cmp should return number'); | |
1498 | ||
1499 | } | |
2c615c57 NC |
1500 | |
1501 | { | |
2ab54efd MB |
1502 | # nomethod called for '!' after attempted fallback |
1503 | my $nomethod_called = 0; | |
1504 | ||
1505 | package nomethod_not; | |
1506 | use overload nomethod => sub { $nomethod_called = 'yes'; }; | |
1507 | ||
1508 | package main; | |
1509 | my $o = bless [], 'nomethod_not'; | |
1510 | my $res = ! $o; | |
1511 | ||
1512 | is($nomethod_called, 'yes', "nomethod() is called for '!'"); | |
1513 | is($res, 'yes', "nomethod(..., '!') return value propagates"); | |
1514 | } | |
1515 | ||
1516 | { | |
2c615c57 | 1517 | # Subtle bug pre 5.10, as a side effect of the overloading flag being |
b0bf6df7 | 1518 | # stored on the reference rather than the referent. Despite the fact that |
2c615c57 | 1519 | # objects can only be accessed via references (even internally), the |
b0bf6df7 | 1520 | # referent actually knows that it's blessed, not the references. So taking |
2c615c57 NC |
1521 | # a new, unrelated, reference to it gives an object. However, the |
1522 | # overloading-or-not flag was on the reference prior to 5.10, and taking | |
1523 | # a new reference didn't (use to) copy it. | |
1524 | ||
1525 | package kayo; | |
1526 | ||
1527 | use overload '""' => sub {${$_[0]}}; | |
1528 | ||
1529 | sub Pie { | |
1530 | return "$_[0], $_[1]"; | |
1531 | } | |
1532 | ||
1533 | package main; | |
1534 | ||
1535 | my $class = 'kayo'; | |
1536 | my $string = 'bam'; | |
1537 | my $crunch_eth = bless \$string, $class; | |
1538 | ||
1539 | is("$crunch_eth", $string); | |
1540 | is ($crunch_eth->Pie("Meat"), "$string, Meat"); | |
1541 | ||
1542 | my $wham_eth = \$string; | |
1543 | ||
1544 | is("$wham_eth", $string, | |
1545 | 'This reference did not have overloading in 5.8.8 and earlier'); | |
1546 | is ($crunch_eth->Pie("Apple"), "$string, Apple"); | |
1547 | ||
1548 | my $class = ref $wham_eth; | |
1549 | $class =~ s/=.*//; | |
1550 | ||
1551 | # Bless it back into its own class! | |
1552 | bless $wham_eth, $class; | |
1553 | ||
1554 | is("$wham_eth", $string); | |
1555 | is ($crunch_eth->Pie("Blackbird"), "$string, Blackbird"); | |
1556 | } | |
c781a409 RD |
1557 | |
1558 | { | |
1559 | package numify_int; | |
1560 | use overload "0+" => sub { $_[0][0] += 1; 42 }; | |
1561 | package numify_self; | |
1562 | use overload "0+" => sub { $_[0][0]++; $_[0] }; | |
1563 | package numify_other; | |
1564 | use overload "0+" => sub { $_[0][0]++; $_[0][1] = bless [], 'numify_int' }; | |
e28bb1d5 | 1565 | package numify_by_fallback; |
800401ee | 1566 | use overload fallback => 1; |
c781a409 RD |
1567 | |
1568 | package main; | |
1569 | my $o = bless [], 'numify_int'; | |
1570 | is(int($o), 42, 'numifies to integer'); | |
1571 | is($o->[0], 1, 'int() numifies only once'); | |
1572 | ||
1573 | my $aref = []; | |
93521010 | 1574 | my $num_val = int($aref); |
c781a409 RD |
1575 | my $r = bless $aref, 'numify_self'; |
1576 | is(int($r), $num_val, 'numifies to self'); | |
1577 | is($r->[0], 1, 'int() numifies once when returning self'); | |
1578 | ||
1579 | my $s = bless [], 'numify_other'; | |
1580 | is(int($s), 42, 'numifies to numification of other object'); | |
1581 | is($s->[0], 1, 'int() numifies once when returning other object'); | |
1582 | is($s->[1][0], 1, 'returned object numifies too'); | |
e28bb1d5 RD |
1583 | |
1584 | my $m = bless $aref, 'numify_by_fallback'; | |
1585 | is(int($m), $num_val, 'numifies to usual reference value'); | |
800401ee JH |
1586 | is(abs($m), $num_val, 'numifies to usual reference value'); |
1587 | is(-$m, -$num_val, 'numifies to usual reference value'); | |
1588 | is(0+$m, $num_val, 'numifies to usual reference value'); | |
1589 | is($m+0, $num_val, 'numifies to usual reference value'); | |
1590 | is($m+$m, 2*$num_val, 'numifies to usual reference value'); | |
1591 | is(0-$m, -$num_val, 'numifies to usual reference value'); | |
1592 | is(1*$m, $num_val, 'numifies to usual reference value'); | |
9fa8ecf2 | 1593 | is(int($m/1), $num_val, 'numifies to usual reference value'); |
800401ee JH |
1594 | is($m%100, $num_val%100, 'numifies to usual reference value'); |
1595 | is($m**1, $num_val, 'numifies to usual reference value'); | |
1596 | ||
1597 | is(abs($aref), $num_val, 'abs() of ref'); | |
1598 | is(-$aref, -$num_val, 'negative of ref'); | |
1599 | is(0+$aref, $num_val, 'ref addition'); | |
1600 | is($aref+0, $num_val, 'ref addition'); | |
1601 | is($aref+$aref, 2*$num_val, 'ref addition'); | |
1602 | is(0-$aref, -$num_val, 'subtraction of ref'); | |
1603 | is(1*$aref, $num_val, 'multiplicaton of ref'); | |
9fa8ecf2 | 1604 | is(int($aref/1), $num_val, 'division of ref'); |
800401ee JH |
1605 | is($aref%100, $num_val%100, 'modulo of ref'); |
1606 | is($aref**1, $num_val, 'exponentiation of ref'); | |
c781a409 | 1607 | } |
800401ee | 1608 | |
49c95d58 RD |
1609 | { |
1610 | package CopyConstructorFallback; | |
1611 | use overload | |
1612 | '++' => sub { "$_[0]"; $_[0] }, | |
1613 | fallback => 1; | |
1614 | sub new { bless {} => shift } | |
1615 | ||
1616 | package main; | |
1617 | ||
1618 | my $o = CopyConstructorFallback->new; | |
1619 | my $x = $o++; # would segfault | |
1620 | my $y = ++$o; | |
1621 | is($x, $o, "copy constructor falls back to assignment (postinc)"); | |
1622 | is($y, $o, "copy constructor falls back to assignment (preinc)"); | |
1623 | } | |
1624 | ||
6f1401dc DM |
1625 | # only scalar 'x' should currently overload |
1626 | ||
1627 | { | |
1628 | package REPEAT; | |
1629 | ||
1630 | my ($x,$n, $nm); | |
1631 | ||
1632 | use overload | |
1633 | 'x' => sub { $x++; 1 }, | |
1634 | '0+' => sub { $n++; 1 }, | |
1635 | 'nomethod' => sub { $nm++; 1 }, | |
1636 | 'fallback' => 0, | |
1637 | ; | |
1638 | ||
1639 | my $s = bless {}; | |
1640 | ||
1641 | package main; | |
1642 | ||
1643 | my @a; | |
1644 | my $count = 3; | |
1645 | ||
1646 | ($x,$n,$nm) = (0,0,0); | |
1647 | @a = ((1,2,$s) x $count); | |
1648 | is("$x-$n-$nm", "0-0-0", 'repeat 1'); | |
1649 | ||
1650 | ($x,$n,$nm) = (0,0,0); | |
1651 | @a = ((1,$s,3) x $count); | |
1652 | is("$x-$n-$nm", "0-0-0", 'repeat 2'); | |
1653 | ||
1654 | ($x,$n,$nm) = (0,0,0); | |
1655 | @a = ((1,2,3) x $s); | |
1656 | is("$x-$n-$nm", "0-1-0", 'repeat 3'); | |
1657 | } | |
1658 | ||
1659 | ||
1660 | ||
1661 | # RT #57012: magic items need to have mg_get() called before testing for | |
1662 | # overload. Lack of this means that overloaded values returned by eg a | |
1663 | # tied array didn't call overload methods. | |
1664 | # We test here both a tied array and scalar, since the implementation of | |
1665 | # tied arrays (and hashes) is such that in rvalue context, mg_get is | |
1666 | # called prior to executing the op, while it isn't for a tied scalar. | |
a02ec77a DM |
1667 | # We also check that return values are correctly tainted. |
1668 | # We try against two overload packages; one has all expected methods, the | |
1669 | # other uses only fallback methods. | |
6f1401dc DM |
1670 | |
1671 | { | |
1672 | ||
a02ec77a DM |
1673 | # @tests holds a list of test cases. Each elem is an array ref with |
1674 | # the following entries: | |
1675 | # | |
1676 | # * the value that the overload method should return | |
1677 | # | |
1678 | # * the expression to be evaled. %s is replaced with the | |
1679 | # variable being tested ($ta[0], $ts, or $plain) | |
1680 | # | |
1681 | # * a string listing what functions we expect to be called. | |
1682 | # Each method appends its name in parentheses, so "(=)(+)" means | |
1683 | # we expect the copy constructor and then the add method to be | |
1684 | # called. | |
1685 | # | |
1686 | # * like above, but what should be called for the fallback-only test | |
1687 | # (in this case, nomethod() identifies itself as "(NM:*)" where * | |
1688 | # is the op). If this value is undef, fallback tests are skipped. | |
1689 | # | |
1690 | # * An array ref of expected counts of calls to FETCH/STORE. | |
1691 | # The first three values are: | |
1692 | # 1. the expected number of FETCHs for a tied array | |
1693 | # 2. the expected number of FETCHs for a tied scalar | |
1694 | # 3. the expected number of STOREs | |
1695 | # If there are a further three elements present, then | |
1696 | # these represent the expected counts for the fallback | |
1697 | # version of the tests. If absent, they are assumed to | |
1698 | # be the same as for the full method test | |
1699 | # | |
1700 | # * Under the taint version of the tests, whether we expect | |
1701 | # the result to be tainted (for example comparison ops | |
1702 | # like '==' don't return a tainted value, even if their | |
1703 | # args are. | |
1704 | my @tests; | |
1705 | ||
6f1401dc DM |
1706 | my %subs; |
1707 | my $funcs; | |
1708 | my $use_int; | |
1709 | ||
1710 | BEGIN { | |
1711 | # A note on what methods to expect to be called, and | |
1712 | # how many times FETCH/STORE is called: | |
1713 | # | |
1714 | # Mutating ops (+=, ++ etc) trigger a copy ('='), since | |
7f1e438c | 1715 | # the code can't distinguish between something that's been copied: |
6f1401dc DM |
1716 | # $a = foo->new(0); $b = $a; refcnt($$b) == 2 |
1717 | # and overloaded objects stored in ties which will have extra | |
1718 | # refcounts due to the tied_obj magic and entries on the tmps | |
1719 | # stack when returning from FETCH etc. So we always copy. | |
1720 | ||
1721 | # This accounts for a '=', and an extra STORE. | |
1722 | # We also have a FETCH returning the final value from the eval, | |
1723 | # plus a FETCH in the overload subs themselves: ($_[0][0]) | |
7f1e438c | 1724 | # triggers one. However, tied aggregates have a mechanism to prevent |
6f1401dc DM |
1725 | # multiple fetches between STOREs, which means that the tied |
1726 | # hash skips doing a FETCH during '='. | |
1727 | ||
a02ec77a DM |
1728 | for (qw(+ - * / % ** << >> & | ^)) { |
1729 | my $op = $_; | |
1730 | $op = '%%' if $op eq '%'; | |
1731 | my $e = "%s $op= 3"; | |
6f1401dc DM |
1732 | $subs{"$_="} = $e; |
1733 | # ARRAY FETCH: initial, sub+=, eval-return, | |
1734 | # SCALAR FETCH: initial, sub=, sub+=, eval-return, | |
1735 | # STORE: copy, mutator | |
a02ec77a DM |
1736 | push @tests, [ 18, $e, "(=)($_=)", "(=)(NM:$_=)", [ 3, 4, 2 ], 1 ]; |
1737 | ||
1738 | $subs{$_} = | |
1739 | "do { my \$arg = %s; \$_[2] ? (3 $op \$arg) : (\$arg $op 3) }"; | |
6f1401dc DM |
1740 | # ARRAY FETCH: initial |
1741 | # SCALAR FETCH: initial eval-return, | |
a02ec77a DM |
1742 | push @tests, [ 18, "%s $op 3", "($_)", "(NM:$_)", [ 1, 2, 0 ], 1 ]; |
1743 | push @tests, [ 18, "3 $op %s", "($_)", "(NM:$_)", [ 1, 2, 0 ], 1 ]; | |
6f1401dc | 1744 | } |
a02ec77a DM |
1745 | |
1746 | # these use string fallback rather than nomethod | |
1747 | for (qw(x .)) { | |
1748 | my $op = $_; | |
1749 | my $e = "%s $op= 3"; | |
1750 | $subs{"$_="} = $e; | |
1751 | # For normal case: | |
1752 | # ARRAY FETCH: initial, sub+=, eval-return, | |
1753 | # SCALAR FETCH: initial, sub=, sub+=, eval-return, | |
1754 | # STORE: copy, mutator | |
1755 | # for fallback, we just stringify, so eval-return and copy skipped | |
c5aa2872 DM |
1756 | |
1757 | push @tests, [ 18, $e, "(=)($_=)", '("")', | |
1758 | [ 3, 4, 2, 2, 3, 1 ], 1 ]; | |
a02ec77a DM |
1759 | |
1760 | $subs{$_} = | |
1761 | "do { my \$arg = %s; \$_[2] ? (3 $op \$arg) : (\$arg $op 3) }"; | |
1762 | # ARRAY FETCH: initial | |
1763 | # SCALAR FETCH: initial eval-return, | |
3bc4ee4c DM |
1764 | # with fallback, we just stringify, so eval-return skipped, |
1765 | # but an extra FETCH happens in sub"", except for 'x', | |
1766 | # which passes a copy of the RV to sub"", avoiding the | |
1767 | # second FETCH | |
1768 | ||
1769 | push @tests, [ 18, "%s $op 3", "($_)", '("")', | |
1770 | [ 1, 2, 0, 1, ($_ eq '.' ? 2 : 1), 0 ], 1 ]; | |
1771 | next if $_ eq 'x'; # repeat only overloads on LHS | |
1772 | push @tests, [ 18, "3 $op %s", "($_)", '("")', | |
1773 | [ 1, 2, 0, 1, 2, 0 ], 1 ]; | |
a02ec77a DM |
1774 | } |
1775 | ||
6f1401dc DM |
1776 | for (qw(++ --)) { |
1777 | my $pre = "$_%s"; | |
1778 | my $post = "%s$_"; | |
1779 | $subs{$_} = $pre; | |
a02ec77a | 1780 | push @tests, |
6f1401dc DM |
1781 | # ARRAY FETCH: initial, sub+=, eval-return, |
1782 | # SCALAR FETCH: initial, sub=, sub+=, eval-return, | |
1783 | # STORE: copy, mutator | |
a02ec77a | 1784 | [ 18, $pre, "(=)($_)(\"\")", "(=)(NM:$_)(\"\")", [ 3, 4, 2 ], 1 ], |
6f1401dc DM |
1785 | # ARRAY FETCH: initial, sub+= |
1786 | # SCALAR FETCH: initial, sub=, sub+= | |
1787 | # STORE: copy, mutator | |
a02ec77a | 1788 | [ 18, $post, "(=)($_)(\"\")", "(=)(NM:$_)(\"\")", [ 2, 3, 2 ], 1 ]; |
6f1401dc DM |
1789 | } |
1790 | ||
1791 | # For the non-mutator ops, we have a initial FETCH, | |
1792 | # an extra FETCH within the sub itself for the scalar option, | |
1793 | # and no STOREs | |
1794 | ||
a02ec77a | 1795 | for (qw(< <= > >= == != lt le gt ge eq ne)) { |
6f1401dc DM |
1796 | my $e = "%s $_ 3"; |
1797 | $subs{$_} = $e; | |
a02ec77a DM |
1798 | push @tests, [ 3, $e, "($_)", "(NM:$_)", [ 1, 2, 0 ], 0 ]; |
1799 | } | |
1800 | for (qw(<=> cmp)) { | |
1801 | my $e = "%s $_ 3"; | |
1802 | $subs{$_} = $e; | |
1803 | push @tests, [ 3, $e, "($_)", "(NM:$_)", [ 1, 2, 0 ], 1 ]; | |
6f1401dc DM |
1804 | } |
1805 | for (qw(atan2)) { | |
1806 | my $e = "$_ %s, 3"; | |
1807 | $subs{$_} = $e; | |
a02ec77a | 1808 | push @tests, [ 18, $e, "($_)", "(NM:$_)", [ 1, 2, 0 ], 1 ]; |
6f1401dc | 1809 | } |
a02ec77a | 1810 | for (qw(cos sin exp abs log sqrt int ~)) { |
6f1401dc DM |
1811 | my $e = "$_(%s)"; |
1812 | $subs{$_} = $e; | |
a02ec77a DM |
1813 | push @tests, [ 1.23, $e, "($_)", |
1814 | ($_ eq 'int' ? '(0+)' : "(NM:$_)") , [ 1, 2, 0 ], 1 ]; | |
1815 | } | |
1816 | for (qw(!)) { | |
1817 | my $e = "$_(%s)"; | |
1818 | $subs{$_} = $e; | |
1819 | push @tests, [ 1.23, $e, "($_)", '(0+)', [ 1, 2, 0 ], 0 ]; | |
6f1401dc DM |
1820 | } |
1821 | for (qw(-)) { | |
1822 | my $e = "$_(%s)"; | |
1823 | $subs{neg} = $e; | |
a02ec77a | 1824 | push @tests, [ 18, $e, '(neg)', '(NM:neg)', [ 1, 2, 0 ], 1 ]; |
6f1401dc DM |
1825 | } |
1826 | my $e = '(%s) ? 1 : 0'; | |
1827 | $subs{bool} = $e; | |
a02ec77a | 1828 | push @tests, [ 18, $e, '(bool)', '(0+)', [ 1, 2, 0 ], 0 ]; |
6f1401dc DM |
1829 | |
1830 | # note: this is testing unary qr, not binary =~ | |
a02ec77a | 1831 | $subs{qr} = '(qr/%s/)'; |
f3ec07c7 | 1832 | push @tests, [ "abc", '"abc" =~ (%s)', '(qr)', '("")', [ 1, 2, 0 ], 0 ]; |
540576b3 FC |
1833 | push @tests, [ chr 256, 'chr(256) =~ (%s)', '(qr)', '("")', |
1834 | [ 1, 2, 0 ], 0 ]; | |
6f1401dc DM |
1835 | |
1836 | $e = '"abc" ~~ (%s)'; | |
1837 | $subs{'~~'} = $e; | |
7896dde7 | 1838 | push @tests, [ "abc", $e, '(~~)', '(NM:~~)', [ 1, 1, 0 ], 0 ]; |
6f1401dc DM |
1839 | |
1840 | $subs{'-X'} = 'do { my $f = (%s);' | |
1841 | . '$_[1] eq "r" ? (-r ($f)) :' | |
1842 | . '$_[1] eq "e" ? (-e ($f)) :' | |
1843 | . '$_[1] eq "f" ? (-f ($f)) :' | |
1844 | . '$_[1] eq "l" ? (-l ($f)) :' | |
1845 | . '$_[1] eq "t" ? (-t ($f)) :' | |
1846 | . '$_[1] eq "T" ? (-T ($f)) : 0;}'; | |
7f1e438c | 1847 | # Note - we don't care what these file tests return, as |
6f1401dc DM |
1848 | # long as the tied and untied versions return the same value. |
1849 | # The flags below are chosen to test all uses of tryAMAGICftest_MG | |
1850 | for (qw(r e f l t T)) { | |
40c852de | 1851 | push @tests, [ 'TEST', "-$_ (%s)", '(-X)', '("")', [ 1, 2, 0 ], 0 ]; |
6f1401dc DM |
1852 | } |
1853 | ||
1854 | $subs{'${}'} = '%s'; | |
a02ec77a | 1855 | push @tests, [ do {my $s=99; \$s}, '${%s}', '(${})', undef, [ 1, 1, 0 ], 0 ]; |
6f1401dc DM |
1856 | |
1857 | # we skip testing '@{}' here because too much of this test | |
a02ec77a | 1858 | # framework involves array dereferences! |
6f1401dc DM |
1859 | |
1860 | $subs{'%{}'} = '%s'; | |
a02ec77a | 1861 | push @tests, [ {qw(a 1 b 2 c 3)}, 'join "", sort keys %%{%s}', |
9026059d | 1862 | '(%{})', undef, [ 1, 1, 0 ], 0 ]; |
6f1401dc DM |
1863 | |
1864 | $subs{'&{}'} = '%s'; | |
a02ec77a DM |
1865 | push @tests, [ sub {99}, 'do {&{%s} for 1,2}', |
1866 | '(&{})(&{})', undef, [ 2, 2, 0 ], 0 ]; | |
6f1401dc DM |
1867 | |
1868 | our $RT57012A = 88; | |
1869 | our $RT57012B; | |
1870 | $subs{'*{}'} = '%s'; | |
a02ec77a DM |
1871 | push @tests, [ \*RT57012A, '*RT57012B = *{%s}; our $RT57012B', |
1872 | '(*{})', undef, [ 1, 1, 0 ], 0 ]; | |
6f1401dc | 1873 | |
9426e1a5 DM |
1874 | my $iter_text = ("some random text\n" x 100) . $^X; |
1875 | open my $iter_fh, '<', \$iter_text | |
1876 | or die "open of \$iter_text gave ($!)\n"; | |
1877 | $subs{'<>'} = '<$iter_fh>'; | |
1878 | push @tests, [ $iter_fh, '<%s>', '(<>)', undef, [ 1, 1, 0 ], 1 ]; | |
bb4f579a DM |
1879 | push @tests, [ $iter_fh, |
1880 | 'local *CORE::GLOBAL::glob = sub {}; eval q|<%s>|', | |
1881 | '(<>)', undef, [ 1, 1, 0 ], 1 ]; | |
6f1401dc | 1882 | |
895b760f DM |
1883 | # eval should do tie, overload on its arg before checking taint */ |
1884 | push @tests, [ '1;', 'eval q(eval %s); $@ =~ /Insecure/', | |
1885 | '("")', '("")', [ 1, 2, 0 ], 0 ]; | |
1886 | ||
1887 | ||
6f1401dc | 1888 | for my $sub (keys %subs) { |
0f539b13 | 1889 | no warnings 'experimental::smartmatch'; |
6f1401dc DM |
1890 | my $term = $subs{$sub}; |
1891 | my $t = sprintf $term, '$_[0][0]'; | |
a02ec77a | 1892 | my $e ="sub { \$funcs .= '($sub)'; my \$r; if (\$use_int) {" |
6f1401dc | 1893 | . "use integer; \$r = ($t) } else { \$r = ($t) } \$r }"; |
a02ec77a DM |
1894 | $subs{$sub} = eval $e; |
1895 | die "Compiling sub gave error:\n<$e>\n<$@>\n" if $@; | |
6f1401dc DM |
1896 | } |
1897 | } | |
1898 | ||
1899 | my $fetches; | |
1900 | my $stores; | |
1901 | ||
1902 | package RT57012_OV; | |
1903 | ||
6f1401dc DM |
1904 | use overload |
1905 | %subs, | |
a02ec77a DM |
1906 | "=" => sub { $funcs .= '(=)'; bless [ $_[0][0] ] }, |
1907 | '0+' => sub { $funcs .= '(0+)'; 0 + $_[0][0] }, | |
1908 | '""' => sub { $funcs .= '("")'; "$_[0][0]" }, | |
1909 | ; | |
1910 | ||
1911 | package RT57012_OV_FB; # only contains fallback conversion functions | |
1912 | ||
1913 | use overload | |
1914 | "=" => sub { $funcs .= '(=)'; bless [ $_[0][0] ] }, | |
1915 | '0+' => sub { $funcs .= '(0+)'; 0 + $_[0][0] }, | |
1916 | '""' => sub { $funcs .= '("")'; "$_[0][0]" }, | |
1917 | "nomethod" => sub { | |
1918 | $funcs .= "(NM:$_[3])"; | |
1919 | my $e = defined($_[1]) | |
1920 | ? $_[3] eq 'atan2' | |
1921 | ? $_[2] | |
1922 | ? "atan2(\$_[1],\$_[0][0])" | |
1923 | : "atan2(\$_[0][0],\$_[1])" | |
1924 | : $_[2] | |
1925 | ? "\$_[1] $_[3] \$_[0][0]" | |
1926 | : "\$_[0][0] $_[3] \$_[1]" | |
1927 | : $_[3] eq 'neg' | |
1928 | ? "-\$_[0][0]" | |
1929 | : "$_[3](\$_[0][0])"; | |
1930 | my $r; | |
0f539b13 | 1931 | no warnings 'experimental::smartmatch'; |
a02ec77a DM |
1932 | if ($use_int) { |
1933 | use integer; $r = eval $e; | |
1934 | } | |
1935 | else { | |
1936 | $r = eval $e; | |
1937 | } | |
1938 | ::diag("eval of nomethod <$e> gave <$@>") if $@; | |
1939 | $r; | |
1940 | } | |
1941 | ||
6f1401dc DM |
1942 | ; |
1943 | ||
1944 | package RT57012_TIE_S; | |
1945 | ||
1946 | my $tie_val; | |
a02ec77a | 1947 | sub TIESCALAR { bless [ bless [ $tie_val ], $_[1] ] } |
6f1401dc DM |
1948 | sub FETCH { $fetches++; $_[0][0] } |
1949 | sub STORE { $stores++; $_[0][0] = $_[1] } | |
1950 | ||
1951 | package RT57012_TIE_A; | |
1952 | ||
1953 | sub TIEARRAY { bless [] } | |
1954 | sub FETCH { $fetches++; $_[0][0] } | |
1955 | sub STORE { $stores++; $_[0][$_[1]] = $_[2] } | |
1956 | ||
1957 | package main; | |
1958 | ||
a02ec77a DM |
1959 | for my $test (@tests) { |
1960 | my ($val, $sub_term, $exp_funcs, $exp_fb_funcs, | |
1961 | $exp_counts, $exp_taint) = @$test; | |
1962 | ||
1963 | my $tainted_val; | |
1964 | { | |
1965 | # create tainted version of $val (unless its a ref) | |
1966 | my $t = substr($^X,0,0); | |
1967 | my $t0 = $t."0"; | |
1968 | my $val1 = $val; # use a copy to avoid stringifying original | |
1969 | $tainted_val = ref($val1) ? $val : | |
1970 | ($val1 =~ /^[\d\.]+$/) ? $val+$t0 : $val.$t; | |
1971 | } | |
1972 | $tie_val = $tainted_val; | |
6f1401dc | 1973 | |
6f1401dc DM |
1974 | for my $int ('', 'use integer; ') { |
1975 | $use_int = ($int ne ''); | |
a02ec77a DM |
1976 | my $plain = $tainted_val; |
1977 | my $plain_term = $int . sprintf $sub_term, '$plain'; | |
0f539b13 | 1978 | my $exp = do {no warnings 'experimental::smartmatch'; eval $plain_term }; |
a02ec77a DM |
1979 | diag("eval of plain_term <$plain_term> gave <$@>") if $@; |
1980 | is(tainted($exp), $exp_taint, | |
1981 | "<$plain_term> taint of expected return"); | |
1982 | ||
1983 | for my $ov_pkg (qw(RT57012_OV RT57012_OV_FB)) { | |
a02ec77a DM |
1984 | next if $ov_pkg eq 'RT57012_OV_FB' |
1985 | and not defined $exp_fb_funcs; | |
1986 | my ($exp_fetch_a, $exp_fetch_s, $exp_store) = | |
1987 | ($ov_pkg eq 'RT57012_OV' || @$exp_counts < 4) | |
1988 | ? @$exp_counts[0,1,2] | |
1989 | : @$exp_counts[3,4,5]; | |
1990 | ||
1991 | tie my $ts, 'RT57012_TIE_S', $ov_pkg; | |
6f1401dc | 1992 | tie my @ta, 'RT57012_TIE_A'; |
a02ec77a DM |
1993 | $ta[0] = bless [ $tainted_val ], $ov_pkg; |
1994 | my $oload = bless [ $tainted_val ], $ov_pkg; | |
1995 | ||
9426e1a5 DM |
1996 | for my $var ('$ta[0]', '$ts', '$oload', |
1997 | ($sub_term eq '<%s>' ? '${ts}' : ()) | |
1998 | ) { | |
a02ec77a DM |
1999 | |
2000 | $funcs = ''; | |
2001 | $fetches = 0; | |
2002 | $stores = 0; | |
2003 | ||
2004 | my $res_term = $int . sprintf $sub_term, $var; | |
2005 | my $desc = "<$res_term> $ov_pkg" ; | |
0f539b13 | 2006 | my $res = do { no warnings 'experimental::smartmatch'; eval $res_term }; |
a02ec77a | 2007 | diag("eval of res_term $desc gave <$@>") if $@; |
7f1e438c | 2008 | # uniquely, the inc/dec ops return the original |
a02ec77a DM |
2009 | # ref rather than a copy, so stringify it to |
2010 | # find out if its tainted | |
2011 | $res = "$res" if $res_term =~ /\+\+|--/; | |
2012 | is(tainted($res), $exp_taint, | |
2013 | "$desc taint of result return"); | |
a02ec77a DM |
2014 | is($res, $exp, "$desc return value"); |
2015 | my $fns =($ov_pkg eq 'RT57012_OV_FB') | |
2016 | ? $exp_fb_funcs : $exp_funcs; | |
2017 | if ($var eq '$oload' && $res_term !~ /oload(\+\+|--)/) { | |
2018 | # non-tied overloading doesn't trigger a copy | |
2019 | # except for post inc/dec | |
2020 | $fns =~ s/^\(=\)//; | |
2021 | } | |
2022 | is($funcs, $fns, "$desc methods called"); | |
2023 | next if $var eq '$oload'; | |
2024 | my $exp_fetch = ($var eq '$ts') ? | |
2025 | $exp_fetch_s : $exp_fetch_a; | |
2026 | is($fetches, $exp_fetch, "$desc FETCH count"); | |
2027 | is($stores, $exp_store, "$desc STORE count"); | |
2028 | ||
2029 | } | |
2030 | ||
6f1401dc DM |
2031 | } |
2032 | } | |
2033 | } | |
2034 | } | |
2035 | ||
25222ff9 FC |
2036 | # Test overload from the main package |
2037 | fresh_perl_is | |
2038 | '$^W = 1; use overload q\""\ => sub {"ning"}; print bless []', | |
2039 | 'ning', | |
2040 | { switches => ['-wl'], stderr => 1 }, | |
2041 | 'use overload from the main package' | |
2042 | ; | |
2043 | ||
56f08af2 FC |
2044 | { |
2045 | package blessed_methods; | |
2046 | use overload '+' => sub {}; | |
2047 | bless overload::Method __PACKAGE__,'+'; | |
2048 | eval { overload::Method __PACKAGE__,'+' }; | |
2049 | ::is($@, '', 'overload::Method and blessed overload methods'); | |
2050 | } | |
2051 | ||
bf5522a1 MB |
2052 | { |
2053 | # fallback to 'cmp' and '<=>' with heterogeneous operands | |
2054 | # [perl #71286] | |
2055 | my $not_found = 'no method found'; | |
2056 | my $used = 0; | |
2057 | package CmpBase; | |
2058 | sub new { | |
2059 | my $n = $_[1] || 0; | |
2060 | bless \$n, ref $_[0] || $_[0]; | |
2061 | } | |
2062 | sub cmp { | |
2063 | $used = \$_[0]; | |
2064 | (${$_[0]} <=> ${$_[1]}) * ($_[2] ? -1 : 1); | |
2065 | } | |
2066 | ||
2067 | package NCmp; | |
383fb793 | 2068 | use parent '-norequire', 'CmpBase'; |
bf5522a1 MB |
2069 | use overload '<=>' => 'cmp'; |
2070 | ||
2071 | package SCmp; | |
383fb793 | 2072 | use parent '-norequire', 'CmpBase'; |
bf5522a1 MB |
2073 | use overload 'cmp' => 'cmp'; |
2074 | ||
2075 | package main; | |
2076 | my $n = NCmp->new(5); | |
2077 | my $s = SCmp->new(3); | |
2078 | my $res; | |
2079 | ||
2080 | eval { $res = $n > $s; }; | |
2081 | $res = $not_found if $@ =~ /$not_found/; | |
2082 | is($res, 1, 'A>B using A<=> when B overloaded, no B<=>'); | |
2083 | ||
2084 | eval { $res = $s < $n; }; | |
2085 | $res = $not_found if $@ =~ /$not_found/; | |
2086 | is($res, 1, 'A<B using B<=> when A overloaded, no A<=>'); | |
2087 | ||
2088 | eval { $res = $s lt $n; }; | |
2089 | $res = $not_found if $@ =~ /$not_found/; | |
2090 | is($res, 1, 'A lt B using A:cmp when B overloaded, no B:cmp'); | |
2091 | ||
2092 | eval { $res = $n gt $s; }; | |
2093 | $res = $not_found if $@ =~ /$not_found/; | |
2094 | is($res, 1, 'A gt B using B:cmp when A overloaded, no A:cmp'); | |
2095 | ||
2096 | my $o = NCmp->new(9); | |
2097 | $res = $n < $o; | |
2098 | is($used, \$n, 'A < B uses <=> from A in preference to B'); | |
2099 | ||
2100 | my $t = SCmp->new(7); | |
2101 | $res = $s lt $t; | |
2102 | is($used, \$s, 'A lt B uses cmp from A in preference to B'); | |
2103 | } | |
2104 | ||
2105 | { | |
2106 | # Combinatorial testing of 'fallback' and 'nomethod' | |
2107 | # [perl #71286] | |
2108 | package NuMB; | |
2109 | use overload '0+' => sub { ${$_[0]}; }, | |
2110 | '""' => 'str'; | |
2111 | sub new { | |
2112 | my $self = shift; | |
2113 | my $n = @_ ? shift : 0; | |
2114 | bless my $obj = \$n, ref $self || $self; | |
2115 | } | |
2116 | sub str { | |
2117 | no strict qw/refs/; | |
2118 | my $s = "(${$_[0]} "; | |
2119 | $s .= "nomethod, " if defined ${ref($_[0]).'::(nomethod'}; | |
2120 | my $fb = ${ref($_[0]).'::()'}; | |
2121 | $s .= "fb=" . (defined $fb ? 0 + $fb : 'undef') . ")"; | |
2122 | } | |
2123 | sub nomethod { "${$_[0]}.nomethod"; } | |
2124 | ||
2125 | # create classes for tests | |
2126 | package main; | |
2127 | my @falls = (0, 'undef', 1); | |
2128 | my @nomethods = ('', 'nomethod'); | |
2129 | my $not_found = 'no method found'; | |
2130 | for my $fall (@falls) { | |
2131 | for my $nomethod (@nomethods) { | |
2132 | my $nomethod_decl = $nomethod | |
2133 | ? $nomethod . "=>'nomethod'," : ''; | |
2134 | eval qq{ | |
2135 | package NuMB$fall$nomethod; | |
383fb793 | 2136 | use parent '-norequire', qw/NuMB/; |
bf5522a1 MB |
2137 | use overload $nomethod_decl |
2138 | fallback => $fall; | |
2139 | }; | |
2140 | } | |
2141 | } | |
2142 | ||
2143 | # operation and precedence of 'fallback' and 'nomethod' | |
2144 | # for all combinations with 2 overloaded operands | |
2145 | for my $nomethod2 (@nomethods) { | |
2146 | for my $nomethod1 (@nomethods) { | |
2147 | for my $fall2 (@falls) { | |
2148 | my $pack2 = "NuMB$fall2$nomethod2"; | |
2149 | for my $fall1 (@falls) { | |
2150 | my $pack1 = "NuMB$fall1$nomethod1"; | |
2151 | my ($test, $out, $exp); | |
2152 | eval qq{ | |
2153 | my \$x = $pack1->new(2); | |
2154 | my \$y = $pack2->new(3); | |
2155 | \$test = "\$x" . ' * ' . "\$y"; | |
2156 | \$out = \$x * \$y; | |
2157 | }; | |
2158 | $out = $not_found if $@ =~ /$not_found/; | |
2159 | $exp = $nomethod1 ? '2.nomethod' : | |
2160 | $nomethod2 ? '3.nomethod' : | |
2161 | $fall1 eq '1' && $fall2 eq '1' ? 6 | |
2162 | : $not_found; | |
2163 | is($out, $exp, "$test --> $exp"); | |
2164 | } | |
2165 | } | |
2166 | } | |
2167 | } | |
2168 | ||
2169 | # operation of 'fallback' and 'nomethod' | |
2170 | # where the other operand is not overloaded | |
2171 | for my $nomethod (@nomethods) { | |
2172 | for my $fall (@falls) { | |
2173 | my ($test, $out, $exp); | |
2174 | eval qq{ | |
2175 | my \$x = NuMB$fall$nomethod->new(2); | |
2176 | \$test = "\$x" . ' * 3'; | |
2177 | \$out = \$x * 3; | |
2178 | }; | |
2179 | $out = $not_found if $@ =~ /$not_found/; | |
2180 | $exp = $nomethod ? '2.nomethod' : | |
2181 | $fall eq '1' ? 6 | |
2182 | : $not_found; | |
2183 | is($out, $exp, "$test --> $exp"); | |
2184 | ||
2185 | eval qq{ | |
2186 | my \$x = NuMB$fall$nomethod->new(2); | |
2187 | \$test = '3 * ' . "\$x"; | |
2188 | \$out = 3 * \$x; | |
2189 | }; | |
2190 | $out = $not_found if $@ =~ /$not_found/; | |
2191 | is($out, $exp, "$test --> $exp"); | |
2192 | } | |
2193 | } | |
2194 | } | |
2195 | ||
bff33ce0 DM |
2196 | # since 5.6 overloaded <> was leaving an extra arg on the stack! |
2197 | ||
2198 | { | |
2199 | package Iter1; | |
2200 | use overload '<>' => sub { 11 }; | |
2201 | package main; | |
2202 | my $a = bless [], 'Iter1'; | |
2203 | my $x; | |
2204 | my @a = (10, ($x = <$a>), 12); | |
2205 | is ($a[0], 10, 'Iter1: a[0]'); | |
2206 | is ($a[1], 11, 'Iter1: a[1]'); | |
2207 | is ($a[2], 12, 'Iter1: a[2]'); | |
2208 | @a = (10, ($x .= <$a>), 12); | |
2209 | is ($a[0], 10, 'Iter1: a[0] concat'); | |
2210 | is ($a[1], 1111, 'Iter1: a[1] concat'); | |
2211 | is ($a[2], 12, 'Iter1: a[2] concat'); | |
2212 | } | |
bf5522a1 | 2213 | |
5e2699c2 FC |
2214 | # Some tests for error messages |
2215 | { | |
2216 | package Justus; | |
2217 | use overload '+' => 'justice'; | |
bb4f579a | 2218 | eval {"".bless[]}; |
5e2699c2 | 2219 | ::like $@, qr/^Can't resolve method "justice" overloading "\+" in p(?x: |
f0e9f182 | 2220 | )ackage "Justus" at /, |
5e2699c2 FC |
2221 | 'Error message when explicitly named overload method does not exist'; |
2222 | ||
2223 | package JustUs; | |
2224 | our @ISA = 'JustYou'; | |
2225 | package JustYou { use overload '+' => 'injustice'; } | |
2226 | "JustUs"->${\"(+"}; | |
bb4f579a | 2227 | eval {"".bless []}; |
5e2699c2 | 2228 | ::like $@, qr/^Stub found while resolving method "\?{3}" overloadin(?x: |
f0e9f182 | 2229 | )g "\+" in package "JustUs" at /, |
5e2699c2 FC |
2230 | 'Error message when sub stub is encountered'; |
2231 | } | |
bf5522a1 | 2232 | |
c4b0eb7f DM |
2233 | { |
2234 | # check that the right number of stringifications | |
2235 | # and the correct un-utf8-ifying happen on regex compile | |
2236 | package utf8_match; | |
2237 | my $c; | |
2238 | use overload '""' => sub { $c++; $_[0][0] ? "^\x{100}\$" : "^A\$"; }; | |
2239 | my $o = bless [0], 'utf8_match'; | |
2240 | ||
2241 | $o->[0] = 0; | |
2242 | $c = 0; | |
2243 | ::ok("A" =~ "^A\$", "regex stringify utf8=0 ol=0 bytes=0"); | |
2244 | ::ok("A" =~ $o, "regex stringify utf8=0 ol=1 bytes=0"); | |
2245 | ::is($c, 1, "regex stringify utf8=0 ol=1 bytes=0 count"); | |
2246 | ||
2247 | $o->[0] = 1; | |
2248 | $c = 0; | |
2249 | ::ok("\x{100}" =~ "^\x{100}\$", | |
2250 | "regex stringify utf8=1 ol=0 bytes=0"); | |
2251 | ::ok("\x{100}" =~ $o, "regex stringify utf8=1 ol=1 bytes=0"); | |
2252 | ::is($c, 1, "regex stringify utf8=1 ol=1 bytes=0 count"); | |
2253 | ||
2254 | use bytes; | |
2255 | ||
2256 | $o->[0] = 0; | |
2257 | $c = 0; | |
2258 | ::ok("A" =~ "^A\$", "regex stringify utf8=0 ol=0 bytes=1"); | |
2259 | ::ok("A" =~ $o, "regex stringify utf8=0 ol=1 bytes=1"); | |
2260 | ::is($c, 1, "regex stringify utf8=0 ol=1 bytes=1 count"); | |
2261 | ||
2262 | $o->[0] = 1; | |
2263 | $c = 0; | |
9b513004 | 2264 | ::ok(main::byte_utf8a_to_utf8n("\xc4\x80") =~ "^\x{100}\$", |
c4b0eb7f | 2265 | "regex stringify utf8=1 ol=0 bytes=1"); |
9b513004 | 2266 | ::ok(main::byte_utf8a_to_utf8n("\xc4\x80") =~ $o, "regex stringify utf8=1 ol=1 bytes=1"); |
c4b0eb7f DM |
2267 | ::is($c, 1, "regex stringify utf8=1 ol=1 bytes=1 count"); |
2268 | ||
2269 | ||
2270 | } | |
2271 | ||
d4a6f307 DM |
2272 | # [perl #40333] |
2273 | # overload::Overloaded should not use a ->can designed for autoloading. | |
2274 | # This example attempts to be as realistic as possible. The o class has a | |
2275 | # default singleton object, but can have instances, too. The proxy class | |
2276 | # represents proxies for o objects, but class methods delegate to the | |
2277 | # singleton. | |
2278 | # overload::Overloaded used to return incorrect results for proxy objects. | |
2279 | package proxy { | |
2280 | sub new { bless [$_[1]], $_[0] } | |
2281 | sub AUTOLOAD { | |
2282 | our $AUTOLOAD =~ s/.*:://; | |
2283 | &_self->$AUTOLOAD; | |
2284 | } | |
2285 | sub can { SUPER::can{@_} || &_self->can($_[1]) } | |
2286 | sub _self { ref $_[0] ? $_[0][0] : $o::singleton } | |
2287 | } | |
2288 | package o { use overload '""' => sub { 'keck' }; | |
2289 | sub new { bless[], $_[0] } | |
2290 | our $singleton = o->new; } | |
2291 | ok !overload::Overloaded(new proxy new o), | |
2292 | 'overload::Overloaded does not incorrectly return true for proxy classes'; | |
2293 | ||
2294 | # Another test, based on the type of explosive test class for which | |
2295 | # perl #40333 was filed. | |
2296 | { | |
2297 | package broken_can; | |
2298 | sub can {} | |
2299 | use overload '""' => sub {"Ahoy!"}; | |
2300 | ||
2301 | package main; | |
2302 | my $obj = bless [], 'broken_can'; | |
2303 | ok(overload::Overloaded($obj)); | |
2304 | } | |
2305 | ||
bb4f579a DM |
2306 | sub eleventative::cos { 'eleven' } |
2307 | sub twelvetative::abs { 'twelve' } | |
2308 | sub thirteentative::abs { 'thirteen' } | |
2309 | sub fourteentative::abs { 'fourteen' } | |
2310 | @eleventative::ISA = twelvetative::; | |
2311 | { | |
2312 | my $o = bless [], 'eleventative'; | |
2313 | eval 'package eleventative; use overload map +($_)x2, cos=>abs=>'; | |
2314 | is cos $o, 'eleven', 'overloading applies to object blessed before'; | |
2315 | bless [], 'eleventative'; | |
2316 | is cos $o, 'eleven', | |
2317 | 'ovrld applies to previously-blessed obj after other obj is blessed'; | |
2318 | $o = bless [], 'eleventative'; | |
2319 | *eleventative::cos = sub { 'ten' }; | |
2320 | is cos $o, 'ten', 'method changes affect overloading'; | |
2321 | @eleventative::ISA = thirteentative::; | |
2322 | is abs $o, 'thirteen', 'isa changes affect overloading'; | |
2323 | bless $o, 'fourteentative'; | |
2324 | @fourteentative::ISA = 'eleventative'; | |
2325 | is abs $o, 'fourteen', 'isa changes can turn overloading on'; | |
2326 | } | |
2327 | ||
2328 | # no overload "fallback"; | |
2329 | { package phake; | |
2330 | use overload fallback => 1, '""' => sub { 'arakas' }; | |
2331 | no overload 'fallback'; | |
2332 | } | |
2333 | $a = bless [], 'phake'; | |
2334 | is "$a", "arakas", | |
2335 | 'no overload "fallback" does not stop overload from working'; | |
2336 | ok !eval { () = $a eq 'mpizeli'; 1 }, | |
2337 | 'no overload "fallback" resets fallback to undef on overloaded class'; | |
2338 | { package ent; use overload fallback => 0, abs => sub{}; | |
2339 | our@ISA = 'huorn'; | |
2340 | package huorn; | |
2341 | use overload fallback => 1; | |
2342 | package ent; | |
2343 | no overload "fallback"; # disable previous declaration | |
2344 | } | |
2345 | $a = bless [], ent::; | |
2346 | is eval {"$a"}, overload::StrVal($a), | |
2347 | 'no overload undoes fallback declaration completetly' | |
2348 | or diag $@; | |
2349 | ||
2350 | # inherited fallback | |
2351 | { | |
2352 | package pervyy; | |
2353 | our @ISA = 'vtoryy'; | |
2354 | use overload "abs" =>=> sub {}; | |
2355 | package vtoryy; | |
2356 | use overload fallback => 1, 'sin' =>=> sub{} | |
2357 | } | |
2358 | $a = bless [], pervyy::; | |
2359 | is eval {"$a"}, overload::StrVal($a), | |
2360 | 'fallback is inherited by classes that have their own overloading' | |
2361 | or diag $@; | |
2362 | ||
2363 | # package separators in method names | |
2364 | { | |
2365 | package mane; | |
2366 | use overload q\""\ => "bear::strength"; | |
2367 | use overload bool => "bear'bouillon"; | |
2368 | } | |
2369 | @bear::ISA = 'food'; | |
2370 | sub food::strength { 'twine' } | |
2371 | sub food::bouillon { 0 } | |
2372 | $a = bless[], mane::; | |
2373 | is eval { "$a" }, 'twine', ':: in method name' or diag $@; | |
2374 | is eval { !$a }, 1, "' in method name" or diag $@; | |
2375 | ||
2376 | # [perl #113050] Half of CPAN assumes fallback is under "()" | |
2377 | { | |
2378 | package dodo; | |
2379 | use overload '+' => sub {}; | |
2380 | no strict; | |
2381 | *{"dodo::()"} = sub{}; | |
2382 | ${"dodo::()"} = 1; | |
2383 | } | |
2384 | $a = bless [],'dodo'; | |
2385 | is eval {"$a"}, overload::StrVal($a), 'fallback is stored under "()"'; | |
2386 | ||
67288365 JL |
2387 | # [perl #47119] |
2388 | { | |
2389 | my $context; | |
2390 | ||
2391 | { | |
2392 | package Splitter; | |
2393 | use overload '<>' => \&chars; | |
2394 | ||
2395 | sub new { | |
2396 | my $class = shift; | |
2397 | my ($string) = @_; | |
2398 | bless \$string, $class; | |
2399 | } | |
2400 | ||
2401 | sub chars { | |
2402 | my $self = shift; | |
2403 | my @chars = split //, $$self; | |
2404 | $context = wantarray; | |
2405 | return @chars; | |
2406 | } | |
2407 | } | |
2408 | ||
2409 | my $obj = Splitter->new('bar'); | |
2410 | ||
2411 | $context = 42; # not 1, '', or undef | |
2412 | ||
2413 | my @foo = <$obj>; | |
2414 | is($context, 1, "list context (readline list)"); | |
2415 | is(scalar(@foo), 3, "correct result (readline list)"); | |
2416 | is($foo[0], 'b', "correct result (readline list)"); | |
2417 | is($foo[1], 'a', "correct result (readline list)"); | |
2418 | is($foo[2], 'r', "correct result (readline list)"); | |
2419 | ||
2420 | $context = 42; | |
2421 | ||
2422 | my $foo = <$obj>; | |
2423 | ok(defined($context), "scalar context (readline scalar)"); | |
2424 | is($context, '', "scalar context (readline scalar)"); | |
2425 | is($foo, 3, "correct result (readline scalar)"); | |
2426 | ||
2427 | $context = 42; | |
2428 | ||
2429 | <$obj>; | |
2430 | ok(!defined($context), "void context (readline void)"); | |
2431 | ||
2432 | $context = 42; | |
2433 | ||
2434 | my @bar = <${obj}>; | |
2435 | is($context, 1, "list context (glob list)"); | |
2436 | is(scalar(@bar), 3, "correct result (glob list)"); | |
2437 | is($bar[0], 'b', "correct result (glob list)"); | |
2438 | is($bar[1], 'a', "correct result (glob list)"); | |
2439 | is($bar[2], 'r', "correct result (glob list)"); | |
2440 | ||
2441 | $context = 42; | |
2442 | ||
2443 | my $bar = <${obj}>; | |
2444 | ok(defined($context), "scalar context (glob scalar)"); | |
2445 | is($context, '', "scalar context (glob scalar)"); | |
2446 | is($bar, 3, "correct result (glob scalar)"); | |
2447 | ||
2448 | $context = 42; | |
2449 | ||
2450 | <${obj}>; | |
2451 | ok(!defined($context), "void context (glob void)"); | |
2452 | } | |
2453 | { | |
2454 | my $context; | |
2455 | ||
2456 | { | |
2457 | package StringWithContext; | |
2458 | use overload '""' => \&stringify; | |
2459 | ||
2460 | sub new { | |
2461 | my $class = shift; | |
2462 | my ($string) = @_; | |
2463 | bless \$string, $class; | |
2464 | } | |
2465 | ||
2466 | sub stringify { | |
2467 | my $self = shift; | |
2468 | $context = wantarray; | |
2469 | return $$self; | |
2470 | } | |
2471 | } | |
2472 | ||
2473 | my $obj = StringWithContext->new('bar'); | |
2474 | ||
2475 | $context = 42; | |
2476 | ||
2477 | my @foo = "".$obj; | |
2478 | ok(defined($context), "scalar context (stringify list)"); | |
2479 | is($context, '', "scalar context (stringify list)"); | |
2480 | is(scalar(@foo), 1, "correct result (stringify list)"); | |
2481 | is($foo[0], 'bar', "correct result (stringify list)"); | |
2482 | ||
2483 | $context = 42; | |
2484 | ||
2485 | my $foo = "".$obj; | |
2486 | ok(defined($context), "scalar context (stringify scalar)"); | |
2487 | is($context, '', "scalar context (stringify scalar)"); | |
2488 | is($foo, 'bar', "correct result (stringify scalar)"); | |
2489 | ||
2490 | $context = 42; | |
2491 | ||
2492 | "".$obj; | |
2493 | ||
2494 | is($context, '', "scalar context (stringify void)"); | |
2495 | } | |
2496 | { | |
2497 | my ($context, $swap); | |
2498 | ||
2499 | { | |
2500 | package AddWithContext; | |
2501 | use overload '+' => \&add; | |
2502 | ||
2503 | sub new { | |
2504 | my $class = shift; | |
2505 | my ($num) = @_; | |
2506 | bless \$num, $class; | |
2507 | } | |
2508 | ||
2509 | sub add { | |
2510 | my $self = shift; | |
2511 | my ($other, $swapped) = @_; | |
2512 | $context = wantarray; | |
2513 | $swap = $swapped; | |
2514 | return ref($self)->new($$self + $other); | |
2515 | } | |
2516 | ||
2517 | sub val { ${ $_[0] } } | |
2518 | } | |
2519 | ||
2520 | my $obj = AddWithContext->new(6); | |
2521 | ||
2522 | $context = $swap = 42; | |
2523 | ||
2524 | my @foo = $obj + 7; | |
2525 | ok(defined($context), "scalar context (add list)"); | |
2526 | is($context, '', "scalar context (add list)"); | |
2527 | ok(defined($swap), "not swapped (add list)"); | |
2528 | is($swap, '', "not swapped (add list)"); | |
2529 | is(scalar(@foo), 1, "correct result (add list)"); | |
2530 | is($foo[0]->val, 13, "correct result (add list)"); | |
2531 | ||
2532 | $context = $swap = 42; | |
2533 | ||
2534 | @foo = 7 + $obj; | |
2535 | ok(defined($context), "scalar context (add list swap)"); | |
2536 | is($context, '', "scalar context (add list swap)"); | |
2537 | ok(defined($swap), "swapped (add list swap)"); | |
2538 | is($swap, 1, "swapped (add list swap)"); | |
2539 | is(scalar(@foo), 1, "correct result (add list swap)"); | |
2540 | is($foo[0]->val, 13, "correct result (add list swap)"); | |
2541 | ||
2542 | $context = $swap = 42; | |
2543 | ||
2544 | my $foo = $obj + 7; | |
2545 | ok(defined($context), "scalar context (add scalar)"); | |
2546 | is($context, '', "scalar context (add scalar)"); | |
2547 | ok(defined($swap), "not swapped (add scalar)"); | |
2548 | is($swap, '', "not swapped (add scalar)"); | |
2549 | is($foo->val, 13, "correct result (add scalar)"); | |
2550 | ||
2551 | $context = $swap = 42; | |
2552 | ||
2553 | my $foo = 7 + $obj; | |
2554 | ok(defined($context), "scalar context (add scalar swap)"); | |
2555 | is($context, '', "scalar context (add scalar swap)"); | |
2556 | ok(defined($swap), "swapped (add scalar swap)"); | |
2557 | is($swap, 1, "swapped (add scalar swap)"); | |
2558 | is($foo->val, 13, "correct result (add scalar swap)"); | |
2559 | ||
2560 | $context = $swap = 42; | |
2561 | ||
2562 | $obj + 7; | |
2563 | ||
2564 | ok(!defined($context), "void context (add void)"); | |
2565 | ok(defined($swap), "not swapped (add void)"); | |
2566 | is($swap, '', "not swapped (add void)"); | |
2567 | ||
2568 | $context = $swap = 42; | |
2569 | ||
2570 | 7 + $obj; | |
2571 | ||
2572 | ok(!defined($context), "void context (add void swap)"); | |
2573 | ok(defined($swap), "swapped (add void swap)"); | |
2574 | is($swap, 1, "swapped (add void swap)"); | |
2575 | ||
2576 | $obj = AddWithContext->new(6); | |
2577 | ||
2578 | $context = $swap = 42; | |
2579 | ||
2580 | my @foo = $obj += 7; | |
2581 | ok(defined($context), "scalar context (add assign list)"); | |
2582 | is($context, '', "scalar context (add assign list)"); | |
2583 | ok(!defined($swap), "not swapped and autogenerated (add assign list)"); | |
2584 | is(scalar(@foo), 1, "correct result (add assign list)"); | |
2585 | is($foo[0]->val, 13, "correct result (add assign list)"); | |
2586 | is($obj->val, 13, "correct result (add assign list)"); | |
2587 | ||
2588 | $obj = AddWithContext->new(6); | |
2589 | ||
2590 | $context = $swap = 42; | |
2591 | ||
2592 | my $foo = $obj += 7; | |
2593 | ok(defined($context), "scalar context (add assign scalar)"); | |
2594 | is($context, '', "scalar context (add assign scalar)"); | |
2595 | ok(!defined($swap), "not swapped and autogenerated (add assign scalar)"); | |
2596 | is($foo->val, 13, "correct result (add assign scalar)"); | |
2597 | is($obj->val, 13, "correct result (add assign scalar)"); | |
2598 | ||
2599 | $obj = AddWithContext->new(6); | |
2600 | ||
2601 | $context = $swap = 42; | |
2602 | ||
2603 | $obj += 7; | |
2604 | ||
2605 | ok(defined($context), "scalar context (add assign void)"); | |
2606 | is($context, '', "scalar context (add assign void)"); | |
2607 | ok(!defined($swap), "not swapped and autogenerated (add assign void)"); | |
2608 | is($obj->val, 13, "correct result (add assign void)"); | |
2609 | ||
2610 | $obj = AddWithContext->new(6); | |
2611 | ||
2612 | $context = $swap = 42; | |
2613 | ||
2614 | my @foo = ++$obj; | |
2615 | ok(defined($context), "scalar context (add incr list)"); | |
2616 | is($context, '', "scalar context (add incr list)"); | |
2617 | ok(!defined($swap), "not swapped and autogenerated (add incr list)"); | |
2618 | is(scalar(@foo), 1, "correct result (add incr list)"); | |
2619 | is($foo[0]->val, 7, "correct result (add incr list)"); | |
2620 | is($obj->val, 7, "correct result (add incr list)"); | |
2621 | ||
2622 | $obj = AddWithContext->new(6); | |
2623 | ||
2624 | $context = $swap = 42; | |
2625 | ||
2626 | my $foo = ++$obj; | |
2627 | ok(defined($context), "scalar context (add incr scalar)"); | |
2628 | is($context, '', "scalar context (add incr scalar)"); | |
2629 | ok(!defined($swap), "not swapped and autogenerated (add incr scalar)"); | |
2630 | is($foo->val, 7, "correct result (add incr scalar)"); | |
2631 | is($obj->val, 7, "correct result (add incr scalar)"); | |
2632 | ||
2633 | $obj = AddWithContext->new(6); | |
2634 | ||
2635 | $context = $swap = 42; | |
2636 | ||
2637 | ++$obj; | |
2638 | ||
2639 | ok(defined($context), "scalar context (add incr void)"); | |
2640 | is($context, '', "scalar context (add incr void)"); | |
2641 | ok(!defined($swap), "not swapped and autogenerated (add incr void)"); | |
2642 | is($obj->val, 7, "correct result (add incr void)"); | |
2643 | } | |
2644 | ||
386a5489 JL |
2645 | # [perl #113010] |
2646 | { | |
2647 | { | |
2648 | package OnlyFallback; | |
2649 | use overload fallback => 0; | |
2650 | } | |
79c9643d JL |
2651 | { |
2652 | my $obj = bless {}, 'OnlyFallback'; | |
2653 | my $died = !eval { "".$obj; 1 }; | |
2654 | my $err = $@; | |
2655 | ok($died, "fallback of 0 causes error"); | |
2656 | like($err, qr/"\.": no method found/, "correct error"); | |
2657 | } | |
2658 | ||
2659 | { | |
2660 | package OnlyFallbackUndef; | |
2661 | use overload fallback => undef; | |
2662 | } | |
2663 | { | |
2664 | my $obj = bless {}, 'OnlyFallbackUndef'; | |
2665 | my $died = !eval { "".$obj; 1 }; | |
2666 | my $err = $@; | |
2667 | ok($died, "fallback of undef causes error"); | |
2668 | # this one tries falling back to stringify before dying | |
2669 | like($err, qr/"""": no method found/, "correct error"); | |
2670 | } | |
2671 | ||
2672 | { | |
2673 | package OnlyFallbackTrue; | |
2674 | use overload fallback => 1; | |
2675 | } | |
2676 | { | |
2677 | my $obj = bless {}, 'OnlyFallbackTrue'; | |
2678 | my $val; | |
2679 | my $died = !eval { $val = "".$obj; 1 }; | |
2680 | my $err = $@; | |
2681 | ok(!$died, "fallback of 1 doesn't cause error") | |
2682 | || diag("got error of $err"); | |
2683 | like($val, qr/^OnlyFallbackTrue=HASH\(/, "stringified correctly"); | |
2684 | } | |
386a5489 | 2685 | } |
bb4f579a | 2686 | |
00bf6ed2 DM |
2687 | { |
2688 | # Making Regexp class overloaded: avoid infinite recursion. | |
2689 | # Do this in a separate process since it, well, overloads Regexp! | |
2690 | fresh_perl_is( | |
2691 | <<'EOF', | |
2692 | package Regexp; | |
2693 | use overload q{""} => sub {$_[0] }; | |
2694 | package main; | |
2695 | my $r1 = qr/1/; | |
2696 | my $r2 = qr/ABC$r1/; | |
2697 | print $r2,"\n"; | |
2698 | EOF | |
2699 | '(?^:ABC(?^:1))', | |
2700 | { stderr => 1 }, | |
2701 | 'overloaded REGEXP' | |
2702 | ); | |
2703 | } | |
2704 | ||
45479970 DM |
2705 | { |
2706 | # RT #121362 | |
2707 | # splitting the stash HV while rebuilding the overload cache gave | |
2708 | # valgrind errors. This test code triggers such a split. It doesn't | |
2709 | # actually test anything; its just there for valgrind to spot | |
2710 | # problems. | |
2711 | ||
2712 | package A_121362; | |
2713 | ||
2714 | sub stringify { } | |
2715 | use overload '""' => 'stringify'; | |
2716 | ||
2717 | package B_121362; | |
2718 | our @ISA = qw(A_121362); | |
2719 | ||
2720 | package main; | |
2721 | ||
2722 | my $x = bless { }, 'B_121362'; | |
2723 | ||
2724 | for ('a'..'z') { | |
2725 | delete $B_121362::{stringify}; # delete cache entry | |
2726 | no strict 'refs'; | |
2727 | *{"B_121362::$_"} = sub { }; # increase size of %B_121362 | |
2728 | my $y = $x->{value}; # trigger cache add to %B_121362 | |
2729 | } | |
2730 | pass("RT 121362"); | |
2731 | } | |
2732 | ||
eff75473 FC |
2733 | package refsgalore { |
2734 | use overload | |
2735 | '${}' => sub { \42 }, | |
2736 | '@{}' => sub { [43] }, | |
2737 | '%{}' => sub { { 44 => 45 } }, | |
2738 | '&{}' => sub { sub { 46 } }; | |
2739 | } | |
2740 | { | |
2741 | use feature 'postderef'; | |
eff75473 FC |
2742 | tell myio; # vivifies *myio{IO} at compile time |
2743 | use constant ioref => bless *myio{IO}, refsgalore::; | |
2744 | is ioref->$*, 42, '(overloaded constant that is not a scalar ref)->$*'; | |
2745 | is ioref->[0], 43, '(ovrld constant that is not an array ref)->[0]'; | |
2746 | is ioref->{44}, 45, "(ovrld const that is not a hash ref)->{key}"; | |
2747 | is ioref->(), 46, '(overloaded constant that is not a sub ref)->()'; | |
2748 | } | |
45479970 | 2749 | |
3a100dab FC |
2750 | package xstack { use overload 'x' => sub { shift . " x " . shift }, |
2751 | '""'=> sub { "xstack" } } | |
2752 | is join(",", 1..3, scalar((bless([], 'xstack')) x 3, 1), 4..6), | |
2753 | "1,2,3,1,4,5,6", | |
2754 | '(...)x... in void cx with x overloaded [perl #121827]'; | |
2755 | ||
e6288bd5 FC |
2756 | package bitops { |
2757 | our @o; | |
2758 | use overload do { | |
2759 | my %o; | |
2760 | for my $o (qw(& | ^ ~ &. |. ^. ~. &= |= ^= &.= |.= ^.=)) { | |
636ac8fc FC |
2761 | $o{$o} = sub { |
2762 | ::ok !defined $_[3], "undef (or nonexistent) arg 3 for $o"; | |
2763 | push @o, $o, scalar @_, $_[4]//'u'; | |
2764 | $_[0] | |
2765 | } | |
e6288bd5 FC |
2766 | } |
2767 | %o, '=' => sub { bless [] }; | |
2768 | } | |
2769 | } | |
2770 | { | |
2771 | use experimental 'bitwise'; | |
2772 | my $o = bless [], bitops::; | |
2773 | $_ = $o & 0; | |
2774 | $_ = $o | 0; | |
2775 | $_ = $o ^ 0; | |
2776 | $_ = ~$o; | |
2777 | $_ = $o &. 0; | |
2778 | $_ = $o |. 0; | |
2779 | $_ = $o ^. 0; | |
2780 | $_ = ~.$o; | |
2781 | $o &= 0; | |
2782 | $o |= 0; | |
2783 | $o ^= 0; | |
2784 | $o &.= 0; | |
2785 | $o |.= 0; | |
2786 | $o ^.= 0; | |
636ac8fc FC |
2787 | # elems are in triplets: op, length of @_, numeric? (1/u for y/n) |
2788 | is "@bitops::o", '& 5 1 | 5 1 ^ 5 1 ~ 5 1 &. 3 u |. 3 u ^. 3 u ~. 3 u ' . '&= 5 1 |= 5 1 ^= 5 1 &.= 3 u |.= 3 u ^.= 3 u', | |
e6288bd5 FC |
2789 | 'experimental "bitwise" ops' |
2790 | } | |
636ac8fc FC |
2791 | package bitops2 { |
2792 | our @o; | |
2793 | use overload | |
2794 | nomethod => sub { push @o, $_[3], scalar @_, $_[4]//'u'; $_[0] }, | |
2795 | '=' => sub { bless [] }; | |
2796 | } | |
2797 | { | |
2798 | use experimental 'bitwise'; | |
2799 | my $o = bless [], bitops2::; | |
2800 | $_ = $o & 0; | |
2801 | $_ = $o | 0; | |
2802 | $_ = $o ^ 0; | |
2803 | $_ = ~$o; | |
2804 | $_ = $o &. 0; | |
2805 | $_ = $o |. 0; | |
2806 | $_ = $o ^. 0; | |
2807 | $_ = ~.$o; | |
2808 | $o &= 0; | |
2809 | $o |= 0; | |
2810 | $o ^= 0; | |
2811 | $o &.= 0; | |
2812 | $o |.= 0; | |
2813 | $o ^.= 0; | |
2814 | # elems are in triplets: op, length of @_, numeric? (1/u for y/n) | |
2815 | is "@bitops2::o", '& 5 1 | 5 1 ^ 5 1 ~ 5 1 &. 4 u |. 4 u ^. 4 u ~. 4 u ' . '&= 5 1 |= 5 1 ^= 5 1 &.= 4 u |.= 4 u ^.= 4 u', | |
2816 | 'experimental "bitwise" ops with nomethod' | |
2817 | } | |
e6288bd5 | 2818 | |
5b750817 DM |
2819 | package length_utf8 { |
2820 | use overload '""' => sub { "\x{100}" }; | |
2821 | my $o = bless []; | |
2822 | print length $o, "\n"; | |
2823 | ||
2824 | ::is length($o), 1, "overloaded utf8 length"; | |
2825 | ::is "$o", "\x{100}", "overloaded utf8 value"; | |
2826 | } | |
2827 | ||
2828 | ||
bb4f579a DM |
2829 | { # undefining the overload stash -- KEEP THIS TEST LAST |
2830 | package ant; | |
2831 | use overload '+' => 'onion'; | |
2832 | $_ = \&overload::nil; | |
2833 | undef %overload::; | |
2834 | ()=0+bless[]; | |
2835 | ::ok(1, 'no crash when undefining %overload::'); | |
2836 | } | |
2837 | ||
d4a6f307 | 2838 | |
c0acf911 DM |
2839 | # test various aspects of string concat overloading, especially where |
2840 | # multiple concats etc are optimised into a single multiconcat op | |
2841 | ||
2842 | package Concat { | |
2843 | ||
2844 | my $id; | |
2845 | ||
2846 | # append a brief description of @_ to $id | |
2847 | sub id { | |
2848 | my @a = map ref $_ ? "[" . $_->[0] . "]" : | |
2849 | !defined $_ ? "u" : | |
2850 | $_, | |
2851 | @_; | |
2852 | $id .= '(' . join (',', @a) . ')'; | |
2853 | } | |
2854 | ||
2855 | use overload | |
2856 | '.' => sub { | |
2857 | id('.', @_); | |
2858 | my ($l, $r, $rev) = @_; | |
2859 | ($l, $r) = map ref $_ ? $_->[0] : $_, $l, $r; | |
2860 | ($l,$r) = ($r, $l) if $rev; | |
2861 | bless [ $l . $r ]; | |
2862 | }, | |
2863 | ||
2864 | '.=' => sub { | |
2865 | id('.=', @_); | |
2866 | my ($l, $r, $rev) = @_; | |
2867 | my ($ll, $rr) = map ref $_ ? $_->[0] : $_, $l, $r; | |
2868 | die "Unexpected reverse in .=" if $rev; | |
2869 | $l->[0] .= ref $r ? $r->[0] : $r; | |
2870 | $l; | |
2871 | }, | |
2872 | ||
2873 | '=' => sub { | |
2874 | id('=', @_); | |
2875 | bless [ $_[0][0] ]; | |
2876 | }, | |
2877 | ||
2878 | '""' => sub { | |
2879 | id('""', @_); | |
2880 | $_[0][0]; | |
2881 | }, | |
2882 | ; | |
2883 | ||
2884 | my $a = 'a'; | |
2885 | my $b = 'b'; | |
2886 | my $c = 'c'; | |
2887 | my $A = bless [ 'A' ]; | |
2888 | my $B = bless [ 'B' ]; | |
2889 | my $C = bless [ 'C' ]; | |
2890 | ||
2891 | my ($r, $R); | |
2892 | ||
2893 | ||
bcc30fd0 | 2894 | # like cc, but with $is_ref set to 1 |
c0acf911 DM |
2895 | sub c { |
2896 | my ($expr, $expect, $exp_id) = @_; | |
2897 | cc($expr, $expect, 1, $exp_id); | |
2898 | } | |
2899 | ||
2900 | # eval $expr, and see if it returns $expect, and whether | |
2901 | # the returned value is a ref ($is_ref). Finally, check that | |
2902 | # $id, which has accumulated info from all overload method calls, | |
2903 | # matches $exp_id. | |
2904 | ||
2905 | sub cc { | |
2906 | my ($expr, $expect, $is_ref, $exp_id) = @_; | |
2907 | ||
2908 | $id = ''; | |
2909 | $r = 'r'; | |
2910 | $R = bless ['R']; | |
2911 | ||
2912 | my $got = eval $expr; | |
2913 | die "eval failed: $@" if $@; | |
2914 | ::is "$got", $expect, "expect: $expr"; | |
2915 | ::is $id, $exp_id, "id: $expr"; | |
2916 | ::is ref($got), ($is_ref ? 'Concat' : ''), "is_ref: $expr"; | |
2917 | } | |
2918 | ||
2919 | # single concats | |
2920 | ||
2921 | c '$r=$A.$b', 'Ab', '(.,[A],b,)("",[Ab],u,)'; | |
2922 | c '$r=$a.$B', 'aB', '(.,[B],a,1)("",[aB],u,)'; | |
2923 | c '$r=$A.$B', 'AB', '(.,[A],[B],)("",[AB],u,)'; | |
2924 | c '$R.=$a', 'Ra', '(.=,[R],a,u)("",[Ra],u,)'; | |
2925 | c '$R.=$A', 'RA', '(.=,[R],[A],u)("",[RA],u,)'; | |
2926 | ||
2927 | # two concats | |
2928 | ||
2929 | c '$r=$A.$b.$c', 'Abc', '(.,[A],b,)(.=,[Ab],c,u)("",[Abc],u,)'; | |
2930 | c '$r=$A.($b.$c)', 'Abc', '(.,[A],bc,)("",[Abc],u,)'; | |
2931 | c '$r=$a.$B.$c', 'aBc', '(.,[B],a,1)(.=,[aB],c,u)("",[aBc],u,)'; | |
2932 | c '$r=$a.($B.$c)', 'aBc', '(.,[B],c,)(.,[Bc],a,1)("",[aBc],u,)'; | |
2933 | c '$r=$a.$b.$C', 'abC', '(.,[C],ab,1)("",[abC],u,)'; | |
2934 | c '$r=$a.($b.$C)', 'abC', '(.,[C],b,1)(.,[bC],a,1)("",[abC],u,)'; | |
2935 | ||
2936 | # two concats plus mutator | |
2937 | ||
2938 | c '$r.=$A.$b.$c', 'rAbc', '(.,[A],b,)(.=,[Ab],c,u)(.,[Abc],r,1)' | |
2939 | .'("",[rAbc],u,)'; | |
2940 | c '$r.=$A.($b.$c)', 'rAbc', '(.,[A],bc,)(.,[Abc],r,1)("",[rAbc],u,)'; | |
2941 | c '$r.=$a.$B.$c', 'raBc', '(.,[B],a,1)(.=,[aB],c,u)(.,[aBc],r,1)' | |
2942 | .'("",[raBc],u,)'; | |
2943 | c '$r.=$a.($B.$c)', 'raBc', '(.,[B],c,)(.,[Bc],a,1)(.,[aBc],r,1)' | |
2944 | .'("",[raBc],u,)'; | |
2945 | c '$r.=$a.$b.$C', 'rabC', '(.,[C],ab,1)(.,[abC],r,1)("",[rabC],u,)'; | |
2946 | c '$r.=$a.($b.$C)', 'rabC', '(.,[C],b,1)(.,[bC],a,1)(.,[abC],r,1)' | |
2947 | .'("",[rabC],u,)'; | |
2948 | ||
2949 | c '$R.=$A.$b.$c', 'RAbc', '(.,[A],b,)(.=,[Ab],c,u)(.=,[R],[Abc],u)' | |
2950 | .'("",[RAbc],u,)'; | |
2951 | c '$R.=$A.($b.$c)', 'RAbc', '(.,[A],bc,)(.=,[R],[Abc],u)("",[RAbc],u,)'; | |
2952 | c '$R.=$a.$B.$c', 'RaBc', '(.,[B],a,1)(.=,[aB],c,u)(.=,[R],[aBc],u)' | |
2953 | .'("",[RaBc],u,)'; | |
2954 | c '$R.=$a.($B.$c)', 'RaBc', '(.,[B],c,)(.,[Bc],a,1)(.=,[R],[aBc],u)' | |
2955 | .'("",[RaBc],u,)'; | |
2956 | c '$R.=$a.$b.$C', 'RabC', '(.,[C],ab,1)(.=,[R],[abC],u)("",[RabC],u,)'; | |
2957 | c '$R.=$a.($b.$C)', 'RabC', '(.,[C],b,1)(.,[bC],a,1)(.=,[R],[abC],u)' | |
2958 | .'("",[RabC],u,)'; | |
2959 | ||
2960 | # concat over assign | |
2961 | ||
2962 | c '($R.=$a).$B.$c', 'RaBc', '(.=,[R],a,u)(.,[Ra],[B],)(.=,[RaB],c,u)' | |
2963 | .'("",[RaBc],u,)'; | |
2964 | ::is "$R", "Ra", 'R in concat over assign'; | |
2965 | ||
2966 | ||
2967 | # nested mutators | |
2968 | ||
2969 | c '(($R.=$a).=$b).=$c', 'Rabc', '(.=,[R],a,u)(=,[Ra],u,)(.=,[Ra],b,u)' | |
2970 | . '(=,[Rab],u,)(.=,[Rab],c,u)("",[Rabc],u,)'; | |
2971 | c '(($R.=$a).=$B).=$c', 'RaBc', '(.=,[R],a,u)(=,[Ra],u,)(.=,[Ra],[B],u)' | |
2972 | . '(=,[RaB],u,)(.=,[RaB],c,u)("",[RaBc],u,)'; | |
2973 | ||
2974 | # plain SV on both LHS and RHS with RHS object | |
2975 | ||
2976 | c '$r=$r.$A.$r', 'rAr', '(.,[A],r,1)(.=,[rA],r,u)("",[rAr],u,)'; | |
2977 | c '$r.=$r.$A.$r', 'rrAr', '(.,[A],r,1)(.=,[rA],r,u)(.,[rAr],r,1)' | |
2978 | .'("",[rrAr],u,)'; | |
2979 | ||
2980 | # object on both LHS and RHS | |
2981 | ||
2982 | c '$R.=$R', 'RR', '(.=,[R],[R],u)("",[RR],u,)'; | |
2983 | c '$R.=$R.$b.$c', 'RRbc', '(.,[R],b,)(.=,[Rb],c,u)(.=,[R],[Rbc],u)' | |
2984 | .'("",[RRbc],u,)'; | |
2985 | c '$R.=$a.$R.$c', 'RaRc', '(.,[R],a,1)(.=,[aR],c,u)(.=,[R],[aRc],u)' | |
2986 | .'("",[RaRc],u,)'; | |
2987 | c '$R.=$a.$b.$R', 'RabR', '(.,[R],ab,1)(.=,[R],[abR],u)("",[RabR],u,)'; | |
2988 | ||
2989 | ||
2990 | # sprintf shouldn't do concat overloading | |
2991 | ||
2992 | cc '$r=sprintf("%s%s%s",$a,$B,$c)', 'aBc', 0, '("",[B],u,)'; | |
2993 | cc '$R=sprintf("%s%s%s",$a,$B,$c)', 'aBc', 0, '("",[B],u,)'; | |
2994 | cc '$r.=sprintf("%s%s%s",$a,$B,$c)', 'raBc', 0, '("",[B],u,)'; | |
2995 | cc '$R.=sprintf("%s%s%s",$a,$B,$c)', 'RaBc', 1, '("",[B],u,)(.=,[R],aBc,u)' | |
2996 | .'("",[RaBc],u,)'; | |
bcc30fd0 DM |
2997 | |
2998 | # multiple constants should individually overload (RT #132385) | |
2999 | ||
3000 | c '$r=$A."b"."c"', 'Abc', '(.,[A],b,)(.=,[Ab],c,u)("",[Abc],u,)'; | |
3001 | ||
3002 | # ... except for this | |
3003 | c '$R.="a"."b"', 'Rab', '(.=,[R],ab,u)("",[Rab],u,)'; | |
c0acf911 | 3004 | } |
b3ab0375 DM |
3005 | |
3006 | # RT #132385 | |
3007 | # The first arg of a reversed concat shouldn't be stringified: | |
3008 | # $left . $right | |
3009 | # where $right is overloaded, should invoke | |
3010 | # concat($right, $left, 1) | |
3011 | # rather than | |
3012 | # concat($right, "$left", 1) | |
d7e75038 DM |
3013 | # There's a similar issue with |
3014 | # $left .= $right | |
3015 | # when left is overloaded | |
b3ab0375 DM |
3016 | |
3017 | package RT132385 { | |
3018 | ||
3019 | use constant C => [ "constref" ]; | |
3020 | ||
3021 | use overload '.' => sub { | |
d7e75038 DM |
3022 | my ($l, $r, $rev) = @_; |
3023 | ($l,$r) = ($r,$l) if $rev; | |
3024 | $l = ref $l ? $l->[0] : "$l"; | |
3025 | $r = ref $r ? $r->[0] : "$r"; | |
3026 | "$l-$r"; | |
b3ab0375 DM |
3027 | } |
3028 | ; | |
3029 | ||
3030 | my $r1 = [ "ref1" ]; | |
3031 | my $r2 = [ "ref2" ]; | |
3032 | my $s1 = "str1"; | |
3033 | ||
3034 | my $o = bless [ "obj" ]; | |
3035 | ||
3036 | # try variations that will call either pp_concat or pp_multiconcat, | |
3037 | # with the ref as the first or a later arg | |
3038 | ||
3039 | ::is($r1.$o, "ref1-obj", "RT #132385 r1.o"); | |
3040 | ::is($r1.$o.$s1 , "ref1-objstr1", "RT #132385 r1.o.s1"); | |
3041 | ::is("const".$o.$s1 ,"const-objstr1", "RT #132385 const.o.s1"); | |
3042 | ::is(C.$o.$s1 ,"constref-objstr1", "RT #132385 C.o.s1"); | |
3043 | ||
3044 | ::like($r1.$r2.$o, qr/^ARRAY\(0x\w+\)ARRAY\(0x\w+\)-obj/, | |
3045 | "RT #132385 r1.r2.o"); | |
d7e75038 DM |
3046 | |
3047 | # ditto with a mutator | |
3048 | ::is($o .= $r1, "obj-ref1", "RT #132385 o.=r1"); | |
b3ab0375 | 3049 | } |