3 # Test scoping issues with embedded code in regexps.
9 skip_all_if_miniperl("no dynamic loading on miniperl, no re");
14 fresh_perl_is <<'CODE', '781745', {}, '(?{}) has its own lexical scope';
15 my $x = 7; my $a = 4; my $b = 5;
16 print "a" =~ /(?{ print $x; my $x = 8; print $x; my $y })a/;
20 fresh_perl_is <<'CODE',
27 a (?{ print $y; local $y = $y+1; print $x; my $x = 8; print $x })
28 b (?{ print $y; local $y = $y+1; print $x; my $x = 9; print $x })
29 c (?{ print $y; local $y = $y+1; print $x; my $x = 10; print $x })
35 '1a82a93a104a85a96a101a 1b82b93b104b85b96b101b 1c82c93c104c85c96c101c ',
37 'multiple (?{})s in loop with lexicals';
39 fresh_perl_is <<'CODE', '781745', {}, 'run-time re-eval has its own scope';
41 my $x = 7; my $a = 4; my $b = 5;
43 print "a" =~ /(?{ print $x; my $x = 8; print $x; my $y })$rest/;
47 fresh_perl_is <<'CODE', '178279371047857967101745', {},
55 a (?{ print $y; local $y = $y+1; print $x; my $x = 8; print $x })
56 b (?{ print $y; local $y = $y+1; print $x; my $x = 9; print $x })
57 c (?{ print $y; local $y = $y+1; print $x; my $x = 10; print $x })
62 'multiple (?{})s in "foo" =~ $string';
64 fresh_perl_is <<'CODE', '178279371047857967101745', {},
72 a (?{ print $y; local $y = $y+1; print $x; my $x = 8; print $x })
73 b (?{ print $y; local $y = $y+1; print $x; my $x = 9; print $x })
74 c (?{ print $y; local $y = $y+1; print $x; my $x = 10; print $x })
79 'multiple (?{})s in "foo" =~ /$string/x';
81 fresh_perl_is <<'CODE', '123123', {},
83 push @regexps, qr/(?{ print $x })a/;
85 "a" =~ $_ for @regexps;
86 "ba" =~ /b$_/ for @regexps;
88 'qr/(?{})/ is a closure';
90 "a" =~ do { package foo; qr/(?{ $::pack = __PACKAGE__ })a/ };
91 is $pack, 'foo', 'qr// inherits package';
92 "a" =~ do { use re "/x"; qr/(?{ $::re = qr-- })a/ };
93 is $re, '(?^x:)', 'qr// inherits pragmata';
96 "ba" =~ /b${\do { package baz; qr|(?{ $::pack = __PACKAGE__ })a| }}/;
97 is $pack, 'baz', '/text$qr/ inherits package';
98 "ba" =~ m+b${\do { use re "/i"; qr|(?{ $::re = qr-- })a| }}+;
99 is $re, '(?^i:)', '/text$qr/ inherits pragmata';
104 "ba" =~ /${\'(?{ $::pack = __PACKAGE__ })a'}/;
106 is $pack, 'bar', '/$text/ containing (?{}) inherits package';
109 "ba" =~ /${\'(?{ $::re = qr -- })a'}/;
111 is $re, '(?^m:)', '/$text/ containing (?{}) inherits pragmata';
113 fresh_perl_is <<'CODE', '45', { stderr => 1 }, '(?{die})';
114 my $a=4; my $b=5; eval { "a" =~ /(?{die})a/ }; print $a,$b;
117 fresh_perl_is <<'CODE', 'Y45', { stderr => 1 }, '(?{eval{die}})';
119 "a" =~ /(?{eval { die; print "X" }; print "Y"; })a/; print $a,$b;
122 fresh_perl_is <<'CODE',
124 sub f { "a" =~ /(?{print((caller(0))[3], "\n");})a/ };
129 { stderr => 1 }, 'sub f {(?{caller})}';
132 fresh_perl_is <<'CODE',
134 sub f { print ((caller(0))[3], "-", (caller(1))[3], "\n") };
138 "main::f-(unknown)\n45",
139 { stderr => 1 }, 'sub f {caller} /(?{f()})/';
142 fresh_perl_is <<'CODE',
145 "a" =~ /(?{print "X"; return; print "Y"; })a/;
152 { stderr => 1 }, 'sub f {(?{return})}';
155 fresh_perl_is <<'CODE',
156 my $a=4; my $b=5; "a" =~ /(?{last})a/; print $a,$b
158 q{Can't "last" outside a loop block at - line 1.},
159 { stderr => 1 }, '(?{last})';
162 fresh_perl_is <<'CODE',
163 my $a=4; my $b=5; "a" =~ /(?{for (1..4) {last}})a/; print $a,$b
166 { stderr => 1 }, '(?{for {last}})';
169 fresh_perl_is <<'CODE',
170 for (1) { my $a=4; my $b=5; "a" =~ /(?{last})a/ }; print $a,$b
172 q{Can't "last" outside a loop block at - line 1.},
173 { stderr => 1 }, 'for (1) {(?{last})}';
176 fresh_perl_is <<'CODE',
177 my $a=4; my $b=5; eval { "a" =~ /(?{last})a/ }; print $a,$b
180 { stderr => 1 }, 'eval {(?{last})}';
183 fresh_perl_is <<'CODE',
184 my $a=4; my $b=5; "a" =~ /(?{next})a/; print $a,$b
186 q{Can't "next" outside a loop block at - line 1.},
187 { stderr => 1 }, '(?{next})';
190 fresh_perl_is <<'CODE',
191 my $a=4; my $b=5; "a" =~ /(?{for (1,2,3) { next} })a/; print $a,$b
194 { stderr => 1 }, '(?{for {next}})';
197 fresh_perl_is <<'CODE',
198 for (1) { my $a=4; my $b=5; "a" =~ /(?{next})a/ }; print $a,$b
200 q{Can't "next" outside a loop block at - line 1.},
201 { stderr => 1 }, 'for (1) {(?{next})}';
204 fresh_perl_is <<'CODE',
205 my $a=4; my $b=5; eval { "a" =~ /(?{next})a/ }; print $a,$b
208 { stderr => 1 }, 'eval {(?{next})}';
211 fresh_perl_is <<'CODE',
213 "a" =~ /(?{ goto FOO; print "X"; })a/;
218 q{Can't "goto" out of a pseudo block at - line 2.},
219 { stderr => 1 }, '{(?{goto})}';
223 local $::TODO = "goto doesn't yet work in pseduo blocks";
224 fresh_perl_is <<'CODE',
226 "a" =~ /(?{ goto FOO; print "X"; FOO: print "Y"; })a/;
232 { stderr => 1 }, '{(?{goto FOO; FOO:})}';
236 fresh_perl_is <<'CODE', '', { stderr => 1 }, '(?{eval{die}})';
237 "$_$_$_"; my $foo; # these consume pad entries and ensure a SEGV on opd perls
238 "" =~ m{(?{exit(0)})};
243 { my $y = "a"; $y =~ /a(?{ undef *_ })/ }
244 pass "undef *_ in a re-eval does not cause a double free";
246 # make sure regexp warnings are reported on the right line
247 # (we don't care what warning; the 32768 limit is just one
248 # that was easy to reproduce) */
252 local $SIG{__WARN__} = sub { $w = "@_" };
253 my $qr = qr/(??{'a'})/;
255 ("a" x 40_000) =~ /^$qr(ab*)+/; my $line = __LINE__;
256 like($w, qr/recursion limit.* line $line\b/, "warning on right line");
259 # on immediate exit from pattern with code blocks, make sure PL_curcop is
269 local $SIG{__WARN__} = sub { $w .= "@_" };
270 $l = __LINE__; "1" =~ /^1$c/x and warn "foo";
271 like($w, qr/foo.+line $l/, 'curcop 1');
274 $l = __LINE__; "4" =~ /^1$c/x or warn "foo";
275 like($w, qr/foo.+line $l/, 'curcop 2');
278 $l = __LINE__; "1" =~ /^$c/x and warn "foo";
279 like($w, qr/foo.+line $l/, 'curcop 3');
282 $l = __LINE__; "4" =~ /^$c/x or warn "foo";
283 like($w, qr/foo.+line $l/, 'curcop 4');