[perl #31697] [PATCH] B::Showlex::newlex enhancement and pod
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / overload.t
CommitLineData
7a6a85bf 1#!./perl
7a6a85bf 2#
3# Copyright (c) 1995-2000, Raphael Manfredi
4#
9e21b3d0 5# You may redistribute only under the same terms as Perl 5, as specified
6# in the README file that comes with the distribution.
7a6a85bf 7#
7a6a85bf 8
9sub BEGIN {
0c384302 10 if ($ENV{PERL_CORE}){
11 chdir('t') if -d 't';
7dadce44 12 @INC = ('.', '../lib', '../ext/Storable/t');
372cb964 13 } else {
14 unshift @INC, 't';
0c384302 15 }
9f233367 16 require Config; import Config;
0c384302 17 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
9f233367 18 print "1..0 # Skip: Storable was not built\n";
19 exit 0;
20 }
372cb964 21 require 'st-dump.pl';
7a6a85bf 22}
23
24sub ok;
25
26use Storable qw(freeze thaw);
27
165cc789 28print "1..16\n";
7a6a85bf 29
30package OVERLOADED;
31
32use overload
33 '""' => sub { $_[0][0] };
34
35package main;
36
37$a = bless [77], OVERLOADED;
38
39$b = thaw freeze $a;
40ok 1, ref $b eq 'OVERLOADED';
41ok 2, "$b" eq "77";
42
43$c = thaw freeze \$a;
44ok 3, ref $c eq 'REF';
45ok 4, ref $$c eq 'OVERLOADED';
46ok 5, "$$c" eq "77";
47
48$d = thaw freeze [$a, $a];
49ok 6, "$d->[0]" eq "77";
50$d->[0][0]++;
51ok 7, "$d->[1]" eq "78";
52
b12202d0 53package REF_TO_OVER;
54
55sub make {
56 my $self = bless {}, shift;
57 my ($over) = @_;
58 $self->{over} = $over;
59 return $self;
60}
61
62package OVER;
63
64use overload
65 '+' => \&plus,
66 '""' => sub { ref $_[0] };
67
68sub plus {
69 return 314;
70}
71
72sub make {
73 my $self = bless {}, shift;
74 my $ref = REF_TO_OVER->make($self);
75 $self->{ref} = $ref;
76 return $self;
77}
78
79package main;
80
81$a = OVER->make();
82$b = thaw freeze $a;
83
84ok 8, ref $b eq 'OVER';
85ok 9, $a + $a == 314;
86ok 10, ref $b->{ref} eq 'REF_TO_OVER';
87ok 11, "$b->{ref}->{over}" eq "$b";
88ok 12, $b + $b == 314;
89
165cc789 90# nfreeze data generated by make_overload.pl
91my $f = unpack 'u', q{7!084$0Q(05-?3U9%4DQ/040*!'-N;W<`};
92
93# see note at the end of do_retrieve in Storable.xs about why this test has to
94# use a reference to an overloaded reference, rather than just a reference.
95my $t = eval {thaw $f};
96print "# $@" if $@;
97ok 13, $@ eq "";
98ok 14, ref ($t) eq 'REF';
99ok 15, ref ($$t) eq 'HAS_OVERLOAD';
100ok 16, $$$t eq 'snow';
b12202d0 1011;