Commit | Line | Data |
7a6a85bf |
1 | #!./perl |
2 | |
b12202d0 |
3 | # $Id: overload.t,v 1.0.1.1 2001/02/17 12:27:22 ram Exp $ |
7a6a85bf |
4 | # |
5 | # Copyright (c) 1995-2000, Raphael Manfredi |
6 | # |
9e21b3d0 |
7 | # You may redistribute only under the same terms as Perl 5, as specified |
8 | # in the README file that comes with the distribution. |
7a6a85bf |
9 | # |
10 | # $Log: overload.t,v $ |
b12202d0 |
11 | # Revision 1.0.1.1 2001/02/17 12:27:22 ram |
12 | # patch8: added test for structures with indirect ref to overloaded |
13 | # |
9e21b3d0 |
14 | # Revision 1.0 2000/09/01 19:40:42 ram |
15 | # Baseline for first official release. |
7a6a85bf |
16 | # |
17 | |
18 | sub BEGIN { |
0c384302 |
19 | if ($ENV{PERL_CORE}){ |
20 | chdir('t') if -d 't'; |
21 | @INC = '.'; |
22 | push @INC, '../lib'; |
23 | } |
9f233367 |
24 | require Config; import Config; |
0c384302 |
25 | if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) { |
9f233367 |
26 | print "1..0 # Skip: Storable was not built\n"; |
27 | exit 0; |
28 | } |
7a6a85bf |
29 | require 'lib/st-dump.pl'; |
30 | } |
31 | |
32 | sub ok; |
33 | |
34 | use Storable qw(freeze thaw); |
35 | |
b12202d0 |
36 | print "1..12\n"; |
7a6a85bf |
37 | |
38 | package OVERLOADED; |
39 | |
40 | use overload |
41 | '""' => sub { $_[0][0] }; |
42 | |
43 | package main; |
44 | |
45 | $a = bless [77], OVERLOADED; |
46 | |
47 | $b = thaw freeze $a; |
48 | ok 1, ref $b eq 'OVERLOADED'; |
49 | ok 2, "$b" eq "77"; |
50 | |
51 | $c = thaw freeze \$a; |
52 | ok 3, ref $c eq 'REF'; |
53 | ok 4, ref $$c eq 'OVERLOADED'; |
54 | ok 5, "$$c" eq "77"; |
55 | |
56 | $d = thaw freeze [$a, $a]; |
57 | ok 6, "$d->[0]" eq "77"; |
58 | $d->[0][0]++; |
59 | ok 7, "$d->[1]" eq "78"; |
60 | |
b12202d0 |
61 | package REF_TO_OVER; |
62 | |
63 | sub make { |
64 | my $self = bless {}, shift; |
65 | my ($over) = @_; |
66 | $self->{over} = $over; |
67 | return $self; |
68 | } |
69 | |
70 | package OVER; |
71 | |
72 | use overload |
73 | '+' => \&plus, |
74 | '""' => sub { ref $_[0] }; |
75 | |
76 | sub plus { |
77 | return 314; |
78 | } |
79 | |
80 | sub make { |
81 | my $self = bless {}, shift; |
82 | my $ref = REF_TO_OVER->make($self); |
83 | $self->{ref} = $ref; |
84 | return $self; |
85 | } |
86 | |
87 | package main; |
88 | |
89 | $a = OVER->make(); |
90 | $b = thaw freeze $a; |
91 | |
92 | ok 8, ref $b eq 'OVER'; |
93 | ok 9, $a + $a == 314; |
94 | ok 10, ref $b->{ref} eq 'REF_TO_OVER'; |
95 | ok 11, "$b->{ref}->{over}" eq "$b"; |
96 | ok 12, $b + $b == 314; |
97 | |
98 | 1; |
99 | |