10 # Please do not eliminate the plan. We have tests in DESTROY blocks.
13 my($object, $package, $type) = @_;
14 print "# $object $package $type\n";
15 is(ref($object), $package);
16 my $r = qr/^\Q$package\E=(\w+)\(0x([0-9a-f]+)\)$/;
18 if ("$object" =~ $r) {
20 # in 64-bit platforms hex warns for 32+ -bit values
21 cmp_ok(do {no warnings 'portable'; hex($2)}, '==', $object);
28 # test blessing simple types
31 expected($a1, "A", "HASH");
33 expected($b1, "B", "ARRAY");
34 $c1 = bless \(map "$_", "test"), "C";
35 expected($c1, "C", "SCALAR");
36 our $test = "foo"; $d1 = bless \*test, "D";
37 expected($d1, "D", "GLOB");
38 $e1 = bless sub { 1 }, "E";
39 expected($e1, "E", "CODE");
41 expected($f1, "F", "REF");
42 $g1 = bless \substr("test", 1, 2), "G";
43 expected($g1, "G", "LVALUE");
45 # blessing ref to object doesn't modify object
47 expected(bless(\$a1, "F"), "F", "REF");
48 expected($a1, "A", "HASH");
50 # reblessing does modify object
53 expected($a1, "A2", "HASH");
57 local $a1 = bless $a1, "A3"; # should rebless outer $a1
58 local $b1 = bless [], "B3";
59 my $c1 = bless $c1, "C3"; # should rebless outer $c1
60 our $test2 = ""; my $d1 = bless \*test2, "D3";
61 expected($a1, "A3", "HASH");
62 expected($b1, "B3", "ARRAY");
63 expected($c1, "C3", "SCALAR");
64 expected($d1, "D3", "GLOB");
66 expected($a1, "A3", "HASH");
67 expected($b1, "B", "ARRAY");
68 expected($c1, "C3", "SCALAR");
69 expected($d1, "D", "GLOB");
73 expected(bless({}, $1), "E", "HASH");
77 $! = 2; # attempt to avoid cached string
79 expected(bless({}, $!), $string, "HASH");
85 sub test { main::is(${$_[0]}, $string) }
95 ### example of magic variable that is a reference??
97 # no class, or empty string (with a warning), or undef (with two)
98 expected(bless([]), 'main', "ARRAY");
100 local $SIG{__WARN__} = sub { push @w, join '', @_ };
104 expected($m, 'main', "ARRAY");
109 expected($m, 'main', "ARRAY");
113 $m = bless [], undef;
114 expected($m, 'main', "ARRAY");
119 $a1 = bless {}, "A4";
120 $b1 = eval { bless {}, $a1 };
121 like ($@, qr/^Attempt to bless into a reference at /, "class is a ref");
123 # class is an overloaded ref
126 use overload '""' => sub { "C4" };
128 $h1 = bless {}, "H4";
129 $c4 = eval { bless \$test, $h1 };
130 is ($@, '', "class is an overloaded ref");
131 expected($c4, 'C4', "SCALAR");
140 my $a = bless \(keys %h), 'zap';
145 ok(1, 'blessing into main:: does not crash'); # [perl #87388]
147 sub _117941 { package _117941; bless [] }
148 delete $::{"_117941::"};
150 like $@, qr/^Attempt to bless into a freed package at /,
151 'bless with one arg when current stash is freed';
155 like $@, qr/^Modification of a read-only value attempted/,
156 'read-only COWs cannot be blessed';
159 sub TIESCALAR { bless \(my $thing = pop), shift }
160 sub FETCH { ${$_[0]} }
161 tie $tied, main => $untied = [];
162 eval { bless $tied };
163 is ref $untied, "main", 'blessing through tied refs' or diag $@;
165 bless \$victim, "Food";
166 eval 'bless \$Food::bard, "Bard"';
168 isnt ref(\$victim), '__ANON__',
169 'reblessing does not leave an object in limbo temporarily';
175 # This should catch ‘Attempt to free unreferenced scalar’.
176 local $SIG{__WARN__} = sub { $w .= shift };
179 'no warnings when reblessing inside DESTROY triggered by reblessing'
185 my ($class, $code) = @_;
186 my $ret = ref($code);
187 bless $code => $class;
191 $ref = main -> new (sub {$i});
193 is $ref, 'CODE', 'RT #3305: Code ref should not be blessed yet';
195 local $TODO = 'RT #3305';
198 $ref = main -> new (sub {});
200 is $ref, 'CODE', 'RT #3305: Code ref should not be blessed yet';
207 sub FooClosure::new {
208 my ($class, $code) = @_;
209 bless $code => $class;
211 sub FooClosure::DESTROY {
216 my ($class, $code) = @_;
217 bless $code => $class;
219 sub FooSub::DESTROY {
224 FooClosure -> new (sub {$i});
225 FooSub -> new (sub {});
228 is $t_3306_c, 1, 'RT #3306: DESTROY should be called on CODE ref (works on closures)';
231 local $TODO = 'RT #3306';
232 is $t_3306_s, 1, 'RT #3306: DESTROY should be called on CODE ref';