1 #!./perl 2 3 print "1..31\n"; 4 5 BEGIN { 6 chdir 't' if -d 't'; 7 @INC = '../lib'; 8 } 9 10 sub expected { 11 my($object, $package, $type) = @_; 12 return "" if ( 13 ref($object) eq $package 14 && "$object" =~ /^\Q$package\E=(\w+)\(0x([0-9a-f]+)\)$/ 15 && $1 eq $type 16 # in 64-bit platforms hex warns for 32+ -bit values 17 && do { no warnings 'portable'; hex($2) == $object } 18 ); 19 print "# $object $package $type\n"; 20 return "not "; 21 } 22 23 # test blessing simple types 24 25 $a1 = bless {}, "A"; 26 print expected($a1, "A", "HASH"), "ok 1\n"; 27 $b1 = bless [], "B"; 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"; 35 $f1 = bless \[], "F"; 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"; 39 40 # blessing ref to object doesn't modify object 41 42 print expected(bless(\$a1, "F"), "F", "REF"), "ok 8\n"; 43 print expected($a1, "A", "HASH"), "ok 9\n"; 44 45 # reblessing does modify object 46 47 bless $a1, "A2"; 48 print expected($a1, "A2", "HASH"), "ok 10\n"; 49 50 # local and my 51 { 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"; 60 } 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"; 65 66 # class is magic 67 "E" =~ /(.)/; 68 print expected(bless({}, $1), "E", "HASH"), "ok 19\n"; 69 { 70 local $! = 1; 71 my $string = "$!"; 72 $! = 2; # attempt to avoid cached string 73 $! = 1; 74 print expected(bless({}, $!), $string, "HASH"), "ok 20\n"; 75 76 # ref is ref to magic 77 { 78 { 79 package F; 80 sub test { ${$_[0]} eq $string or print "not " } 81 } 82 $! = 2; 83 $f1 = bless \$!, "F"; 84 $! = 1; 85 $f1->test; 86 print "ok 21\n"; 87 } 88 } 89 90 # ref is magic 91 ### example of magic variable that is a reference?? 92 93 # no class, or empty string (with a warning), or undef (with two) 94 print expected(bless([]), 'main', "ARRAY"), "ok 22\n"; 95 { 96 local $SIG{__WARN__} = sub { push @w, join '', @_ }; 97 use warnings; 98 99 $m = bless []; 100 print expected($m, 'main', "ARRAY"), "ok 23\n"; 101 print @w ? "not ok 24\t# @w\n" : "ok 24\n"; 102 103 @w = (); 104 $m = bless [], ''; 105 print expected($m, 'main', "ARRAY"), "ok 25\n"; 106 print @w != 1 ? "not ok 26\t# @w\n" : "ok 26\n"; 107 108 @w = (); 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"; 112 } 113 114 # class is a ref 115 $a1 = bless {}, "A4"; 116 $b1 = eval { bless {}, $a1 }; 117 print $@ ? "ok 29\n" : "not ok 29\t# $b1\n"; 118 119 # class is an overloaded ref 120 { 121 package H4; 122 use overload '""' => sub { "C4" }; 123 } 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"; 128