xref: /openbsd-src/gnu/usr.bin/perl/t/uni/bless.t (revision 5759b3d249badf144a6240f7eec4dcf9df003e6b)
1898184e3Ssthen#!./perl
2898184e3Ssthen
3898184e3SsthenBEGIN {
4898184e3Ssthen    chdir 't' if -d 't';
5898184e3Ssthen    require './test.pl';
6*5759b3d2Safresh1    set_up_inc('../lib');
7898184e3Ssthen}
8898184e3Ssthen
9898184e3Ssthenuse utf8;
10898184e3Ssthenuse open qw( :utf8 :std );
11898184e3Ssthenplan (84);
12898184e3Ssthen
13898184e3Ssthensub expected {
14898184e3Ssthen    my($object, $package, $type) = @_;
15898184e3Ssthen    print "# $object $package $type\n";
16898184e3Ssthen    is(ref($object), $package);
17898184e3Ssthen    my $r = qr/^\Q$package\E=(\w+)\(0x([0-9a-f]+)\)$/u;
18898184e3Ssthen    like("$object", $r);
19898184e3Ssthen    if ("$object" =~ $r) {
20898184e3Ssthen	is($1, $type);
21898184e3Ssthen	# in 64-bit platforms hex warns for 32+ -bit values
22898184e3Ssthen	cmp_ok(do {no warnings 'portable'; hex($2)}, '==', $object);
23898184e3Ssthen    }
24898184e3Ssthen    else {
25898184e3Ssthen	fail(); fail();
26898184e3Ssthen    }
27898184e3Ssthen}
28898184e3Ssthen
29898184e3Ssthen# test blessing simple types
30898184e3Ssthen
31898184e3Ssthen$a1 = bless {}, "ዐ";
32898184e3Ssthenexpected($a1, "ዐ", "HASH");
33898184e3Ssthen$b1 = bless [], "B";
34898184e3Ssthenexpected($b1, "B", "ARRAY");
35898184e3Ssthen$c1 = bless \(map "$_", "test"), "ᶜ";
36898184e3Ssthenexpected($c1, "ᶜ", "SCALAR");
37898184e3Ssthen$tèst = "foo"; $d1 = bless \*tèst, "ɖ";
38898184e3Ssthenexpected($d1, "ɖ", "GLOB");
39898184e3Ssthen$e1 = bless sub { 1 }, "ಎ";
40898184e3Ssthenexpected($e1, "ಎ", "CODE");
41898184e3Ssthen$f1 = bless \[], "ḟ";
42898184e3Ssthenexpected($f1, "ḟ", "REF");
43898184e3Ssthen$g1 = bless \substr("test", 1, 2), "ㄍ";
44898184e3Ssthenexpected($g1, "ㄍ", "LVALUE");
45898184e3Ssthen
46898184e3Ssthen# blessing ref to object doesn't modify object
47898184e3Ssthen
48898184e3Ssthenexpected(bless(\$a1, "ḟ"), "ḟ", "REF");
49898184e3Ssthenexpected($a1, "ዐ", "HASH");
50898184e3Ssthen
51898184e3Ssthen# reblessing does modify object
52898184e3Ssthen
53898184e3Ssthenbless $a1, "ዐ2";
54898184e3Ssthenexpected($a1, "ዐ2", "HASH");
55898184e3Ssthen
56898184e3Ssthen# local and my
57898184e3Ssthen{
58898184e3Ssthen    local $a1 = bless $a1, "ዐ3";	# should rebless outer $a1
59898184e3Ssthen    local $b1 = bless [], "B3";
60898184e3Ssthen    my $c1 = bless $c1, "ᶜ3";		# should rebless outer $c1
61898184e3Ssthen    our $test2 = ""; my $d1 = bless \*test2, "ɖ3";
62898184e3Ssthen    expected($a1, "ዐ3", "HASH");
63898184e3Ssthen    expected($b1, "B3", "ARRAY");
64898184e3Ssthen    expected($c1, "ᶜ3", "SCALAR");
65898184e3Ssthen    expected($d1, "ɖ3", "GLOB");
66898184e3Ssthen}
67898184e3Ssthenexpected($a1, "ዐ3", "HASH");
68898184e3Ssthenexpected($b1, "B", "ARRAY");
69898184e3Ssthenexpected($c1, "ᶜ3", "SCALAR");
70898184e3Ssthenexpected($d1, "ɖ", "GLOB");
71898184e3Ssthen
72898184e3Ssthen# class is magic
73898184e3Ssthen"ಎ" =~ /(.)/;
74898184e3Ssthenexpected(bless({}, $1), "ಎ", "HASH");
75898184e3Ssthen{
76898184e3Ssthen    local $! = 1;
77898184e3Ssthen    my $string = "$!";
78898184e3Ssthen    $! = 2;	# attempt to avoid cached string
79898184e3Ssthen    $! = 1;
80898184e3Ssthen    expected(bless({}, $!), $string, "HASH");
81898184e3Ssthen
82898184e3Ssthen# ref is ref to magic
83898184e3Ssthen    {
84898184e3Ssthen	{
85898184e3Ssthen	    package ḟ;
86898184e3Ssthen	    sub test { main::is(${$_[0]}, $string) }
87898184e3Ssthen	}
88898184e3Ssthen	$! = 2;
89898184e3Ssthen	$f1 = bless \$!, "ḟ";
90898184e3Ssthen	$! = 1;
91898184e3Ssthen	$f1->test;
92898184e3Ssthen    }
93898184e3Ssthen}
94898184e3Ssthen
95898184e3Ssthen# ref is magic
96898184e3Ssthen
97898184e3Ssthen# class is a ref
98898184e3Ssthen$a1 = bless {}, "ዐ4";
99898184e3Ssthen$b1 = eval { bless {}, $a1 };
100898184e3Ssthenisnt ($@, '', "class is a ref");
101898184e3Ssthen
102898184e3Ssthen# class is an overloaded ref
103898184e3Ssthen=begin
104898184e3Ssthen$TODO = "Package not yet clean";
105898184e3Ssthen{
106898184e3Ssthen    package4;
107898184e3Ssthen    use overload '""' => sub { "ᶜ4" };
108898184e3Ssthen}
109898184e3Ssthen$h1 = bless {}, "ᚺ4";
110898184e3Ssthen$c4 = eval { bless \$test, $h1 };
111898184e3Ssthenis ($@, '', "class is an overloaded ref");
112898184e3Ssthenexpected($c4, 'ᶜ4', "SCALAR");
113898184e3Ssthen=cut
114898184e3Ssthen
115898184e3Ssthen{
116898184e3Ssthen    my %h = 1..2;
117898184e3Ssthen    my($k) = keys %h;
118898184e3Ssthen    my $x=\$k;
119898184e3Ssthen    bless $x, 'pàm';
120898184e3Ssthen    is(ref $x, 'pàm');
121898184e3Ssthen
122898184e3Ssthen    my $a = bless \(keys %h), 'zàp';
123898184e3Ssthen    is(ref $a, 'zàp');
124898184e3Ssthen}
125