3 # "This IS structured code. It's just randomly structured."
34 is($foo, 2, 'escape while loop');
38 is($foo, 4, 'second escape while loop');
40 my $r = run_perl(prog => 'goto foo;', stderr => 1);
41 like($r, qr/label/, 'cant find label');
52 ok($ok, 'goto in sub');
63 is(curr_test(), 16, 'FINALE');
65 # does goto LABEL handle block contexts correctly?
66 # note that this scope-hopping differs from last & next,
67 # which always go up-scope strictly.
78 is($count, 0, 'OTHER');
84 is($count, 1, 'THIRD');
88 is($count, 2, 'end of loop');
90 # Does goto work correctly within a for(;;) loop?
91 # (BUG ID 20010309.004)
96 label: is($x, 1, 'goto inside a for(;;) loop body from inside the body');
99 # Does goto work correctly going *to* a for(;;) loop?
100 # (make sure it doesn't skip the initializer)
103 FORL1: for ($y=1; $z;) {
104 ok($y, 'goto a for(;;) loop, from outside (does initializer)');
109 # Even from within the loop?
111 FORL2: for($y=1; 1;) {
113 ok($y, 'goto a for(;;) loop, from inside (does initializer)');
120 # Does goto work correctly within a try block?
121 # (BUG ID 20000313.004) - [perl #2359]
126 LABEL20: $ok = 1 if $variable;
128 ok($ok, 'works correctly within a try block');
129 is($@, "", '...and $@ not set');
131 # And within an eval-string?
136 LABEL21: $ok = 1 if $variable;
138 ok($ok, 'works correctly within an eval string');
139 is($@, "", '...and $@ still not set');
142 # Test that goto works in nested eval-string
155 ok($ok, 'works correctly in a nested eval string');
162 { goto A; A: $ok = 1 } continue { }
163 ok($ok, '#20357 goto inside /{ } continue { }/ loop');
166 { do { goto A; A: $ok = 1 } while $false }
167 ok($ok, '#20154 goto inside /do { } while ()/ loop');
169 foreach(1) { goto A; A: $ok = 1 } continue { };
170 ok($ok, 'goto inside /foreach () { } continue { }/ loop');
174 A: { if ($false) { redo A; B: $ok = 1; redo A; } }
175 goto B unless $count++;
178 ok($ok, '#19061 loop label wiped away by goto');
182 for ($p=1;$p && goto A;$p=0) { A: $ok = 1 }
183 ok($ok, 'weird case of goto and for(;;) loop');
186 # bug #9990 - don't prematurely free the CV we're &going to.
190 goto sub { $x=0; ok(1,"don't prematurely free CV\n") }
194 # bug #22181 - this used to coredump or make $x undefined, due to
195 # erroneous popping of the inner BLOCK context
198 for ($count=0; $count<2; $count++) {
204 is($ok, 1, 'goto in for(;;) with continuation');
206 # bug #22299 - goto in require doesn't find label
208 open my $f, ">Op_goto01.pm" or die;
218 $r = runperl(prog => 'use Op_goto01; print qq[DONE\n]');
219 is($r, "OK\nDONE\n", "goto within use-d file");
220 unlink "Op_goto01.pm";
222 # test for [perl #24108]
225 sub i_return_a_label {
227 return "returned_label";
229 eval { goto +i_return_a_label; };
233 is($count, 1, 'called i_return_a_label');
234 ok($ok, 'skipped to returned_label');
236 # [perl #29708] - goto &foo could leave foo() at depth two with
237 # @_ == PL_sv_undef, causing a coredump
242 'sub f { return if $d; $d=1; my $a=sub {goto &f}; &$a; f() } f(); print qq(ok\n)',
245 is($r, "ok\n", 'avoid pad without an @_');
248 fail('goto moretests');
253 is(curr_test(), 5, 'eval "goto $x"');
255 # Test autoloading mechanism.
258 my ($pack, $file, $line) = caller; # Should indicate original call stats.
259 is("@_ $pack $file $line", "1 2 3 main $::FILE $::LINE",
260 'autoloading mechanism.');
265 no warnings 'redefine';
266 sub one { pass('sub one'); goto &two; fail('sub one tail'); }
272 $::LINE = __LINE__ + 1;
276 my $wherever = 'NOWHERE';
277 eval { goto $wherever };
278 like($@, qr/Can't find label NOWHERE/, 'goto NOWHERE sets $@');
281 # see if a modified @_ propagates
285 sub DESTROY { my $s = shift; ::is($s->[0], $i, "destroy $i"); }
286 sub show { ::is(+@_, 5, "show $i",); }
287 sub start { push @_, 1, "foo", {}; goto &show; }
288 for (1..3) { $i = $_; start(bless([$_]), 'bar'); }
295 sub AUTOLOAD { $ok = 1 if "@_" eq "foo" }
302 my $wherever = 'FINALE';
305 fail('goto $wherever');
308 # test goto duplicated labels.
314 L4: # not outer scope
321 like($@, qr/Can't "goto" into the middle of a foreach loop/,
322 'catch goto middle of foreach');
325 # ambiguous label resolution (outer scope means endless loop!)
329 is($z, 10, 'prefer same scope (loop body) to outer scope (loop entry)');
333 is($z, 10, 'prefer same scope: second');
341 is($z, 10, 'prefer this scope (block body) to outer scope (block entry)');
345 is($z, 10, 'prefer this scope: second');
352 L3: # not inner scope
356 is($z, 10, 'prefer this scope to inner scope');
360 is($z, 10, 'prefer this scope to inner scope: second');
363 L4: # not outer scope
367 L4: # not inner scope
371 is($z, 1, 'prefer this scope to inner,outer scopes');
375 is($z, 1, 'prefer this scope to inner,outer scopes: second');
381 L2: # without this, fails 1 (middle) out of 3 iterations
386 "same label, multiple times in same scope (choose 1st) $loop");
387 goto L2 if $z == 10 and not $loop++;
392 # deep recursion with gotos eventually caused a stack reallocation
393 # which messed up buggy internals that didn't expect the stack to move
397 no warnings 'recursion';
402 $_[0] ? +1 + recurse1($_[0] - 1) : 0
404 is(recurse1(500), 500, 'recursive goto &foo');
406 # [perl #32039] Chained goto &sub drops data too early.
408 sub a32039 { @_=("foo"); goto &b32039; }
409 sub b32039 { goto &c32039; }
410 sub c32039 { is($_[0], 'foo', 'chained &goto') }
413 # [perl #35214] next and redo re-entered the loop with the wrong cop,
414 # causing a subsequent goto to crash
420 'for ($_=0;$_<3;$_++){A: if($_==1){next} if($_==2){$_++;goto A}}print qq(ok\n)'
422 is($r, "ok\n", 'next and goto');
427 'for ($_=0;$_<3;$_++){A: if($_==1){$_++;redo} if($_==2){$_++;goto A}}print qq(ok\n)'
429 is($r, "ok\n", 'redo and goto');
432 # goto &foo not allowed in evals
437 like($@, qr/Can't goto subroutine from an eval-string/, 'eval string');
439 like($@, qr/Can't goto subroutine from an eval-block/, 'eval block');
441 # [perl #36521] goto &foo in warn handler could defeat recursion avoider
446 prog => 'my $d; my $w = sub { return if $d++; warn q(bar)}; local $SIG{__WARN__} = sub { goto &$w; }; warn q(foo);'
448 like($r, qr/bar/, "goto &foo in warn");
452 local $TODO = "[perl #43403] goto() from an if to an else doesn't undo local () changes";
453 our $global = "unmodified";
454 if ($global) { # true but not constant-folded
455 local $global = "modified";
458 ELSE: is($global, "unmodified");