1#!/usr/bin/perl -w 2 3my ($Has_PH, $Field); 4BEGIN { 5 $Has_PH = $] < 5.009; 6 $Field = $Has_PH ? "pseudo-hash field" : "class field"; 7} 8 9my $W; 10 11BEGIN { 12 $W = 0; 13 $SIG{__WARN__} = sub { 14 if ($_[0] =~ /^Hides field '.*?' in base class/) { 15 $W++; 16 } 17 else { 18 warn $_[0]; 19 } 20 }; 21} 22 23use strict; 24use Test::More tests => 29; 25 26BEGIN { use_ok('base'); } 27 28package B1; 29use fields qw(b1 b2 b3); 30 31package B2; 32use fields '_b1'; 33use fields qw(b1 _b2 b2); 34 35sub new { fields::new(shift) } 36 37package B3; 38use fields qw(b4 _b5 b6 _b7); 39 40package D1; 41use base 'B1'; 42use fields qw(d1 d2 d3); 43 44package D2; 45use base 'B1'; 46use fields qw(_d1 _d2); 47use fields qw(d1 d2); 48 49 50package D3; 51use base 'B2'; 52use fields qw(b1 d1 _b1 _d1); # hide b1 53 54package D4; 55use base 'D3'; 56use fields qw(_d3 d3); 57 58package M; 59sub m {} 60 61package D5; 62use base qw(M B2); 63 64# Test that multiple inheritance fails. 65package D6; 66eval { 'base'->import(qw(B2 M B3)); }; 67::like($@, qr/can't multiply inherit fields/i, 68 'No multiple field inheritance'); 69 70package Foo::Bar; 71use base 'B1'; 72 73package Foo::Bar::Baz; 74use base 'Foo::Bar'; 75use fields qw(foo bar baz); 76 77# Test repeatability for when modules get reloaded. 78package B1; 79use fields qw(b1 b2 b3); 80 81package D3; 82use base 'B2'; 83use fields qw(b1 d1 _b1 _d1); # hide b1 84 85 86# Test that a package with only private fields gets inherited properly 87package B7; 88use fields qw(_b1); 89 90package D7; 91use base qw(B7); 92use fields qw(b1); 93 94 95# Test that an intermediate package with no fields doesn't cause a problem. 96package B8; 97use fields qw(_b1); 98 99package D8; 100use base qw(B8); 101 102package D8A; 103use base qw(D8); 104use fields qw(b1); 105 106 107package main; 108 109my %EXPECT = ( 110 B1 => [qw(b1 b2 b3)], 111 D1 => [qw(b1 b2 b3 d1 d2 d3)], 112 D2 => [qw(b1 b2 b3 _d1 _d2 d1 d2)], 113 114 M => [qw()], 115 B2 => [qw(_b1 b1 _b2 b2)], 116 D3 => [(undef,undef,undef, 117 qw(b2 b1 d1 _b1 _d1))], # b1 is hidden 118 D4 => [(undef,undef,undef, 119 qw(b2 b1 d1),undef,undef,qw(_d3 d3))], 120 121 D5 => [undef, 'b1', undef, 'b2'], 122 123 B3 => [qw(b4 _b5 b6 _b7)], 124 125 B7 => [qw(_b1)], 126 D7 => [undef, 'b1'], 127 128 B8 => [qw(_b1)], 129 D8 => [undef], 130 D8A => [undef, 'b1'], 131 132 'Foo::Bar' => [qw(b1 b2 b3)], 133 'Foo::Bar::Baz' => [qw(b1 b2 b3 foo bar baz)], 134 ); 135 136while(my($class, $efields) = each %EXPECT) { 137 no strict 'refs'; 138 my %fields = %{$class.'::FIELDS'}; 139 my %expected_fields; 140 foreach my $idx (1..@$efields) { 141 my $key = $efields->[$idx-1]; 142 next unless $key; 143 $expected_fields{$key} = $idx; 144 } 145 146 ::is_deeply(\%fields, \%expected_fields, "%FIELDS check: $class"); 147} 148 149# Did we get the appropriate amount of warnings? 150is( $W, 1, 'right warnings' ); 151 152 153# A simple object creation and attribute access test 154my B2 $obj1 = D3->new; 155$obj1->{b1} = "B2"; 156my D3 $obj2 = $obj1; 157$obj2->{b1} = "D3"; 158 159# We should get compile time failures field name typos 160eval q(return; my D3 $obj3 = $obj2; $obj3->{notthere} = ""); 161like $@, 162 qr/^No such $Field "notthere" in variable \$obj3 of type D3/, 163 "Compile failure of undeclared fields (helem)"; 164 165SKIP: { 166 # Slices 167 # We should get compile time failures field name typos 168 skip "Doesn't work before 5.9", 2 if $] < 5.009; 169 eval q(return; my D3 $obj3 = $obj2; my $k; @$obj3{$k,'notthere'} = ()); 170 like $@, 171 qr/^No such $Field "notthere" in variable \$obj3 of type D3/, 172 "Compile failure of undeclared fields (hslice)"; 173 eval q(return; my D3 $obj3 = $obj2; my $k; @{$obj3}{$k,'notthere'} = ()); 174 like 175 $@, qr/^No such $Field "notthere" in variable \$obj3 of type D3/, 176 "Compile failure of undeclared fields (hslice (block form))"; 177} 178 179@$obj1{"_b1", "b1"} = (17, 29); 180is( $obj1->{_b1}, 17 ); 181is( $obj1->{b1}, 29 ); 182 183@$obj1{'_b1', 'b1'} = (44,28); 184is( $obj1->{_b1}, 44 ); 185is( $obj1->{b1}, 28 ); 186 187 188 189# Break multiple inheritance with a field name clash. 190package E1; 191use fields qw(yo this _lah meep 42); 192 193package E2; 194use fields qw(_yo ahhh this); 195 196eval { 197 package Broken; 198 199 # The error must occur at run time for the eval to catch it. 200 require base; 201 'base'->import(qw(E1 E2)); 202}; 203::like( $@, qr/Can't multiply inherit fields/i, 'Again, no multi inherit' ); 204 205 206# Test that a package with no fields can inherit from a package with 207# fields, and that pseudohash messages don't show up 208 209package B9; 210use fields qw(b1); 211 212sub _mk_obj { fields::new($_[0])->{'b1'} }; 213 214package D9; 215use base qw(B9); 216 217package main; 218 219{ 220 my $w = 0; 221 local $SIG{__WARN__} = sub { $w++ }; 222 223 B9->_mk_obj(); 224 # used tp emit a warning that pseudohashes are deprecated, because 225 # %FIELDS wasn't blessed. 226 D9->_mk_obj(); 227 228 is ($w, 0, "pseudohash warnings in derived class with no fields of it's own"); 229} 230 231# [perl #31078] an intermediate class with no additional fields caused 232# hidden fields in base class to get stomped on 233 234{ 235 package X; 236 use fields qw(X1 _X2); 237 sub new { 238 my X $self = shift; 239 $self = fields::new($self) unless ref $self; 240 $self->{X1} = "x1"; 241 # FIXME. This code is dead on blead because the test is skipped. 242 # The test states that it's being skipped because restricted hashes 243 # don't support a feature. Presumably we need to make that feature 244 # supported. Bah. 245 # use Devel::Peek; Dump($self); 246 $self->{_X2} = "_x2"; 247 return $self; 248 } 249 sub get_X2 { my X $self = shift; $self->{_X2} } 250 251 package Y; 252 use base qw(X); 253 254 sub new { 255 my Y $self = shift; 256 $self = fields::new($self) unless ref $self; 257 $self->SUPER::new(); 258 return $self; 259 } 260 261 262 package Z; 263 use base qw(Y); 264 use fields qw(Z1); 265 266 sub new { 267 my Z $self = shift; 268 $self = fields::new($self) unless ref $self; 269 $self->SUPER::new(); 270 $self->{Z1} = 'z1'; 271 return $self; 272 } 273 274 package main; 275 276 if ($Has_PH) { 277 my Z $c = Z->new(); 278 is($c->get_X2, '_x2', "empty intermediate class"); 279 } 280 else { 281 SKIP: { 282 skip "restricted hashes don't support private fields properly", 1; 283 } 284 } 285} 286