3 # Regression tests for attributes.pm and the C< : attrs> syntax.
9 skip_all_if_miniperl("miniperl can't load attributes");
13 use open qw( :utf8 :std );
15 use feature 'unicode_strings';
17 $SIG{__WARN__} = sub { die @_ };
24 fresh_perl_is 'use attributes; print "ok"', 'ok', {},
25 'attributes.pm can load without warnings.pm already loaded';
27 eval 'sub è1 ($) : plùgh ;';
28 like $@, qr/^Invalid CODE attributes?: ["']?plùgh["']? at/;
30 eval 'sub ɛ2 ($) : plǖgh(0,0) xyzzy ;';
31 like $@, qr/^Invalid CODE attributes: ["']?plǖgh\(0,0\)["']? /;
33 eval 'my ($x,$y) : plǖgh;';
34 like $@, qr/^Invalid SCALAR attribute: ["']?plǖgh["']? at/;
37 eval '{my $x : plǖgh}';
38 like $@, qr/^Invalid SCALAR attribute: ["']?plǖgh["']? at/;
39 eval '{my ($x,$y) : plǖgh(})}';
40 like $@, qr/^Invalid SCALAR attribute: ["']?plǖgh\(\}\)["']? at/;
42 # More syntax tests from the attributes manpage
43 eval 'my $x : Şʨᚻ(10,ᕘ(7,3)) : 에ㄒ펜ሲ;';
44 like $@, qr/^Invalid SCALAR attributes: ["']?Şʨᚻ\(10,ᕘ\(7,3\)\) : 에ㄒ펜ሲ["']? at/;
45 eval q/my $x : Ugļᑈ('\(") :받;/;
46 like $@, qr/^Invalid SCALAR attributes: ["']?Ugļᑈ\('\\\("\) : 받["']? at/;
47 eval 'my $x : Şʨᚻ(10,ᕘ();';
48 like $@, qr/^Unterminated attribute parameter in attribute list at/;
49 eval q/my $x : Ugļᑈ('(');/;
50 like $@, qr/^Unterminated attribute parameter in attribute list at/;
52 sub A::MODIFY_SCALAR_ATTRIBUTES { return }
53 eval 'my A $x : plǖgh;';
54 like $@, qr/^SCALAR package attribute may clash with future reserved word: ["']?plǖgh["']? at/;
56 eval 'my A $x : plǖgh plover;';
57 like $@, qr/^SCALAR package attributes may clash with future reserved words: ["']?plǖgh["']? /;
59 no warnings 'reserved';
60 eval 'my A $x : plǖgh;';
63 eval 'package Càt; my Càt @socks;';
69 sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
72 *Y::bar = \&X::ᕘ; # second time for -w
73 eval 'package Z; sub Y::bar : ᕘ';
76 # Begin testing attributes that tie
81 sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
82 sub FETCH { ${$_[0]} }
88 sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttìè', -1; (); }
95 $x != $i*2 and ::is $x, $i*2;
100 eval 'our ${""} : ᕘ = 1';
101 like $@, qr/Can't declare scalar dereference in "our"/;
102 eval 'my $$ᕘ : bar = 1';
103 like $@, qr/Can't declare scalar dereference in "my"/;
106 # this will segfault if it fails
108 { my $dummy = index 'ᕘ', PVBM }
110 ok !defined(eval 'attributes::get(\PVBM)'),
111 'PVBMs don\'t segfault attributes::get';
114 # [perl #49472] Attributes + Unknown Error
117 sub MODIFY_CODE_ATTRIBUTE{}
118 sub f:Blah {$nosuchvar};
122 like ($err, qr/Global symbol "\$nosuchvar" requires /, 'perl #49472');
125 # Test that code attributes always get applied to the same CV that
126 # we're left with at the end (bug#66970).
130 sub MODIFY_CODE_ATTRIBUTES { $c = $_[1]; () }
131 $c=undef; eval 'sub t0 :ᕘ';
132 main::ok $c == \&{"t0"};
133 $c=undef; eval 'sub t1 :ᕘ { }';
134 main::ok $c == \&{"t1"};
135 $c=undef; eval 'sub t2';
137 $c=undef; eval 'sub t2 :ᕘ';
138 main::ok $c == \&{"t2"} && $c == $t2a;
139 $c=undef; eval 'sub t3';
141 $c=undef; eval 'sub t3 :ᕘ { }';
142 main::ok $c == \&{"t3"} && $c == $t3a;
143 $c=undef; eval 'sub t4 :ᕘ';
146 $c=undef; eval 'sub t4 :ᕘ';
147 main::ok $c == \&{"t4"} && $c == $t4b && $c == $t4a;
148 $c=undef; eval 'sub t5 :ᕘ';
151 $c=undef; eval 'sub t5 :ᕘ { }';
152 main::ok $c == \&{"t5"} && $c == $t5b && $c == $t5a;
155 # [perl #68560] Calling closure prototypes (only accessible via :attr)
159 sub MODIFY_CODE_ATTRIBUTES { $proto = $_[1]; _: }
165 eval { $proto->() }; # used to crash in pp_entersub
166 like $@, qr/^Closure prototype called/,
167 "Calling closure proto with (no) args";
168 eval { () = &$proto }; # used to crash in pp_leavesub
169 like $@, qr/^Closure prototype called/,
170 'Calling closure proto with no @_ that returns a lexical';
173 # [perl #68658] Attributes on stately variables
176 sub MODIFY_SCALAR_ATTRIBUTES { () }
179 eval 'sub ᕘ { use 5.01; state $x :A0 = $i++; $x_values .= $x }';
182 is $x_values, '00', 'state with attributes';
187 sub MODIFY_SCALAR_ATTRIBUTES{}
188 sub MODIFY_ARRAY_ATTRIBUTES{ }
189 sub MODIFY_HASH_ATTRIBUTES{ }
190 my ($cows, @go, %bong) : テa퐅Š = qw[ jibber jabber joo ];
191 ::is $cows, 'jibber', 'list assignment to scalar with attrs';
192 ::is "@go", 'jabber joo', 'list assignment to array with attrs';