3 # Regression tests for attributes.pm and the C< : attrs> syntax.
6 if ($ENV{PERL_CORE_MINITEST}) {
7 print "1..0 # skip: miniperl can't load attributes\n";
19 $SIG{__WARN__} = sub { die @_ };
26 our $anon1; eval_ok '$anon1 = sub : method { $_[0]++ }';
28 eval 'sub e1 ($) : plugh ;';
29 like $@, qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
31 eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
32 like $@, qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
34 eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
35 like $@, qr/Unterminated attribute parameter in attribute list at/;
37 eval 'sub e4 ($) : plugh + xyzzy ;';
38 like $@, qr/Invalid separator character '[+]' in attribute list at/;
40 eval_ok 'my main $x : = 0;';
41 eval_ok 'my $x : = 0;';
43 eval_ok 'my ($x) : = 0;';
45 eval_ok 'my ($x) : ;';
46 eval_ok 'my ($x,$y) : = 0;';
47 eval_ok 'my ($x,$y) ;';
48 eval_ok 'my ($x,$y) : ;';
50 eval 'my ($x,$y) : plugh;';
51 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
54 eval '{my $x : plugh}';
55 like $@, qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
56 eval '{my ($x,$y) : plugh(})}';
57 like $@, qr/^Invalid SCALAR attribute: ["']?plugh\(}\)["']? at/;
59 # More syntax tests from the attributes manpage
60 eval 'my $x : switch(10,foo(7,3)) : expensive;';
61 like $@, qr/^Invalid SCALAR attributes: ["']?switch\(10,foo\(7,3\)\) : expensive["']? at/;
62 eval q/my $x : Ugly('\(") :Bad;/;
63 like $@, qr/^Invalid SCALAR attributes: ["']?Ugly\('\\\("\) : Bad["']? at/;
65 like $@, qr/^Invalid SCALAR attribute: ["']?_5x5["']? at/;
66 eval 'my $x : locked method;';
67 like $@, qr/^Invalid SCALAR attributes: ["']?locked : method["']? at/;
68 eval 'my $x : switch(10,foo();';
69 like $@, qr/^Unterminated attribute parameter in attribute list at/;
70 eval q/my $x : Ugly('(');/;
71 like $@, qr/^Unterminated attribute parameter in attribute list at/;
74 eval 'my $x : Y2::north;';
75 like $@, qr/Invalid separator character ':' in attribute list at/;
77 sub A::MODIFY_SCALAR_ATTRIBUTES { return }
78 eval 'my A $x : plugh;';
79 like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
81 eval 'my A $x : plugh plover;';
82 like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
84 no warnings 'reserved';
85 eval 'my A $x : plugh;';
88 eval 'package Cat; my Cat @socks;';
94 sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
97 *Y::bar = \&X::foo; # second time for -w
98 eval 'package Z; sub Y::bar : foo';
101 @attrs = eval 'attributes::get $anon1';
102 is "@attrs", "method";
105 sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
106 my $thunk = eval 'bless +sub : method { 1 }, "Z"';
109 @attrs = eval 'attributes::get $thunk';
110 is "@attrs", "method Z";
112 # Test attributes on predeclared subroutines:
113 eval 'package A; sub PS : lvalue';
114 @attrs = eval 'attributes::get \&A::PS';
115 is "@attrs", "lvalue";
117 # Test attributes on predeclared subroutines, after definition
118 eval 'package A; sub PS : lvalue; sub PS { }';
119 @attrs = eval 'attributes::get \&A::PS';
120 is "@attrs", "lvalue";
122 # Test ability to modify existing sub's (or XSUB's) attributes.
123 eval 'package A; sub X { $_[0] } sub X : method';
124 @attrs = eval 'attributes::get \&A::X';
125 is "@attrs", "method";
127 # Above not with just 'pure' built-in attributes.
128 sub Z::MODIFY_CODE_ATTRIBUTES { (); }
129 eval 'package Z; sub L { $_[0] } sub L : Z method';
130 @attrs = eval 'attributes::get \&Z::L';
131 is "@attrs", "method Z";
133 # Begin testing attributes that tie
138 sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
139 sub FETCH { ${$_[0]} }
145 sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
151 my $x : TieLoop = $i;
152 $x != $i*2 and ::is $x, $i*2;
157 eval 'our ${""} : foo = 1';
158 like $@, qr/Can't declare scalar dereference in "our"/;
159 eval 'my $$foo : bar = 1';
160 like $@, qr/Can't declare scalar dereference in "my"/;
163 my @code = qw(lvalue method);
164 my @other = qw(shared);
165 my @deprecated = qw(locked unique);
167 $valid{CODE} = {map {$_ => 1} @code};
168 $valid{SCALAR} = {map {$_ => 1} @other};
169 $valid{ARRAY} = $valid{HASH} = $valid{SCALAR};
171 $deprecated{CODE} = { locked => 1 };
172 $deprecated{ARRAY} = $deprecated{HASH} = $deprecated{SCALAR} = { unique => 1 };
174 our ($scalar, @array, %hash);
175 foreach my $value (\&foo, \$scalar, \@array, \%hash) {
176 my $type = ref $value;
177 foreach my $negate ('', '-') {
178 foreach my $attr (@code, @other, @deprecated) {
179 my $attribute = $negate . $attr;
180 eval "use attributes __PACKAGE__, \$value, '$attribute'";
181 if ($deprecated{$type}{$attr}) {
182 like $@, qr/^Attribute "$attr" is deprecated at \(eval \d+\)/,
183 "$type attribute $attribute deprecated";
184 } elsif ($valid{$type}{$attr}) {
185 if ($attribute eq '-shared') {
186 like $@, qr/^A variable may not be unshared/;
188 is( $@, '', "$type attribute $attribute");
191 like $@, qr/^Invalid $type attribute: $attribute/,
192 "Bogus $type attribute $attribute should fail";
198 # this will segfault if it fails
199 sub PVBM () { 'foo' }
200 { my $dummy = index 'foo', PVBM }
202 ok !defined(attributes::get(\PVBM)),
203 'PVBMs don\'t segfault attributes::get';
206 # [perl #49472] Attributes + Unkown Error
209 sub MODIFY_CODE_ATTRIBUTE{}
210 sub f:Blah {$nosuchvar};
214 like ($err, qr/Global symbol "\$nosuchvar" requires /, 'perl #49472');
217 # Test that code attributes always get applied to the same CV that
218 # we're left with at the end (bug#66970).
222 sub MODIFY_CODE_ATTRIBUTES { $c = $_[1]; () }
223 $c=undef; eval 'sub t0 :Foo';
224 main::ok $c == \&{"t0"};
225 $c=undef; eval 'sub t1 :Foo { }';
226 main::ok $c == \&{"t1"};
227 $c=undef; eval 'sub t2';
229 $c=undef; eval 'sub t2 :Foo';
230 main::ok $c == \&{"t2"} && $c == $t2a;
231 $c=undef; eval 'sub t3';
233 $c=undef; eval 'sub t3 :Foo { }';
234 main::ok $c == \&{"t3"} && $c == $t3a;
235 $c=undef; eval 'sub t4 :Foo';
238 $c=undef; eval 'sub t4 :Foo';
239 main::ok $c == \&{"t4"} && $c == $t4b && $c == $t4a;
240 $c=undef; eval 'sub t5 :Foo';
243 $c=undef; eval 'sub t5 :Foo { }';
244 main::ok $c == \&{"t5"} && $c == $t5b && $c == $t5a;