7 if (($Config::Config{'extensions'} !~ m!\bList/Util\b!) ){
8 print "1..0 # Skip -- Perl configured without List::Util module\n";
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]} :
27 '%' => sub {new Oscalar
28 $_[2]? $_[1]%${$_[0]} : ${$_[0]}%$_[1]},
29 '**' => sub {new Oscalar
30 $_[2]? $_[1]**${$_[0]} : ${$_[0]}-$_[1]},
34 0+ numify) # Order of arguments unsignificant
42 sub stringify { "${$_[0]}" }
43 sub numify { 0 + "${$_[0]}" } # Not needed, additional overhead
44 # comparing to direct compilation based on
51 print "1..",&last,"\n";
57 $comment = " # " . $_ [2] if @_ > 2;
59 print "ok $test$comment\n";
62 $comment .= ": '$_[0]' ne '$_[1]'";
63 print "not ok $test$comment\n";
71 print "not ok $test\n";
77 $a = new Oscalar "087";
80 # All test numbers in comments are off by 1.
81 # So much for hard-wiring them in :-) To fix this:
85 test ($b eq "087"); # 3
86 test (ref $a eq "Oscalar"); # 4
88 test ($a eq "087"); # 6
92 test (ref $c eq "Oscalar"); # 7
93 test (!($c eq $a)); # 8
94 test ($c eq "94"); # 9
98 test (ref $a eq "Oscalar"); # 10
102 test (ref $b eq "Oscalar"); # 11
103 test ( $a eq "087"); # 12
104 test ( $b eq "88"); # 13
105 test (ref $a eq "Oscalar"); # 14
110 test (ref $c eq "Oscalar"); # 15
111 test ( $a eq "087"); # 16
112 test ( $c eq "1"); # 17
113 test (ref $a eq "Oscalar"); # 18
118 test (ref $b eq "Oscalar"); # 19
119 test ( $a eq "087"); # 20
120 test ( $b eq "88"); # 21
121 test (ref $a eq "Oscalar"); # 22
123 eval q[ package Oscalar; use overload ('++' => sub { $ {$_[0]}++;$_[0] } ) ];
127 test (ref $a eq "Oscalar"); # 23
131 test (ref $b eq "Oscalar"); # 24
132 test ( $a eq "087"); # 25
133 test ( $b eq "88"); # 26
134 test (ref $a eq "Oscalar"); # 27
137 $dummy=bless \$dummy; # Now cache of method should be reloaded
143 test (ref $b eq "Oscalar"); # 28
144 test ( $a eq "087"); # 29
145 test ( $b eq "88"); # 30
146 test (ref $a eq "Oscalar"); # 31
148 undef $b; # Destroying updates tables too...
150 eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ];
154 test (ref $a eq "Oscalar"); # 32
158 test (ref $b eq "Oscalar"); # 33
159 test ( $a eq "087"); # 34
160 test ( $b eq "88"); # 35
161 test (ref $a eq "Oscalar"); # 36
164 $dummy=bless \$dummy; # Now cache of method should be reloaded
169 test (ref $b eq "Oscalar"); # 37
170 test ( $a eq "087"); # 38
171 test ( $b eq "90"); # 39
172 test (ref $a eq "Oscalar"); # 40
177 test (ref $b eq "Oscalar"); # 41
178 test ( $a eq "087"); # 42
179 test ( $b eq "89"); # 43
180 test (ref $a eq "Oscalar"); # 44
185 eval q[ package Oscalar; use overload ('=' => sub {$main::copies++;
187 local $new=$ {$_[0]};
192 test (ref $b eq "Oscalar"); # 46
193 test ( $a eq "087"); # 47
194 test ( $b eq "087"); # 48
195 test (ref $a eq "Oscalar"); # 49
199 test (ref $b eq "Oscalar"); # 50
200 test ( $a eq "087"); # 51
201 test ( $b eq "89"); # 52
202 test (ref $a eq "Oscalar"); # 53
203 test ($copies == 0); # 54
207 test (ref $b eq "Oscalar"); # 55
208 test ( $a eq "087"); # 56
209 test ( $b eq "90"); # 57
210 test (ref $a eq "Oscalar"); # 58
211 test ($copies == 0); # 59
216 test (ref $b eq "Oscalar"); # 60
217 test ( $a eq "087"); # 61
218 test ( $b eq "88"); # 62
219 test (ref $a eq "Oscalar"); # 63
220 test ($copies == 0); # 64
225 test (ref $b eq "Oscalar") || print ref $b,"=ref(b)\n"; # 65
226 test ( $a eq "087"); # 66
227 test ( $b eq "89"); # 67
228 test (ref $a eq "Oscalar"); # 68
229 test ($copies == 1); # 69
231 eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1];
233 $c=new Oscalar; # Cause rehash
238 test (ref $b eq "Oscalar"); # 70
239 test ( $a eq "087"); # 71
240 test ( $b eq "90"); # 72
241 test (ref $a eq "Oscalar"); # 73
242 test ($copies == 2); # 74
246 test (ref $b eq "Oscalar"); # 75
247 test ( $b eq "360"); # 76
248 test ($copies == 2); # 77
251 test (ref $b eq "Oscalar"); # 78
252 test ( $b eq "-360"); # 79
253 test ($copies == 2); # 80
257 test (ref $b eq "Oscalar"); # 81
258 test ( $b eq "360"); # 82
259 test ($copies == 2); # 83
263 test (ref $b eq "Oscalar"); # 84
264 test ( $b eq "360"); # 85
265 test ($copies == 2); # 86
267 eval q[package Oscalar;
268 use overload ('x' => sub {new Oscalar ( $_[2] ? "_.$_[1]._" x $ {$_[0]}
269 : "_.${$_[0]}._" x $_[1])}) ];
273 test ($a eq "_.yy.__.yy.__.yy._"); # 87
275 eval q[package Oscalar;
276 use overload ('.' => sub {new Oscalar ( $_[2] ?
277 "_.$_[1].__.$ {$_[0]}._"
278 : "_.$ {$_[0]}.__.$_[1]._")}) ];
282 test ("b${a}c" eq "_._.b.__.xx._.__.c._"); # 88
284 # Check inheritance of overloading;
290 $aI = new OscalarI "$a";
291 test (ref $aI eq "OscalarI"); # 89
292 test ("$aI" eq "xx"); # 90
293 test ($aI eq "xx"); # 91
294 test ("b${aI}c" eq "_._.b.__.xx._.__.c._"); # 92
296 # Here we test blessing to a package updates hash
298 eval "package Oscalar; no overload '.'";
300 test ("b${a}" eq "_.b.__.xx._"); # 93
303 test ("b${a}c" eq "bxxc"); # 94
305 test ("b${a}c" eq "bxxc"); # 95
307 # Negative overloading:
310 test($@ =~ /no method found/); # 96
315 sub { *{"Oscalar::$AUTOLOAD"} = sub {"_!_" . shift() . "_!_"} ;
316 goto &{"Oscalar::$AUTOLOAD"}};
318 eval "package Oscalar; sub comple; use overload '~' => 'comple'";
320 $na = eval { ~$a }; # Hash was not updated
321 test($@ =~ /no method found/); # 97
325 $na = eval { ~$a }; # Hash updated
326 warn "`$na', $@" if $@;
328 test($na eq '_!_xx_!_'); # 99
332 $na = eval { ~$aI }; # Hash was not updated
333 test($@ =~ /no method found/); # 100
341 test($na eq '_!_xx_!_'); # 102
343 eval "package Oscalar; sub rshft; use overload '>>' => 'rshft'";
345 $na = eval { $aI >> 1 }; # Hash was not updated
346 test($@ =~ /no method found/); # 103
352 $na = eval { $aI >> 1 };
356 test($na eq '_!_xx_!_'); # 105
358 # warn overload::Method($a, '0+'), "\n";
359 test (overload::Method($a, '0+') eq \&Oscalar::numify); # 106
360 test (overload::Method($aI,'0+') eq \&Oscalar::numify); # 107
361 test (overload::Overloaded($aI)); # 108
362 test (!overload::Overloaded('overload')); # 109
364 test (! defined overload::Method($aI, '<<')); # 110
365 test (! defined overload::Method($a, '<')); # 111
367 test (overload::StrVal($aI) =~ /^OscalarI=SCALAR\(0x[\da-fA-F]+\)$/); # 112
368 test (overload::StrVal(\$aI) eq "@{[\$aI]}"); # 113
370 # Check overloading by methods (specified deep in the ISA tree).
374 sub Oscalar::lshft {"_<<_" . shift() . "_<<_"}
375 eval "package OscalarI; use overload '<<' => 'lshft', '|' => 'lshft'";
380 bless $aII, 'OscalarII';
381 bless \$fake, 'OscalarI'; # update the hash
382 test(($aI | 3) eq '_<<_xx_<<_'); # 114
384 test(($aII << 3) eq '_<<_087_<<_'); # 115
387 BEGIN { $int = 7; overload::constant 'integer' => sub {$int++; shift}; }
391 test($out, 1024); # 117
396 BEGIN { $q = $qr = 7;
397 overload::constant 'q' => sub {$q++; push @q, shift, ($_[1] || 'none'); shift},
398 'qr' => sub {$qr++; push @qr, shift, ($_[1] || 'none'); shift}; }
401 $out2 = "a\a$foo,\,";
405 test($out, 'foo'); # 118
406 test($out, $foo); # 119
407 test($out1, 'f\'o\\o'); # 120
408 test($out1, $foo1); # 121
409 test($out2, "a\afoo,\,"); # 122
410 test("@q", "foo q f'o\\\\o q a\\a qq ,\\, qq"); # 123
412 test("@qr", "b\\b qq .\\. qq"); # 125
416 $_ = '!<b>!foo!<-.>!';
417 BEGIN { overload::constant 'q' => sub {push @q1, shift, ($_[1] || 'none'); "_<" . (shift) . ">_"},
418 'qr' => sub {push @qr1, shift, ($_[1] || 'none'); "!<" . (shift) . ">!"}; }
421 $out2 = "a\a$foo,\,";
431 s'first part'second part';
432 s/yet another/tail here/;
436 test($out, '_<foo>_'); # 117
437 test($out1, '_<f\'o\\o>_'); # 128
438 test($out2, "_<a\a>_foo_<,\,>_"); # 129
439 test("@q1", "foo q f'o\\\\o q a\\a qq ,\\, qq oups
441 q second part q tail here s A-Z tr a-z tr"); # 130
442 test("@qr1", "b\\b qq .\\. qq try it q first part q yet another qq"); # 131
448 test($c, "bareword"); # 135
451 package symbolic; # Primitive symbolic calculator
452 use overload nomethod => \&wrap, '""' => \&str, '0+' => \&num,
453 '=' => \&cpy, '++' => \&inc, '--' => \&dec;
455 sub new { shift; bless ['n', @_] }
458 bless [@$self], ref $self;
460 sub inc { $_[0] = bless ['++', $_[0], 1]; }
461 sub dec { $_[0] = bless ['--', $_[0], 1]; }
463 my ($obj, $other, $inv, $meth) = @_;
464 if ($meth eq '++' or $meth eq '--') {
465 @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference
468 ($obj, $other) = ($other, $obj) if $inv;
469 bless [$meth, $obj, $other];
472 my ($meth, $a, $b) = @{+shift};
473 $a = 'u' unless defined $a;
480 my %subr = ( 'n' => sub {$_[0]} );
481 foreach my $op (split " ", $overload::ops{with_assign}) {
482 $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}";
484 my @bins = qw(binary 3way_comparison num_comparison str_comparison);
485 foreach my $op (split " ", "@overload::ops{ @bins }") {
486 $subr{$op} = eval "sub {shift() $op shift()}";
488 foreach my $op (split " ", "@overload::ops{qw(unary func)}") {
489 $subr{$op} = eval "sub {$op shift()}";
491 $subr{'++'} = $subr{'+'};
492 $subr{'--'} = $subr{'-'};
495 my ($meth, $a, $b) = @{+shift};
496 my $subr = $subr{$meth}
497 or die "Do not know how to ($meth) in symbolic";
498 $a = $a->num if ref $a eq __PACKAGE__;
499 $b = $b->num if ref $b eq __PACKAGE__;
502 sub TIESCALAR { my $pack = shift; $pack->new(@_) }
504 sub nop { } # Around a bug
505 sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; }
514 my $foo = new symbolic 11;
516 test( (sprintf "%d", $foo), '12');
517 test( (sprintf "%d", $baz), '11');
520 test( (sprintf "%d", $foo), '13');
521 test( (sprintf "%d", $bar), '12');
522 test( (sprintf "%d", $baz), '13');
525 test( (sprintf "%d", $foo), '14');
526 test( (sprintf "%d", $bar), '12');
527 test( (sprintf "%d", $baz), '14');
528 test( (sprintf "%d", $ban), '13');
531 test( (sprintf "%d", $foo), '15');
532 test( (sprintf "%d", $baz), '14');
533 test( "$foo", '[++ [+= [++ [++ [n 11] 1] 1] 1] 1]');
537 my $iter = new symbolic 2;
538 my $side = new symbolic 1;
542 $cnt = $cnt - 1; # The "simple" way
543 $side = (sqrt(1 + $side**2) - 1)/$side;
545 my $pi = $side*(2**($iter+2));
546 test "$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]';
547 test( (sprintf "%f", $pi), '3.182598');
551 my $iter = new symbolic 2;
552 my $side = new symbolic 1;
556 $side = (sqrt(1 + $side**2) - 1)/$side;
558 my $pi = $side*(2**($iter+2));
559 test "$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]';
560 test( (sprintf "%f", $pi), '3.182598');
565 symbolic->vars($a, $b);
566 my $c = sqrt($a**2 + $b**2);
568 test( (sprintf "%d", $c), '5');
570 test( (sprintf "%d", $c), '13');
574 package symbolic1; # Primitive symbolic calculator
576 use overload nomethod => \&wrap, '""' => \&str, '0+' => \&num, '=' => \&cpy;
578 sub new { shift; bless ['n', @_] }
581 bless [@$self], ref $self;
584 my ($obj, $other, $inv, $meth) = @_;
585 if ($meth eq '++' or $meth eq '--') {
586 @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference
589 ($obj, $other) = ($other, $obj) if $inv;
590 bless [$meth, $obj, $other];
593 my ($meth, $a, $b) = @{+shift};
594 $a = 'u' unless defined $a;
601 my %subr = ( 'n' => sub {$_[0]} );
602 foreach my $op (split " ", $overload::ops{with_assign}) {
603 $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}";
605 my @bins = qw(binary 3way_comparison num_comparison str_comparison);
606 foreach my $op (split " ", "@overload::ops{ @bins }") {
607 $subr{$op} = eval "sub {shift() $op shift()}";
609 foreach my $op (split " ", "@overload::ops{qw(unary func)}") {
610 $subr{$op} = eval "sub {$op shift()}";
612 $subr{'++'} = $subr{'+'};
613 $subr{'--'} = $subr{'-'};
616 my ($meth, $a, $b) = @{+shift};
617 my $subr = $subr{$meth}
618 or die "Do not know how to ($meth) in symbolic";
619 $a = $a->num if ref $a eq __PACKAGE__;
620 $b = $b->num if ref $b eq __PACKAGE__;
623 sub TIESCALAR { my $pack = shift; $pack->new(@_) }
625 sub nop { } # Around a bug
626 sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; }
635 my $foo = new symbolic1 11;
637 test( (sprintf "%d", $foo), '12');
638 test( (sprintf "%d", $baz), '11');
641 test( (sprintf "%d", $foo), '13');
642 test( (sprintf "%d", $bar), '12');
643 test( (sprintf "%d", $baz), '13');
646 test( (sprintf "%d", $foo), '14');
647 test( (sprintf "%d", $bar), '12');
648 test( (sprintf "%d", $baz), '14');
649 test( (sprintf "%d", $ban), '13');
652 test( (sprintf "%d", $foo), '15');
653 test( (sprintf "%d", $baz), '14');
654 test( "$foo", '[++ [+= [++ [++ [n 11] 1] 1] 1] 1]');
658 my $iter = new symbolic1 2;
659 my $side = new symbolic1 1;
663 $cnt = $cnt - 1; # The "simple" way
664 $side = (sqrt(1 + $side**2) - 1)/$side;
666 my $pi = $side*(2**($iter+2));
667 test "$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]';
668 test( (sprintf "%f", $pi), '3.182598');
672 my $iter = new symbolic1 2;
673 my $side = new symbolic1 1;
677 $side = (sqrt(1 + $side**2) - 1)/$side;
679 my $pi = $side*(2**($iter+2));
680 test "$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]';
681 test( (sprintf "%f", $pi), '3.182598');
686 symbolic1->vars($a, $b);
687 my $c = sqrt($a**2 + $b**2);
689 test( (sprintf "%d", $c), '5');
691 test( (sprintf "%d", $c), '13');
695 package two_face; # Scalars with separate string and
697 sub new { my $p = shift; bless [@_], $p }
698 use overload '""' => \&str, '0+' => \&num, fallback => 1;
704 my $seven = new two_face ("vii", 7);
705 test( (sprintf "seven=$seven, seven=%d, eight=%d", $seven, $seven+1),
706 'seven=vii, seven=7, eight=8');
707 test( scalar ($seven =~ /i/), '1')
712 use overload 'cmp' => \∁
713 sub new { my ($p, $v) = @_; bless \$v, $p }
714 sub comp { my ($x,$y) = @_; ($$x * 3 % 10) <=> ($$y * 3 % 10) or $$x cmp $$y }
717 my @arr = map sorting->new($_), 0..12;
718 my @sorted1 = sort @arr;
719 my @sorted2 = map $$_, @sorted1;
720 test "@sorted2", '0 10 7 4 1 11 8 5 12 2 9 6 3';
724 use overload '<>' => \&iter;
725 sub new { my ($p, $v) = @_; bless \$v, $p }
726 sub iter { my ($x) = @_; return undef if $$x < 0; return $$x--; }
729 # XXX iterator overload not intended to work with CORE::GLOBAL?
730 if (defined &CORE::GLOBAL::glob) {
736 my $iter = iterator->new(5);
739 $acc .= " $out" while $out = <${iter}>;
740 test $acc, ' 5 4 3 2 1 0'; # 175
741 $iter = iterator->new(5);
742 test scalar <${iter}>, '5'; # 176
744 $acc .= " $out" while $out = <$iter>;
745 test $acc, ' 4 3 2 1 0'; # 177
749 use overload '%{}' => \&hderef, '&{}' => \&cderef,
750 '*{}' => \&gderef, '${}' => \&sderef, '@{}' => \&aderef;
751 sub new { my ($p, $v) = @_; bless \$v, $p }
753 my ($self, $key) = (shift, shift);
754 my $class = ref $self;
755 bless $self, 'deref::dummy'; # Disable overloading of %{}
756 my $out = $self->{$key};
757 bless $self, $class; # Restore overloading
760 sub hderef {shift->deref('h')}
761 sub aderef {shift->deref('a')}
762 sub cderef {shift->deref('c')}
763 sub gderef {shift->deref('g')}
764 sub sderef {shift->deref('s')}
767 my $deref = bless { h => { foo => 5 , fake => 23 },
768 c => sub {return shift() + 34},
774 my @cont = sort %$deref;
775 if ("\t" eq "\011") { # ascii
776 test "@cont", '23 5 fake foo'; # 178
778 else { # ebcdic alpha-numeric sort order
779 test "@cont", 'fake foo 23 5'; # 178
781 my @keys = sort keys %$deref;
782 test "@keys", 'fake foo'; # 179
783 my @val = sort values %$deref;
784 test "@val", '23 5'; # 180
785 test $deref->{foo}, 5; # 181
786 test defined $deref->{bar}, ''; # 182
789 push @keys, $key while $key = each %$deref;
791 test "@keys", 'fake foo'; # 183
792 test exists $deref->{bar}, ''; # 184
793 test exists $deref->{foo}, 1; # 185
795 test $deref->(5), 39; # 186
796 test &$deref(6), 40; # 187
797 sub xxx_goto { goto &$deref }
798 test xxx_goto(7), 41; # 188
799 my $srt = bless { c => sub {$b <=> $a}
802 my @sorted = sort srt 11, 2, 5, 1, 22;
803 test "@sorted", '22 11 5 2 1'; # 189
805 test $$deref, 123; # 190
807 @sorted = sort $srt 11, 2, 5, 1, 22;
808 test "@sorted", '22 11 5 2 1'; # 191
810 test "@$deref", '11 12 13'; # 192
811 test $#$deref, '2'; # 193
814 test $deref->[2], '13'; # 195
818 test $deref->[$l], '12'; # 197
819 # Repeated dereference
820 my $double = bless { h => $deref,
822 test $double->{foo}, 5; # 198
827 use overload '%{}' => \&gethash, '@{}' => sub { ${shift()} };
835 tie %h, ref $self, $self;
839 sub TIEHASH { my $p = shift; bless \ shift, $p }
842 $fields{$_} = $i++ foreach qw{zero one two three};
844 my $self = ${shift()};
845 my $key = $fields{shift()};
846 defined $key or die "Out of band access";
847 $$self->[$key] = shift;
850 my $self = ${shift()};
851 my $key = $fields{shift()};
852 defined $key or die "Out of band access";
857 my $bar = new two_refs 3,4,5,6;
859 test $bar->{two}, 11; # 199
861 test $bar->[3], 13; # 200
868 $bar = new two_refs_o 3,4,5,6;
870 test $bar->{two}, 11; # 201
872 test $bar->[3], 13; # 202
876 use overload '%{}' => sub { ${shift()}->[1] },
877 '@{}' => sub { ${shift()}->[0] };
883 bless \ [$a, \%h], $p;
888 tie %h, ref $self, $self;
892 sub TIEHASH { my $p = shift; bless \ shift, $p }
895 $fields{$_} = $i++ foreach qw{zero one two three};
898 my $key = $fields{shift()};
899 defined $key or die "Out of band access";
904 my $key = $fields{shift()};
905 defined $key or die "Out of band access";
910 $bar = new two_refs_o 3,4,5,6;
912 test $bar->{two}, 11; # 203
914 test $bar->[3], 13; # 204
918 @ISA = ('two_refs1');
921 $bar = new two_refs1_o 3,4,5,6;
923 test $bar->{two}, 11; # 205
925 test $bar->[3], 13; # 206
929 use overload bool => sub { ${+shift} };
933 { my $bbbb = 0; $aaa = bless \$bbbb, B }
938 test 'ok', 'ok'; # 208
940 test 'is not', 'ok'; # 208
943 # check that overload isn't done twice by join
946 use overload '""' => sub { $c++ };
947 my $x = join '', bless([]), 'pq', bless([]);
948 main::test $x, '0pq1'; # 209
951 # Test module-specific warning
953 # check the Odd number of arguments for overload::constant warning
955 local $SIG{__WARN__} = sub {$a = $_[0]} ;
956 $x = eval ' overload::constant "integer" ; ' ;
957 test($a eq "") ; # 210
958 use warnings 'overload' ;
959 $x = eval ' overload::constant "integer" ; ' ;
960 test($a =~ /^Odd number of arguments for overload::constant at/) ; # 211
964 # check the `$_[0]' is not an overloadable type warning
966 local $SIG{__WARN__} = sub {$a = $_[0]} ;
967 $x = eval ' overload::constant "fred" => sub {} ; ' ;
968 test($a eq "") ; # 212
969 use warnings 'overload' ;
970 $x = eval ' overload::constant "fred" => sub {} ; ' ;
971 test($a =~ /^`fred' is not an overloadable type at/); # 213
975 # check the `$_[1]' is not a code reference warning
977 local $SIG{__WARN__} = sub {$a = $_[0]} ;
978 $x = eval ' overload::constant "integer" => 1; ' ;
979 test($a eq "") ; # 214
980 use warnings 'overload' ;
981 $x = eval ' overload::constant "integer" => 1; ' ;
982 test($a =~ /^`1' is not a code reference at/); # 215
988 use overload '""' => sub { 3+shift->[0] },
989 '0+' => sub { 10+shift->[0] },
990 'int' => sub { 100+shift->[0] };
991 sub new {my $p = shift; bless [shift], $p}
994 use overload '""' => sub { 5+shift->[0] },
995 '0+' => sub { 30+shift->[0] },
996 'int' => sub { 'ov_int1'->new(1000+shift->[0]) };
997 sub new {my $p = shift; bless [shift], $p}
1000 use overload '""' => sub { 2+shift->[0] },
1001 '0+' => sub { 9+shift->[0] };
1002 sub new {my $p = shift; bless [shift], $p}
1006 my $x = new noov_int 11;
1008 main::test("$int_x" eq 20); # 216
1009 $x = new ov_int1 31;
1011 main::test("$int_x" eq 131); # 217
1012 $x = new ov_int2 51;
1014 main::test("$int_x" eq 1054); # 218
1017 # make sure that we don't inifinitely recurse
1021 use overload '""' => sub { shift },
1022 '0+' => sub { shift },
1023 'bool' => sub { shift },
1026 main::test("$x" =~ /Recurse=ARRAY/); # 219
1027 main::test($x); # 220
1028 main::test($x+0 =~ /Recurse=ARRAY/); # 221
1031 # BugID 20010422.003
1035 'bool' => sub { return !$_[0]->is_zero() || undef; }
1041 return $self->{var} == 0;
1048 $self->{var} = shift;
1056 my $r = Foo->new(8);
1059 test(($r || 0) == 0); # 222
1064 '""' => sub { return $_[0]->{var}; }
1071 $self->{var} = shift;
1078 my $utfvar = new utf8_o 200.2.1;
1079 test("$utfvar" eq 200.2.1); # 223 - stringify
1080 test("a$utfvar" eq "a".200.2.1); # 224 - overload via sv_2pv_flags
1082 # 225..227 -- more %{} tests. Hangs in 5.6.0, okay in later releases.
1083 # Basically this example implements strong encapsulation: if Hderef::import()
1084 # were to eval the overload code in the caller's namespace, the privatisation
1085 # would be quite transparent.
1087 use overload '%{}' => sub { (caller(0))[0] eq 'Foo' ? $_[0] : die "zap" };
1089 @Foo::ISA = 'Hderef';
1090 sub new { bless {}, shift }
1091 sub xet { @_ == 2 ? $_[0]->{$_[1]} :
1092 @_ == 3 ? ($_[0]->{$_[1]} = $_[2]) : undef }
1096 test ($a->xet('b'), 42);
1097 test (!defined eval { $a->{b} });
1102 use overload '=' => sub { 42 },
1103 '++' => sub { my $x = ${$_[0]}; $_[0] };
1104 sub new { my $x = 42; bless \$x }
1108 local $SIG{__WARN__} = sub { $warn++ };
1113 main::test (!$warn);
1117 my ($int, $out1, $out2);
1119 BEGIN { $int = 0; overload::constant 'integer' => sub {$int++; 17}; }
1123 test($int, 2, "#24313"); # 230
1124 test($out1, 17, "#24313"); # 231
1125 test($out2, 17, "#24313"); # 232
1130 use overload (qw(0+ numify fallback 1));
1137 sub numify { ${$_[0]} }
1142 use overload cmp => sub { 0 };
1143 package perl31793_fb;
1144 use overload cmp => sub { 0 }, fallback => 1;
1146 my $o = bless [], 'perl31793';
1147 my $of = bless [], 'perl31793_fb';
1148 my $no = bless [], 'no_overload';
1149 test (overload::StrVal(\"scalar") =~ /^SCALAR\(0x[0-9a-f]+\)$/);
1150 test (overload::StrVal([]) =~ /^ARRAY\(0x[0-9a-f]+\)$/);
1151 test (overload::StrVal({}) =~ /^HASH\(0x[0-9a-f]+\)$/);
1152 test (overload::StrVal(sub{1}) =~ /^CODE\(0x[0-9a-f]+\)$/);
1153 test (overload::StrVal(\*GLOB) =~ /^GLOB\(0x[0-9a-f]+\)$/);
1154 test (overload::StrVal(\$o) =~ /^REF\(0x[0-9a-f]+\)$/);
1155 test (overload::StrVal(qr/a/) =~ /^Regexp=SCALAR\(0x[0-9a-f]+\)$/);
1156 test (overload::StrVal($o) =~ /^perl31793=ARRAY\(0x[0-9a-f]+\)$/);
1157 test (overload::StrVal($of) =~ /^perl31793_fb=ARRAY\(0x[0-9a-f]+\)$/);
1158 test (overload::StrVal($no) =~ /^no_overload=ARRAY\(0x[0-9a-f]+\)$/);
1161 # These are all check that overloaded values rather than reference addressess
1162 # are what is getting tested.
1163 my ($two, $one, $un, $deux) = map {new Numify $_} 2, 1, 1, 2;
1164 my ($ein, $zwei) = (1, 2);
1166 my %map = (one => 1, un => 1, ein => 1, deux => 2, two => 2, zwei => 2);
1167 foreach my $op (qw(<=> == != < <= > >=)) {
1168 foreach my $l (keys %map) {
1169 foreach my $r (keys %map) {
1170 my $ocode = "\$$l $op \$$r";
1171 my $rcode = "$map{$l} $op $map{$r}";
1173 my $got = eval $ocode;
1175 my $expect = eval $rcode;
1177 test ($got, $expect, $ocode) or print "# $rcode\n";
1182 # check that overloading works in regexes
1186 '""' => sub { "^$_[0][0]\$" },
1190 ? (ref $_[1] ? $_[1][0] : $_[1]) . ':' .$_[0][0]
1191 : $_[0][0] . ':' . (ref $_[1] ? $_[1][0] : $_[1])
1196 my $a = bless [ "a" ], 'Foo493';
1198 test('x:a' =~ /x$a/);
1199 test('x:a:=' =~ /x$a=$/);
1200 test('x:a:a:=' =~ /x$a$a=$/);