This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Let taint.t run under miniperl
[perl5.git] / t / op / bless.t
CommitLineData
81689caa
HS
1#!./perl
2
81689caa
HS
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
4a32f131 6 require './test.pl';
81689caa
HS
7}
8
64e070a9
FC
9plan (114);
10# Please do not eliminate the plan. We have tests in DESTROY blocks.
4a32f131 11
81689caa
HS
12sub expected {
13 my($object, $package, $type) = @_;
81689caa 14 print "# $object $package $type\n";
4a32f131
NC
15 is(ref($object), $package);
16 my $r = qr/^\Q$package\E=(\w+)\(0x([0-9a-f]+)\)$/;
17 like("$object", $r);
40ff300d
RGS
18 if ("$object" =~ $r) {
19 is($1, $type);
20 # in 64-bit platforms hex warns for 32+ -bit values
21 cmp_ok(do {no warnings 'portable'; hex($2)}, '==', $object);
22 }
23 else {
24 fail(); fail();
25 }
81689caa
HS
26}
27
28# test blessing simple types
29
30$a1 = bless {}, "A";
4a32f131 31expected($a1, "A", "HASH");
81689caa 32$b1 = bless [], "B";
4a32f131 33expected($b1, "B", "ARRAY");
81689caa 34$c1 = bless \(map "$_", "test"), "C";
4a32f131 35expected($c1, "C", "SCALAR");
016a42f3 36our $test = "foo"; $d1 = bless \*test, "D";
4a32f131 37expected($d1, "D", "GLOB");
81689caa 38$e1 = bless sub { 1 }, "E";
4a32f131 39expected($e1, "E", "CODE");
81689caa 40$f1 = bless \[], "F";
4a32f131 41expected($f1, "F", "REF");
81689caa 42$g1 = bless \substr("test", 1, 2), "G";
4a32f131 43expected($g1, "G", "LVALUE");
81689caa
HS
44
45# blessing ref to object doesn't modify object
46
4a32f131
NC
47expected(bless(\$a1, "F"), "F", "REF");
48expected($a1, "A", "HASH");
81689caa
HS
49
50# reblessing does modify object
51
016a42f3 52bless $a1, "A2";
4a32f131 53expected($a1, "A2", "HASH");
81689caa
HS
54
55# local and my
56{
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
016a42f3 60 our $test2 = ""; my $d1 = bless \*test2, "D3";
4a32f131
NC
61 expected($a1, "A3", "HASH");
62 expected($b1, "B3", "ARRAY");
63 expected($c1, "C3", "SCALAR");
64 expected($d1, "D3", "GLOB");
81689caa 65}
4a32f131
NC
66expected($a1, "A3", "HASH");
67expected($b1, "B", "ARRAY");
68expected($c1, "C3", "SCALAR");
69expected($d1, "D", "GLOB");
81689caa
HS
70
71# class is magic
72"E" =~ /(.)/;
4a32f131 73expected(bless({}, $1), "E", "HASH");
81689caa
HS
74{
75 local $! = 1;
76 my $string = "$!";
77 $! = 2; # attempt to avoid cached string
78 $! = 1;
4a32f131 79 expected(bless({}, $!), $string, "HASH");
81689caa
HS
80
81# ref is ref to magic
82 {
83 {
84 package F;
4a32f131 85 sub test { main::is(${$_[0]}, $string) }
81689caa
HS
86 }
87 $! = 2;
88 $f1 = bless \$!, "F";
89 $! = 1;
90 $f1->test;
81689caa
HS
91 }
92}
93
94# ref is magic
95### example of magic variable that is a reference??
96
97# no class, or empty string (with a warning), or undef (with two)
4a32f131 98expected(bless([]), 'main', "ARRAY");
81689caa
HS
99{
100 local $SIG{__WARN__} = sub { push @w, join '', @_ };
43a954af 101 use warnings;
81689caa
HS
102
103 $m = bless [];
4a32f131
NC
104 expected($m, 'main', "ARRAY");
105 is (scalar @w, 0);
81689caa
HS
106
107 @w = ();
108 $m = bless [], '';
4a32f131
NC
109 expected($m, 'main', "ARRAY");
110 is (scalar @w, 1);
81689caa
HS
111
112 @w = ();
113 $m = bless [], undef;
4a32f131
NC
114 expected($m, 'main', "ARRAY");
115 is (scalar @w, 2);
81689caa
HS
116}
117
118# class is a ref
119$a1 = bless {}, "A4";
120$b1 = eval { bless {}, $a1 };
6f80f2fd 121like ($@, qr/^Attempt to bless into a reference at /, "class is a ref");
016a42f3
HS
122
123# class is an overloaded ref
124{
125 package H4;
126 use overload '""' => sub { "C4" };
127}
128$h1 = bless {}, "H4";
129$c4 = eval { bless \$test, $h1 };
4a32f131
NC
130is ($@, '', "class is an overloaded ref");
131expected($c4, 'C4', "SCALAR");
e0744413
NC
132
133{
134 my %h = 1..2;
135 my($k) = keys %h;
136 my $x=\$k;
137 bless $x, 'pam';
138 is(ref $x, 'pam');
139
140 my $a = bless \(keys %h), 'zap';
141 is(ref $a, 'zap');
142}
1a063a89
FC
143
144bless [], "main::";
145ok(1, 'blessing into main:: does not crash'); # [perl #87388]
dcdfe746
FC
146
147sub _117941 { package _117941; bless [] }
148delete $::{"_117941::"};
149eval { _117941() };
150like $@, qr/^Attempt to bless into a freed package at /,
151 'bless with one arg when current stash is freed';
c13d5d10
FC
152
153for(__PACKAGE__) {
154 eval { bless \$_ };
155 like $@, qr/^Modification of a read-only value attempted/,
156 'read-only COWs cannot be blessed';
157}
5f213d9f
FC
158
159sub TIESCALAR { bless \(my $thing = pop), shift }
160sub FETCH { ${$_[0]} }
161tie $tied, main => $untied = [];
162eval { bless $tied };
163is ref $untied, "main", 'blessing through tied refs' or diag $@;
64e070a9
FC
164
165bless \$victim, "Food";
166eval 'bless \$Food::bard, "Bard"';
167sub Bard::DESTROY {
168 isnt ref(\$victim), '__ANON__',
169 'reblessing does not leave an object in limbo temporarily';
170 bless \$victim
171}
172undef *Food::;
173{
174 my $w;
175 # This should catch ‘Attempt to free unreferenced scalar’.
176 local $SIG{__WARN__} = sub { $w .= shift };
177 bless \$victim;
178 is $w, undef,
179 'no warnings when reblessing inside DESTROY triggered by reblessing'
180}