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