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
50 use Test::More tests => 522;
53 $a = new Oscalar "087";
58 is(ref $a, "Oscalar");
64 is(ref $c, "Oscalar");
70 is(ref $a, "Oscalar");
74 is(ref $b, "Oscalar");
77 is(ref $a, "Oscalar");
82 is(ref $c, "Oscalar");
85 is(ref $a, "Oscalar");
90 is(ref $b, "Oscalar");
93 is(ref $a, "Oscalar");
95 eval q[ package Oscalar; use overload ('++' => sub { $ {$_[0]}++;$_[0] } ) ];
99 is(ref $a, "Oscalar");
103 is(ref $b, "Oscalar");
106 is(ref $a, "Oscalar");
109 $dummy=bless \$dummy; # Now cache of method should be reloaded
115 is(ref $b, "Oscalar");
118 is(ref $a, "Oscalar");
120 undef $b; # Destroying updates tables too...
122 eval q[package Oscalar; use overload ('++' => sub { $ {$_[0]} += 2; $_[0] } ) ];
126 is(ref $a, "Oscalar");
130 is(ref $b, "Oscalar");
133 is(ref $a, "Oscalar");
136 $dummy=bless \$dummy; # Now cache of method should be reloaded
141 is(ref $b, "Oscalar");
144 is(ref $a, "Oscalar");
149 is(ref $b, "Oscalar");
152 is(ref $a, "Oscalar");
157 eval q[ package Oscalar; use overload ('=' => sub {$main::copies++;
159 local $new=$ {$_[0]};
164 is(ref $b, "Oscalar");
167 is(ref $a, "Oscalar");
171 is(ref $b, "Oscalar");
174 is(ref $a, "Oscalar");
179 is(ref $b, "Oscalar");
182 is(ref $a, "Oscalar");
188 is(ref $b, "Oscalar");
191 is(ref $a, "Oscalar");
197 is(ref $b, "Oscalar");
200 is(ref $a, "Oscalar");
203 eval q[package Oscalar; use overload ('+=' => sub {$ {$_[0]} += 3*$_[1];
205 $c=new Oscalar; # Cause rehash
210 is(ref $b, "Oscalar");
213 is(ref $a, "Oscalar");
218 is(ref $b, "Oscalar");
223 is(ref $b, "Oscalar");
229 is(ref $b, "Oscalar");
235 is(ref $b, "Oscalar");
239 eval q[package Oscalar;
240 use overload ('x' => sub {new Oscalar ( $_[2] ? "_.$_[1]._" x $ {$_[0]}
241 : "_.${$_[0]}._" x $_[1])}) ];
245 is($a, "_.yy.__.yy.__.yy._");
247 eval q[package Oscalar;
248 use overload ('.' => sub {new Oscalar ( $_[2] ?
249 "_.$_[1].__.$ {$_[0]}._"
250 : "_.$ {$_[0]}.__.$_[1]._")}) ];
254 is("b${a}c", "_._.b.__.xx._.__.c._");
256 # Check inheritance of overloading;
262 $aI = new OscalarI "$a";
263 is(ref $aI, "OscalarI");
266 is("b${aI}c", "_._.b.__.xx._.__.c._");
268 # Here we test blessing to a package updates hash
270 eval "package Oscalar; no overload '.'";
272 is("b${a}", "_.b.__.xx._");
275 is("b${a}c", "bxxc");
277 is("b${a}c", "bxxc");
279 # Negative overloading:
282 like($@, qr/no method found/);
287 sub { *{"Oscalar::$AUTOLOAD"} = sub {"_!_" . shift() . "_!_"} ;
288 goto &{"Oscalar::$AUTOLOAD"}};
290 eval "package Oscalar; sub comple; use overload '~' => 'comple'";
292 $na = eval { ~$a }; # Hash was not updated
293 like($@, qr/no method found/);
297 $na = eval { ~$a }; # Hash updated
298 warn "`$na', $@" if $@;
304 $na = eval { ~$aI }; # Hash was not updated
305 like($@, qr/no method found/);
315 eval "package Oscalar; sub rshft; use overload '>>' => 'rshft'";
317 $na = eval { $aI >> 1 }; # Hash was not updated
318 like($@, qr/no method found/);
324 $na = eval { $aI >> 1 };
330 # warn overload::Method($a, '0+'), "\n";
331 is(overload::Method($a, '0+'), \&Oscalar::numify);
332 is(overload::Method($aI,'0+'), \&Oscalar::numify);
333 ok(overload::Overloaded($aI));
334 ok(!overload::Overloaded('overload'));
336 ok(! defined overload::Method($aI, '<<'));
337 ok(! defined overload::Method($a, '<'));
339 like (overload::StrVal($aI), qr/^OscalarI=SCALAR\(0x[\da-fA-F]+\)$/);
340 is(overload::StrVal(\$aI), "@{[\$aI]}");
342 # Check overloading by methods (specified deep in the ISA tree).
346 sub Oscalar::lshft {"_<<_" . shift() . "_<<_"}
347 eval "package OscalarI; use overload '<<' => 'lshft', '|' => 'lshft'";
352 bless $aII, 'OscalarII';
353 bless \$fake, 'OscalarI'; # update the hash
354 is(($aI | 3), '_<<_xx_<<_');
356 is(($aII << 3), '_<<_087_<<_');
359 BEGIN { $int = 7; overload::constant 'integer' => sub {$int++; shift}; }
368 BEGIN { $q = $qr = 7;
369 overload::constant 'q' => sub {$q++; push @q, shift, ($_[1] || 'none'); shift},
370 'qr' => sub {$qr++; push @qr, shift, ($_[1] || 'none'); shift}; }
373 $out2 = "a\a$foo,\,";
379 is($out1, 'f\'o\\o');
381 is($out2, "a\afoo,\,");
382 is("@q", "foo q f'o\\\\o q a\\a qq ,\\, qq");
384 is("@qr", "b\\b qq .\\. qq");
388 $_ = '!<b>!foo!<-.>!';
389 BEGIN { overload::constant 'q' => sub {push @q1, shift, ($_[1] || 'none'); "_<" . (shift) . ">_"},
390 'qr' => sub {push @qr1, shift, ($_[1] || 'none'); "!<" . (shift) . ">!"}; }
393 $out2 = "a\a$foo,\,";
403 s'first part'second part';
404 s/yet another/tail here/;
409 is($out1, '_<f\'o\\o>_');
410 is($out2, "_<a\a>_foo_<,\,>_");
411 is("@q1", "foo q f'o\\\\o q a\\a qq ,\\, qq oups
413 q second part q tail here s A-Z tr a-z tr");
414 is("@qr1", "b\\b qq .\\. qq try it q first part q yet another qq");
423 package symbolic; # Primitive symbolic calculator
424 use overload nomethod => \&wrap, '""' => \&str, '0+' => \&num,
425 '=' => \&cpy, '++' => \&inc, '--' => \&dec;
427 sub new { shift; bless ['n', @_] }
430 bless [@$self], ref $self;
432 sub inc { $_[0] = bless ['++', $_[0], 1]; }
433 sub dec { $_[0] = bless ['--', $_[0], 1]; }
435 my ($obj, $other, $inv, $meth) = @_;
436 if ($meth eq '++' or $meth eq '--') {
437 @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference
440 ($obj, $other) = ($other, $obj) if $inv;
441 bless [$meth, $obj, $other];
444 my ($meth, $a, $b) = @{+shift};
445 $a = 'u' unless defined $a;
452 my %subr = ( 'n' => sub {$_[0]} );
453 foreach my $op (split " ", $overload::ops{with_assign}) {
454 $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}";
456 my @bins = qw(binary 3way_comparison num_comparison str_comparison);
457 foreach my $op (split " ", "@overload::ops{ @bins }") {
458 $subr{$op} = eval "sub {shift() $op shift()}";
460 foreach my $op (split " ", "@overload::ops{qw(unary func)}") {
461 $subr{$op} = eval "sub {$op shift()}";
463 $subr{'++'} = $subr{'+'};
464 $subr{'--'} = $subr{'-'};
467 my ($meth, $a, $b) = @{+shift};
468 my $subr = $subr{$meth}
469 or die "Do not know how to ($meth) in symbolic";
470 $a = $a->num if ref $a eq __PACKAGE__;
471 $b = $b->num if ref $b eq __PACKAGE__;
474 sub TIESCALAR { my $pack = shift; $pack->new(@_) }
476 sub nop { } # Around a bug
477 sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; }
486 my $foo = new symbolic 11;
488 is((sprintf "%d", $foo), '12');
489 is((sprintf "%d", $baz), '11');
492 is((sprintf "%d", $foo), '13');
493 is((sprintf "%d", $bar), '12');
494 is((sprintf "%d", $baz), '13');
497 is((sprintf "%d", $foo), '14');
498 is((sprintf "%d", $bar), '12');
499 is((sprintf "%d", $baz), '14');
500 is((sprintf "%d", $ban), '13');
503 is((sprintf "%d", $foo), '15');
504 is((sprintf "%d", $baz), '14');
505 is("$foo", '[++ [+= [++ [++ [n 11] 1] 1] 1] 1]');
509 my $iter = new symbolic 2;
510 my $side = new symbolic 1;
514 $cnt = $cnt - 1; # The "simple" way
515 $side = (sqrt(1 + $side**2) - 1)/$side;
517 my $pi = $side*(2**($iter+2));
518 is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]');
519 is((sprintf "%f", $pi), '3.182598');
523 my $iter = new symbolic 2;
524 my $side = new symbolic 1;
528 $side = (sqrt(1 + $side**2) - 1)/$side;
530 my $pi = $side*(2**($iter+2));
531 is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]');
532 is((sprintf "%f", $pi), '3.182598');
537 symbolic->vars($a, $b);
538 my $c = sqrt($a**2 + $b**2);
540 is((sprintf "%d", $c), '5');
542 is((sprintf "%d", $c), '13');
546 package symbolic1; # Primitive symbolic calculator
548 use overload nomethod => \&wrap, '""' => \&str, '0+' => \&num, '=' => \&cpy;
550 sub new { shift; bless ['n', @_] }
553 bless [@$self], ref $self;
556 my ($obj, $other, $inv, $meth) = @_;
557 if ($meth eq '++' or $meth eq '--') {
558 @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference
561 ($obj, $other) = ($other, $obj) if $inv;
562 bless [$meth, $obj, $other];
565 my ($meth, $a, $b) = @{+shift};
566 $a = 'u' unless defined $a;
573 my %subr = ( 'n' => sub {$_[0]} );
574 foreach my $op (split " ", $overload::ops{with_assign}) {
575 $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}";
577 my @bins = qw(binary 3way_comparison num_comparison str_comparison);
578 foreach my $op (split " ", "@overload::ops{ @bins }") {
579 $subr{$op} = eval "sub {shift() $op shift()}";
581 foreach my $op (split " ", "@overload::ops{qw(unary func)}") {
582 $subr{$op} = eval "sub {$op shift()}";
584 $subr{'++'} = $subr{'+'};
585 $subr{'--'} = $subr{'-'};
588 my ($meth, $a, $b) = @{+shift};
589 my $subr = $subr{$meth}
590 or die "Do not know how to ($meth) in symbolic";
591 $a = $a->num if ref $a eq __PACKAGE__;
592 $b = $b->num if ref $b eq __PACKAGE__;
595 sub TIESCALAR { my $pack = shift; $pack->new(@_) }
597 sub nop { } # Around a bug
598 sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; }
607 my $foo = new symbolic1 11;
609 is((sprintf "%d", $foo), '12');
610 is((sprintf "%d", $baz), '11');
613 is((sprintf "%d", $foo), '13');
614 is((sprintf "%d", $bar), '12');
615 is((sprintf "%d", $baz), '13');
618 is((sprintf "%d", $foo), '14');
619 is((sprintf "%d", $bar), '12');
620 is((sprintf "%d", $baz), '14');
621 is((sprintf "%d", $ban), '13');
624 is((sprintf "%d", $foo), '15');
625 is((sprintf "%d", $baz), '14');
626 is("$foo", '[++ [+= [++ [++ [n 11] 1] 1] 1] 1]');
630 my $iter = new symbolic1 2;
631 my $side = new symbolic1 1;
635 $cnt = $cnt - 1; # The "simple" way
636 $side = (sqrt(1 + $side**2) - 1)/$side;
638 my $pi = $side*(2**($iter+2));
639 is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]');
640 is((sprintf "%f", $pi), '3.182598');
644 my $iter = new symbolic1 2;
645 my $side = new symbolic1 1;
649 $side = (sqrt(1 + $side**2) - 1)/$side;
651 my $pi = $side*(2**($iter+2));
652 is("$side", '[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]] 2]]] 1] [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]');
653 is((sprintf "%f", $pi), '3.182598');
658 symbolic1->vars($a, $b);
659 my $c = sqrt($a**2 + $b**2);
661 is((sprintf "%d", $c), '5');
663 is((sprintf "%d", $c), '13');
667 package two_face; # Scalars with separate string and
669 sub new { my $p = shift; bless [@_], $p }
670 use overload '""' => \&str, '0+' => \&num, fallback => 1;
676 my $seven = new two_face ("vii", 7);
677 is((sprintf "seven=$seven, seven=%d, eight=%d", $seven, $seven+1),
678 'seven=vii, seven=7, eight=8');
679 is(scalar ($seven =~ /i/), '1');
684 use overload 'cmp' => \∁
685 sub new { my ($p, $v) = @_; bless \$v, $p }
686 sub comp { my ($x,$y) = @_; ($$x * 3 % 10) <=> ($$y * 3 % 10) or $$x cmp $$y }
689 my @arr = map sorting->new($_), 0..12;
690 my @sorted1 = sort @arr;
691 my @sorted2 = map $$_, @sorted1;
692 is("@sorted2", '0 10 7 4 1 11 8 5 12 2 9 6 3');
696 use overload '<>' => \&iter;
697 sub new { my ($p, $v) = @_; bless \$v, $p }
698 sub iter { my ($x) = @_; return undef if $$x < 0; return $$x--; }
701 # XXX iterator overload not intended to work with CORE::GLOBAL?
702 if (defined &CORE::GLOBAL::glob) {
708 my $iter = iterator->new(5);
711 $acc .= " $out" while $out = <${iter}>;
712 is($acc, ' 5 4 3 2 1 0');
713 $iter = iterator->new(5);
714 is(scalar <${iter}>, '5');
716 $acc .= " $out" while $out = <$iter>;
717 is($acc, ' 4 3 2 1 0');
721 use overload '%{}' => \&hderef, '&{}' => \&cderef,
722 '*{}' => \&gderef, '${}' => \&sderef, '@{}' => \&aderef;
723 sub new { my ($p, $v) = @_; bless \$v, $p }
725 my ($self, $key) = (shift, shift);
726 my $class = ref $self;
727 bless $self, 'deref::dummy'; # Disable overloading of %{}
728 my $out = $self->{$key};
729 bless $self, $class; # Restore overloading
732 sub hderef {shift->deref('h')}
733 sub aderef {shift->deref('a')}
734 sub cderef {shift->deref('c')}
735 sub gderef {shift->deref('g')}
736 sub sderef {shift->deref('s')}
739 my $deref = bless { h => { foo => 5 , fake => 23 },
740 c => sub {return shift() + 34},
746 my @cont = sort %$deref;
747 if ("\t" eq "\011") { # ascii
748 is("@cont", '23 5 fake foo');
750 else { # ebcdic alpha-numeric sort order
751 is("@cont", 'fake foo 23 5');
753 my @keys = sort keys %$deref;
754 is("@keys", 'fake foo');
755 my @val = sort values %$deref;
757 is($deref->{foo}, 5);
758 is(defined $deref->{bar}, '');
761 push @keys, $key while $key = each %$deref;
763 is("@keys", 'fake foo');
764 is(exists $deref->{bar}, '');
765 is(exists $deref->{foo}, 1);
769 sub xxx_goto { goto &$deref }
771 my $srt = bless { c => sub {$b <=> $a}
774 my @sorted = sort srt 11, 2, 5, 1, 22;
775 is("@sorted", '22 11 5 2 1');
779 @sorted = sort $srt 11, 2, 5, 1, 22;
780 is("@sorted", '22 11 5 2 1');
782 is("@$deref", '11 12 13');
786 is($deref->[2], '13');
790 is($deref->[$l], '12');
791 # Repeated dereference
792 my $double = bless { h => $deref,
794 is($double->{foo}, 5);
799 use overload '%{}' => \&gethash, '@{}' => sub { ${shift()} };
807 tie %h, ref $self, $self;
811 sub TIEHASH { my $p = shift; bless \ shift, $p }
814 $fields{$_} = $i++ foreach qw{zero one two three};
816 my $self = ${shift()};
817 my $key = $fields{shift()};
818 defined $key or die "Out of band access";
819 $$self->[$key] = shift;
822 my $self = ${shift()};
823 my $key = $fields{shift()};
824 defined $key or die "Out of band access";
829 my $bar = new two_refs 3,4,5,6;
840 $bar = new two_refs_o 3,4,5,6;
848 use overload '%{}' => sub { ${shift()}->[1] },
849 '@{}' => sub { ${shift()}->[0] };
855 bless \ [$a, \%h], $p;
860 tie %h, ref $self, $self;
864 sub TIEHASH { my $p = shift; bless \ shift, $p }
867 $fields{$_} = $i++ foreach qw{zero one two three};
870 my $key = $fields{shift()};
871 defined $key or die "Out of band access";
876 my $key = $fields{shift()};
877 defined $key or die "Out of band access";
882 $bar = new two_refs_o 3,4,5,6;
890 @ISA = ('two_refs1');
893 $bar = new two_refs1_o 3,4,5,6;
901 use overload bool => sub { ${+shift} };
905 { my $bbbb = 0; $aaa = bless \$bbbb, B }
915 # check that overload isn't done twice by join
918 use overload '""' => sub { $c++ };
919 my $x = join '', bless([]), 'pq', bless([]);
923 # Test module-specific warning
925 # check the Odd number of arguments for overload::constant warning
927 local $SIG{__WARN__} = sub {$a = $_[0]} ;
928 $x = eval ' overload::constant "integer" ; ' ;
930 use warnings 'overload' ;
931 $x = eval ' overload::constant "integer" ; ' ;
932 like($a, qr/^Odd number of arguments for overload::constant at/);
936 # check the `$_[0]' is not an overloadable type warning
938 local $SIG{__WARN__} = sub {$a = $_[0]} ;
939 $x = eval ' overload::constant "fred" => sub {} ; ' ;
941 use warnings 'overload' ;
942 $x = eval ' overload::constant "fred" => sub {} ; ' ;
943 like($a, qr/^`fred' is not an overloadable type at/);
947 # check the `$_[1]' is not a code reference warning
949 local $SIG{__WARN__} = sub {$a = $_[0]} ;
950 $x = eval ' overload::constant "integer" => 1; ' ;
952 use warnings 'overload' ;
953 $x = eval ' overload::constant "integer" => 1; ' ;
954 like($a, qr/^`1' is not a code reference at/);
960 use overload '""' => sub { 3+shift->[0] },
961 '0+' => sub { 10+shift->[0] },
962 'int' => sub { 100+shift->[0] };
963 sub new {my $p = shift; bless [shift], $p}
966 use overload '""' => sub { 5+shift->[0] },
967 '0+' => sub { 30+shift->[0] },
968 'int' => sub { 'ov_int1'->new(1000+shift->[0]) };
969 sub new {my $p = shift; bless [shift], $p}
972 use overload '""' => sub { 2+shift->[0] },
973 '0+' => sub { 9+shift->[0] };
974 sub new {my $p = shift; bless [shift], $p}
978 my $x = new noov_int 11;
980 main::is("$int_x", 20);
983 main::is("$int_x", 131);
986 main::is("$int_x", 1054);
989 # make sure that we don't inifinitely recurse
993 use overload '""' => sub { shift },
994 '0+' => sub { shift },
995 'bool' => sub { shift },
998 # For some reason beyond me these have to be oks rather than likes.
999 main::ok("$x" =~ /Recurse=ARRAY/);
1001 main::ok($x+0 =~ qr/Recurse=ARRAY/);
1004 # BugID 20010422.003
1008 'bool' => sub { return !$_[0]->is_zero() || undef; }
1014 return $self->{var} == 0;
1021 $self->{var} = shift;
1029 my $r = Foo->new(8);
1037 '""' => sub { return $_[0]->{var}; }
1044 $self->{var} = shift;
1051 my $utfvar = new utf8_o 200.2.1;
1052 is("$utfvar", 200.2.1); # 223 - stringify
1053 is("a$utfvar", "a".200.2.1); # 224 - overload via sv_2pv_flags
1055 # 225..227 -- more %{} tests. Hangs in 5.6.0, okay in later releases.
1056 # Basically this example implements strong encapsulation: if Hderef::import()
1057 # were to eval the overload code in the caller's namespace, the privatisation
1058 # would be quite transparent.
1060 use overload '%{}' => sub { (caller(0))[0] eq 'Foo' ? $_[0] : die "zap" };
1062 @Foo::ISA = 'Hderef';
1063 sub new { bless {}, shift }
1064 sub xet { @_ == 2 ? $_[0]->{$_[1]} :
1065 @_ == 3 ? ($_[0]->{$_[1]} = $_[2]) : undef }
1069 is ($a->xet('b'), 42);
1070 ok (!defined eval { $a->{b} });
1075 use overload '=' => sub { 42 },
1076 '++' => sub { my $x = ${$_[0]}; $_[0] };
1077 sub new { my $x = 42; bless \$x }
1081 local $SIG{__WARN__} = sub { $warn++ };
1090 my ($int, $out1, $out2);
1092 BEGIN { $int = 0; overload::constant 'integer' => sub {$int++; 17}; }
1096 is($int, 2, "#24313"); # 230
1097 is($out1, 17, "#24313"); # 231
1098 is($out2, 17, "#24313"); # 232
1103 use overload (qw(0+ numify fallback 1));
1110 sub numify { ${$_[0]} }
1115 use overload cmp => sub { 0 };
1116 package perl31793_fb;
1117 use overload cmp => sub { 0 }, fallback => 1;
1119 my $o = bless [], 'perl31793';
1120 my $of = bless [], 'perl31793_fb';
1121 my $no = bless [], 'no_overload';
1122 like(overload::StrVal(\"scalar"), qr/^SCALAR\(0x[0-9a-f]+\)$/);
1123 like(overload::StrVal([]), qr/^ARRAY\(0x[0-9a-f]+\)$/);
1124 like(overload::StrVal({}), qr/^HASH\(0x[0-9a-f]+\)$/);
1125 like(overload::StrVal(sub{1}), qr/^CODE\(0x[0-9a-f]+\)$/);
1126 like(overload::StrVal(\*GLOB), qr/^GLOB\(0x[0-9a-f]+\)$/);
1127 like(overload::StrVal(\$o), qr/^REF\(0x[0-9a-f]+\)$/);
1128 like(overload::StrVal(qr/a/), qr/^Regexp=SCALAR\(0x[0-9a-f]+\)$/);
1129 like(overload::StrVal($o), qr/^perl31793=ARRAY\(0x[0-9a-f]+\)$/);
1130 like(overload::StrVal($of), qr/^perl31793_fb=ARRAY\(0x[0-9a-f]+\)$/);
1131 like(overload::StrVal($no), qr/^no_overload=ARRAY\(0x[0-9a-f]+\)$/);
1134 # These are all check that overloaded values rather than reference addressess
1135 # are what is getting tested.
1136 my ($two, $one, $un, $deux) = map {new Numify $_} 2, 1, 1, 2;
1137 my ($ein, $zwei) = (1, 2);
1139 my %map = (one => 1, un => 1, ein => 1, deux => 2, two => 2, zwei => 2);
1140 foreach my $op (qw(<=> == != < <= > >=)) {
1141 foreach my $l (keys %map) {
1142 foreach my $r (keys %map) {
1143 my $ocode = "\$$l $op \$$r";
1144 my $rcode = "$map{$l} $op $map{$r}";
1146 my $got = eval $ocode;
1148 my $expect = eval $rcode;
1150 is ($got, $expect, $ocode) or print "# $rcode\n";
1155 # check that overloading works in regexes
1159 '""' => sub { "^$_[0][0]\$" },
1163 ? (ref $_[1] ? $_[1][0] : $_[1]) . ':' .$_[0][0]
1164 : $_[0][0] . ':' . (ref $_[1] ? $_[1][0] : $_[1])
1169 my $a = bless [ "a" ], 'Foo493';
1171 like('x:a', qr/x$a/);
1172 like('x:a:=', qr/x$a=$/);
1173 like('x:a:a:=', qr/x$a$a=$/);
1178 my $twenty_three = 23;
1179 # Check that constant overloading propagates into evals
1180 BEGIN { overload::constant integer => sub { 23 } }
1181 is(eval "17", $twenty_three);
1187 bool => sub { shift->is_cool };
1190 $_[0]->{name} eq 'cool';
1195 bless $_[0], 'Brap';
1199 sub delete_with_self {
1202 bless $self, 'Brap';
1213 $obj = bless {name => 'cool'}, 'Sklorsh';
1215 ok(eval {if ($obj) {1}; 1}, $@ || 'reblessed into nonexist namespace');
1217 $obj = bless {name => 'cool'}, 'Sklorsh';
1218 $obj->delete_with_self;
1219 ok (eval {if ($obj) {1}; 1}, $@);
1221 my $a = $b = {name => 'hot'};
1222 bless $b, 'Sklorsh';
1223 is(ref $a, 'Sklorsh');
1224 is(ref $b, 'Sklorsh');
1225 ok(!$b, "Expect overloaded boolean");
1226 ok(!$a, "Expect overloaded boolean");
1229 use Scalar::Util 'weaken';
1232 use overload '""' => sub {"CLiK KLAK"};
1239 $obj = bless do {my $a; \$a}, 'Shklitza';
1242 is ($obj, "CLiK KLAK");
1243 is ($ref, "CLiK KLAK");
1246 is ($ref, "CLiK KLAK");
1248 bless $obj, 'Ksshfwoom';
1250 like ($obj, qr/^Ksshfwoom=/);
1251 like ($ref, qr/^Ksshfwoom=/);
1259 # bit operations have overloadable assignment variants too
1261 sub new { bless \$_[1], $_[0] }
1264 "&=" => sub { bit->new($_[0]->val . ' & ' . $_[1]->val) },
1265 "^=" => sub { bit->new($_[0]->val . ' ^ ' . $_[1]->val) },
1266 "|" => sub { bit->new($_[0]->val . ' | ' . $_[1]->val) }, # |= by fallback
1269 sub val { ${$_[0]} }
1273 my $a = bit->new(my $va = 'a');
1274 my $b = bit->new(my $vb = 'b');
1277 is($a->val, 'a & b', "overloaded &= works");
1279 my $c = bit->new(my $vc = 'c');
1282 is($b->val, 'b ^ c', "overloaded ^= works");
1284 my $d = bit->new(my $vd = 'd');
1287 is($c->val, 'c | d', "overloaded |= (by fallback) works");
1291 # comparison operators with nomethod
1295 package nomethod_false;
1296 use overload nomethod => sub { $method = 'nomethod'; 0 };
1298 package nomethod_true;
1299 use overload nomethod => sub { $method= 'nomethod'; 'true' };
1303 local $SIG{__WARN__} = sub { $warning = $_[0] };
1305 my $f = bless [], 'nomethod_false';
1306 ($warning, $method) = ("", "");
1307 is($f eq 'whatever', 0, 'nomethod makes eq return 0');
1308 is($method, 'nomethod');
1310 my $t = bless [], 'nomethod_true';
1311 ($warning, $method) = ("", "");
1312 is($t eq 'whatever', 'true', 'nomethod makes eq return "true"');
1313 is($method, 'nomethod');
1314 is($warning, "", 'nomethod eq need not return number');
1317 package nomethod_false;
1318 use overload cmp => sub { $method = 'cmp'; 0 };
1320 $f = bless [], 'nomethod_false';
1321 ($warning, $method) = ("", "");
1322 ok($f eq 'whatever', 'eq falls back to cmp (nomethod not called)');
1326 package nomethod_true;
1327 use overload cmp => sub { $method = 'cmp'; 'true' };
1329 $t = bless [], 'nomethod_true';
1330 ($warning, $method) = ("", "");
1331 ok($t eq 'whatever', 'eq falls back to cmp (nomethod not called)');
1333 like($warning, qr/isn't numeric/, 'cmp should return number');