This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make pp_reverse fetch the lexical $_ from the correct pad
[perl5.git] / lib / overloading.t
CommitLineData
e46c382e
YK
1#./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8BEGIN {
9 require "./test.pl";
abb619ad 10 plan(tests => 35);
e46c382e
YK
11}
12
13use Scalar::Util qw(refaddr);
14
15{
16 package Stringifies;
17
18 use overload (
19 fallback => 1,
20 '""' => sub { "foo" },
21 '0+' => sub { 42 },
abb619ad 22 cos => sub { "far side of overload table" },
e46c382e
YK
23 );
24
25 sub new { bless {}, shift };
26}
27
28my $x = Stringifies->new;
29
30is( "$x", "foo", "stringifies" );
31is( 0 + $x, 42, "numifies" );
abb619ad 32is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
33
34{
35 no overloading;
36 is( "$x", overload::StrVal($x), "no stringification" );
37 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 38 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
39
40 {
41 no overloading '""';
42 is( "$x", overload::StrVal($x), "no stringification" );
43 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 44 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
45 }
46}
47
48{
49 no overloading '""';
50
51 is( "$x", overload::StrVal($x), "no stringification" );
52 is( 0 + $x, 42, "numifies" );
abb619ad 53 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
54
55 {
56 no overloading;
57 is( "$x", overload::StrVal($x), "no stringification" );
58 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 59 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
60 }
61
62 use overloading '""';
63
64 is( "$x", "foo", "stringifies" );
65 is( 0 + $x, 42, "numifies" );
abb619ad 66 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
67
68 no overloading '0+';
69 is( "$x", "foo", "stringifies" );
70 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 71 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
72
73 {
74 no overloading '""';
75 is( "$x", overload::StrVal($x), "no stringification" );
76 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 77 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
78
79 {
80 use overloading;
81 is( "$x", "foo", "stringifies" );
82 is( 0 + $x, 42, "numifies" );
abb619ad 83 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e
YK
84 }
85 }
86
87 is( "$x", "foo", "stringifies" );
88 is( 0 + $x, refaddr($x), "no numification" );
abb619ad 89 is( cos($x), "far side of overload table", "cosinusfies" );
e46c382e 90
abb619ad
RGS
91 no overloading "cos";
92 is( "$x", "foo", "stringifies" );
93 is( 0 + $x, refaddr($x), "no numification" );
94 is( cos($x), cos(refaddr($x)), "no cosinusfication" );
e46c382e
YK
95
96 BEGIN { ok(exists($^H{overloading}), "overloading hint present") }
97
98 use overloading;
99
100 BEGIN { ok(!exists($^H{overloading}), "overloading hint removed") }
101}