11 my($object, $package, $type) = @_;
13 ref($object) eq $package
14 && "$object" =~ /^\Q$package\E=(\w+)\(0x([0-9a-f]+)\)$/
16 # in 64-bit platforms hex warns for 32+ -bit values
17 && do { no warnings 'portable'; hex($2) == $object }
19 print "# $object $package $type\n";
23 # test blessing simple types
26 print expected($a1, "A", "HASH"), "ok 1\n";
28 print expected($b1, "B", "ARRAY"), "ok 2\n";
29 $c1 = bless \(map "$_", "test"), "C";
30 print expected($c1, "C", "SCALAR"), "ok 3\n";
31 our $test = "foo"; $d1 = bless \*test, "D";
32 print expected($d1, "D", "GLOB"), "ok 4\n";
33 $e1 = bless sub { 1 }, "E";
34 print expected($e1, "E", "CODE"), "ok 5\n";
36 print expected($f1, "F", "REF"), "ok 6\n";
37 $g1 = bless \substr("test", 1, 2), "G";
38 print expected($g1, "G", "LVALUE"), "ok 7\n";
40 # blessing ref to object doesn't modify object
42 print expected(bless(\$a1, "F"), "F", "REF"), "ok 8\n";
43 print expected($a1, "A", "HASH"), "ok 9\n";
45 # reblessing does modify object
48 print expected($a1, "A2", "HASH"), "ok 10\n";
52 local $a1 = bless $a1, "A3"; # should rebless outer $a1
53 local $b1 = bless [], "B3";
54 my $c1 = bless $c1, "C3"; # should rebless outer $c1
55 our $test2 = ""; my $d1 = bless \*test2, "D3";
56 print expected($a1, "A3", "HASH"), "ok 11\n";
57 print expected($b1, "B3", "ARRAY"), "ok 12\n";
58 print expected($c1, "C3", "SCALAR"), "ok 13\n";
59 print expected($d1, "D3", "GLOB"), "ok 14\n";
61 print expected($a1, "A3", "HASH"), "ok 15\n";
62 print expected($b1, "B", "ARRAY"), "ok 16\n";
63 print expected($c1, "C3", "SCALAR"), "ok 17\n";
64 print expected($d1, "D", "GLOB"), "ok 18\n";
68 print expected(bless({}, $1), "E", "HASH"), "ok 19\n";
72 $! = 2; # attempt to avoid cached string
74 print expected(bless({}, $!), $string, "HASH"), "ok 20\n";
80 sub test { ${$_[0]} eq $string or print "not " }
91 ### example of magic variable that is a reference??
93 # no class, or empty string (with a warning), or undef (with two)
94 print expected(bless([]), 'main', "ARRAY"), "ok 22\n";
96 local $SIG{__WARN__} = sub { push @w, join '', @_ };
100 print expected($m, 'main', "ARRAY"), "ok 23\n";
101 print @w ? "not ok 24\t# @w\n" : "ok 24\n";
105 print expected($m, 'main', "ARRAY"), "ok 25\n";
106 print @w != 1 ? "not ok 26\t# @w\n" : "ok 26\n";
109 $m = bless [], undef;
110 print expected($m, 'main', "ARRAY"), "ok 27\n";
111 print @w != 2 ? "not ok 28\t# @w\n" : "ok 28\n";
115 $a1 = bless {}, "A4";
116 $b1 = eval { bless {}, $a1 };
117 print $@ ? "ok 29\n" : "not ok 29\t# $b1\n";
119 # class is an overloaded ref
122 use overload '""' => sub { "C4" };
124 $h1 = bless {}, "H4";
125 $c4 = eval { bless \$test, $h1 };
126 print expected($c4, 'C4', "SCALAR"), "ok 30\n";
127 print $@ ? "not ok 31\t# $@" : "ok 31\n";