3 # "This IS structured code. It's just randomly structured."
17 local $SIG{__WARN__} = sub { if ($_[0] =~ m/jump into a construct/) { $deprecated++; } else { warn $_[0] } };
42 is($foo, 2, 'escape while loop');
47 is($foo, 4, 'second escape while loop');
49 my $r = run_perl(prog => 'goto foo;', stderr => 1);
50 like($r, qr/label/, 'cant find label');
61 ok($ok, 'goto in sub');
72 is(curr_test(), 20, 'FINALE');
74 # does goto LABEL handle block contexts correctly?
75 # note that this scope-hopping differs from last & next,
76 # which always go up-scope strictly.
87 is($count, 0, 'OTHER');
93 is($count, 1, 'THIRD');
97 is($count, 2, 'end of loop');
99 # Does goto work correctly within a for(;;) loop?
100 # (BUG ID 20010309.004)
102 for(my $i=0;!$i++;) {
105 label: is($x, 1, 'goto inside a for(;;) loop body from inside the body');
108 # Does goto work correctly going *to* a for(;;) loop?
109 # (make sure it doesn't skip the initializer)
112 FORL1: for ($y=1; $z;) {
113 ok($y, 'goto a for(;;) loop, from outside (does initializer)');
118 # Even from within the loop?
120 FORL2: for($y=1; 1;) {
122 ok($y, 'goto a for(;;) loop, from inside (does initializer)');
129 # Does goto work correctly within a try block?
130 # (BUG ID 20000313.004) - [perl #2359]
135 LABEL20: $ok = 1 if $variable;
137 ok($ok, 'works correctly within a try block');
138 is($@, "", '...and $@ not set');
140 # And within an eval-string?
145 LABEL21: $ok = 1 if $variable;
147 ok($ok, 'works correctly within an eval string');
148 is($@, "", '...and $@ still not set');
151 # Test that goto works in nested eval-string
164 ok($ok, 'works correctly in a nested eval string');
171 { goto A; A: $ok = 1 } continue { }
172 ok($ok, '#20357 goto inside /{ } continue { }/ loop');
175 { do { goto A; A: $ok = 1 } while $false }
176 ok($ok, '#20154 goto inside /do { } while ()/ loop');
178 foreach(1) { goto A; A: $ok = 1 } continue { };
179 ok($ok, 'goto inside /foreach () { } continue { }/ loop');
183 A: { if ($false) { redo A; B: $ok = 1; redo A; } }
184 goto B unless $count++;
188 ok($ok, '#19061 loop label wiped away by goto');
194 for ($p=1;$p && goto A;$p=0) { A: $ok = 1 }
195 ok($ok, 'weird case of goto and for(;;) loop');
200 # bug #9990 - don't prematurely free the CV we're &going to.
204 goto sub { $x=0; ok(1,"don't prematurely free CV\n") }
208 # bug #22181 - this used to coredump or make $x undefined, due to
209 # erroneous popping of the inner BLOCK context
212 for ($count=0; $count<2; $count++) {
218 is($ok, 1, 'goto in for(;;) with continuation');
220 # bug #22299 - goto in require doesn't find label
222 open my $f, ">Op_goto01.pm" or die;
232 $r = runperl(prog => 'use Op_goto01; print qq[DONE\n]');
233 is($r, "OK\nDONE\n", "goto within use-d file");
234 unlink "Op_goto01.pm";
236 # test for [perl #24108]
239 sub i_return_a_label {
241 return "returned_label";
243 eval { goto +i_return_a_label; };
247 is($count, 1, 'called i_return_a_label');
248 ok($ok, 'skipped to returned_label');
250 # [perl #29708] - goto &foo could leave foo() at depth two with
251 # @_ == PL_sv_undef, causing a coredump
256 'sub f { return if $d; $d=1; my $a=sub {goto &f}; &$a; f() } f(); print qq(ok\n)',
259 is($r, "ok\n", 'avoid pad without an @_');
262 fail('goto moretests');
267 is(curr_test(), 9, 'eval "goto $x"');
269 # Test autoloading mechanism.
272 my ($pack, $file, $line) = caller; # Should indicate original call stats.
273 is("@_ $pack $file $line", "1 2 3 main $::FILE $::LINE",
274 'autoloading mechanism.');
279 no warnings 'redefine';
280 sub one { pass('sub one'); goto &two; fail('sub one tail'); }
286 $::LINE = __LINE__ + 1;
290 my $wherever = 'NOWHERE';
291 eval { goto $wherever };
292 like($@, qr/Can't find label NOWHERE/, 'goto NOWHERE sets $@');
295 # see if a modified @_ propagates
299 sub DESTROY { my $s = shift; ::is($s->[0], $i, "destroy $i"); }
300 sub show { ::is(+@_, 5, "show $i",); }
301 sub start { push @_, 1, "foo", {}; goto &show; }
302 for (1..3) { $i = $_; start(bless([$_]), 'bar'); }
309 sub AUTOLOAD { $ok = 1 if "@_" eq "foo" }
316 my $wherever = 'FINALE';
319 fail('goto $wherever');
322 # test goto duplicated labels.
328 L4: # not outer scope
335 like($@, qr/Can't "goto" into the middle of a foreach loop/,
336 'catch goto middle of foreach');
339 # ambiguous label resolution (outer scope means endless loop!)
343 is($z, 10, 'prefer same scope (loop body) to outer scope (loop entry)');
347 is($z, 10, 'prefer same scope: second');
355 is($z, 10, 'prefer this scope (block body) to outer scope (block entry)');
359 is($z, 10, 'prefer this scope: second');
366 L3: # not inner scope
370 is($z, 10, 'prefer this scope to inner scope');
374 is($z, 10, 'prefer this scope to inner scope: second');
377 L4: # not outer scope
381 L4: # not inner scope
385 is($z, 1, 'prefer this scope to inner,outer scopes');
389 is($z, 1, 'prefer this scope to inner,outer scopes: second');
395 L2: # without this, fails 1 (middle) out of 3 iterations
400 "same label, multiple times in same scope (choose 1st) $loop");
401 goto L2 if $z == 10 and not $loop++;
406 # deep recursion with gotos eventually caused a stack reallocation
407 # which messed up buggy internals that didn't expect the stack to move
411 no warnings 'recursion';
416 $_[0] ? +1 + recurse1($_[0] - 1) : 0
418 is(recurse1(500), 500, 'recursive goto &foo');
420 # [perl #32039] Chained goto &sub drops data too early.
422 sub a32039 { @_=("foo"); goto &b32039; }
423 sub b32039 { goto &c32039; }
424 sub c32039 { is($_[0], 'foo', 'chained &goto') }
427 # [perl #35214] next and redo re-entered the loop with the wrong cop,
428 # causing a subsequent goto to crash
434 'for ($_=0;$_<3;$_++){A: if($_==1){next} if($_==2){$_++;goto A}}print qq(ok\n)'
436 is($r, "ok\n", 'next and goto');
441 'for ($_=0;$_<3;$_++){A: if($_==1){$_++;redo} if($_==2){$_++;goto A}}print qq(ok\n)'
443 is($r, "ok\n", 'redo and goto');
446 # goto &foo not allowed in evals
451 like($@, qr/Can't goto subroutine from an eval-string/, 'eval string');
453 like($@, qr/Can't goto subroutine from an eval-block/, 'eval block');
455 # [perl #36521] goto &foo in warn handler could defeat recursion avoider
460 prog => 'my $d; my $w = sub { return if $d++; warn q(bar)}; local $SIG{__WARN__} = sub { goto &$w; }; warn q(foo);'
462 like($r, qr/bar/, "goto &foo in warn");
466 local $TODO = "[perl #43403] goto() from an if to an else doesn't undo local () changes";
467 our $global = "unmodified";
468 if ($global) { # true but not constant-folded
469 local $global = "modified";
472 ELSE: is($global, "unmodified");