3 # Regression tests for attributes.pm and the C< : attrs> syntax.
15 $SIG{__WARN__} = sub { die @_ };
22 our $anon1; eval_ok '$anon1 = sub : method { $_[0]++ }';
24 eval 'sub e1 ($) : plugh ;';
25 like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
27 eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
28 like $@, qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
30 eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
31 like $@, qr/Unterminated attribute parameter in attribute list at/;
33 eval 'sub e4 ($) : plugh + xyzzy ;';
34 like $@, qr/Invalid separator character '[+]' in attribute list at/;
36 eval_ok 'my main $x : = 0;';
37 eval_ok 'my $x : = 0;';
39 eval_ok 'my ($x) : = 0;';
41 eval_ok 'my ($x) : ;';
42 eval_ok 'my ($x,$y) : = 0;';
43 eval_ok 'my ($x,$y) ;';
44 eval_ok 'my ($x,$y) : ;';
46 eval 'my ($x,$y) : plugh;';
47 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
50 eval '{my $x : plugh}';
51 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
52 eval '{my ($x,$y) : plugh(})}';
53 like $@, qr/^Invalid SCALAR attribute: ["']?plugh\(}\)["']? at/;
55 # More syntax tests from the attributes manpage
56 eval 'my $x : switch(10,foo(7,3)) : expensive;';
57 like $@, qr/^Invalid SCALAR attributes: ["']?switch\(10,foo\(7,3\)\) : expensive["']? at/;
58 eval q/my $x : Ugly('\(") :Bad;/;
59 like $@, qr/^Invalid SCALAR attributes: ["']?Ugly\('\\\("\) : Bad["']? at/;
61 like $@, qr/^Invalid SCALAR attribute: ["']?_5x5["']? at/;
62 eval 'my $x : locked method;';
63 like $@, qr/^Invalid SCALAR attributes: ["']?locked : method["']? at/;
64 eval 'my $x : switch(10,foo();';
65 like $@, qr/^Unterminated attribute parameter in attribute list at/;
66 eval q/my $x : Ugly('(');/;
67 like $@, qr/^Unterminated attribute parameter in attribute list at/;
70 eval 'my $x : Y2::north;';
71 like $@, qr/Invalid separator character ':' in attribute list at/;
73 sub A::MODIFY_SCALAR_ATTRIBUTES { return }
74 eval 'my A $x : plugh;';
75 like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
77 eval 'my A $x : plugh plover;';
78 like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
80 no warnings 'reserved';
81 eval 'my A $x : plugh;';
84 eval 'package Cat; my Cat @socks;';
85 like $@, qr/^Can't declare class for non-scalar \@socks in "my"/;
87 sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
90 *Y::bar = \&X::foo; # second time for -w
91 eval 'package Z; sub Y::bar : foo';
94 @attrs = eval 'attributes::get $anon1';
95 is "@attrs", "method";
98 sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
99 my $thunk = eval 'bless +sub : method { 1 }, "Z"';
102 @attrs = eval 'attributes::get $thunk';
103 is "@attrs", "method Z";
105 # Test attributes on predeclared subroutines:
106 eval 'package A; sub PS : lvalue';
107 @attrs = eval 'attributes::get \&A::PS';
108 is "@attrs", "lvalue";
110 # Test ability to modify existing sub's (or XSUB's) attributes.
111 eval 'package A; sub X { $_[0] } sub X : lvalue';
112 @attrs = eval 'attributes::get \&A::X';
113 is "@attrs", "lvalue";
115 # Above not with just 'pure' built-in attributes.
116 sub Z::MODIFY_CODE_ATTRIBUTES { (); }
117 eval 'package Z; sub L { $_[0] } sub L : Z lvalue';
118 @attrs = eval 'attributes::get \&Z::L';
119 is "@attrs", "lvalue Z";
121 # Begin testing attributes that tie
126 sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
127 sub FETCH { ${$_[0]} }
133 sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
139 my $x : TieLoop = $i;
140 $x != $i*2 and ::is $x, $i*2;
145 eval 'our ${""} : foo = 1';
146 like $@, qr/Can't declare scalar dereference in "our"/;
147 eval 'my $$foo : bar = 1';
148 like $@, qr/Can't declare scalar dereference in "my"/;
151 my @code = qw(lvalue locked method);
152 my @other = qw(shared unique);
154 $valid{CODE} = {map {$_ => 1} @code};
155 $valid{SCALAR} = {map {$_ => 1} @other};
156 $valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
158 our ($scalar, @array, %hash);
159 foreach my $value (\&foo, \$scalar, \@array, \%hash) {
160 my $type = ref $value;
161 foreach my $negate ('', '-') {
162 foreach my $attr (@code, @other) {
163 my $attribute = $negate . $attr;
164 eval "use attributes __PACKAGE__, \$value, '$attribute'";
165 if ($valid{$type}{$attr}) {
166 if ($attribute eq '-shared') {
167 like $@, qr/^A variable may not be unshared/;
169 is( $@, '', "$type attribute $attribute");
172 like $@, qr/^Invalid $type attribute: $attribute/,
173 "Bogus $type attribute $attribute should fail";
179 # this will segfault if it fails
180 sub PVBM () { 'foo' }
181 { my $dummy = index 'foo', PVBM }
183 ok !defined(attributes::get(\PVBM)),
184 'PVBMs don\'t segfault attributes::get';