3 # "This IS structured code. It's just randomly structured."
33 is($foo, 2, 'escape while loop');
37 is($foo, 4, 'second escape while loop');
39 my $r = run_perl(prog => 'goto foo;', stderr => 1);
40 like($r, qr/label/, 'cant find label');
51 ok($ok, 'goto in sub');
62 is(curr_test(), 16, 'FINALE');
64 # does goto LABEL handle block contexts correctly?
65 # note that this scope-hopping differs from last & next,
66 # which always go up-scope strictly.
77 is($count, 0, 'OTHER');
83 is($count, 1, 'THIRD');
87 is($count, 2, 'end of loop');
89 # Does goto work correctly within a for(;;) loop?
90 # (BUG ID 20010309.004)
95 label: is($x, 1, 'goto inside a for(;;) loop body from inside the body');
98 # Does goto work correctly going *to* a for(;;) loop?
99 # (make sure it doesn't skip the initializer)
102 FORL1: for ($y=1; $z;) {
103 ok($y, 'goto a for(;;) loop, from outside (does initializer)');
108 # Even from within the loop?
110 FORL2: for($y=1; 1;) {
112 ok($y, 'goto a for(;;) loop, from inside (does initializer)');
119 # Does goto work correctly within a try block?
120 # (BUG ID 20000313.004) - [perl #2359]
125 LABEL20: $ok = 1 if $variable;
127 ok($ok, 'works correctly within a try block');
128 is($@, "", '...and $@ not set');
130 # And within an eval-string?
135 LABEL21: $ok = 1 if $variable;
137 ok($ok, 'works correctly within an eval string');
138 is($@, "", '...and $@ still not set');
141 # Test that goto works in nested eval-string
154 ok($ok, 'works correctly in a nested eval string');
161 { goto A; A: $ok = 1 } continue { }
162 ok($ok, '#20357 goto inside /{ } continue { }/ loop');
165 { do { goto A; A: $ok = 1 } while $false }
166 ok($ok, '#20154 goto inside /do { } while ()/ loop');
168 foreach(1) { goto A; A: $ok = 1 } continue { };
169 ok($ok, 'goto inside /foreach () { } continue { }/ loop');
173 A: { if ($false) { redo A; B: $ok = 1; redo A; } }
174 goto B unless $count++;
177 ok($ok, '#19061 loop label wiped away by goto');
181 for ($p=1;$p && goto A;$p=0) { A: $ok = 1 }
182 ok($ok, 'weird case of goto and for(;;) loop');
185 # bug #9990 - don't prematurely free the CV we're &going to.
189 goto sub { $x=0; ok(1,"don't prematurely free CV\n") }
193 # bug #22181 - this used to coredump or make $x undefined, due to
194 # erroneous popping of the inner BLOCK context
197 for ($count=0; $count<2; $count++) {
203 is($ok, 1, 'goto in for(;;) with continuation');
205 # bug #22299 - goto in require doesn't find label
207 open my $f, ">goto01.pm" or die;
217 $r = runperl(prog => 'use goto01; print qq[DONE\n]');
218 is($r, "OK\nDONE\n", "goto within use-d file");
221 # test for [perl #24108]
224 sub i_return_a_label {
226 return "returned_label";
228 eval { goto +i_return_a_label; };
232 is($count, 1, 'called i_return_a_label');
233 ok($ok, 'skipped to returned_label');
235 # [perl #29708] - goto &foo could leave foo() at depth two with
236 # @_ == PL_sv_undef, causing a coredump
241 'sub f { return if $d; $d=1; my $a=sub {goto &f}; &$a; f() } f(); print qq(ok\n)',
244 is($r, "ok\n", 'avoid pad without an @_');
247 fail('goto moretests');
252 is(curr_test(), 5, 'eval "goto $x"');
254 # Test autoloading mechanism.
257 my ($pack, $file, $line) = caller; # Should indicate original call stats.
258 is("@_ $pack $file $line", "1 2 3 main $::FILE $::LINE",
259 'autoloading mechanism.');
264 no warnings 'redefine';
265 sub one { pass('sub one'); goto &two; fail('sub one tail'); }
271 $::LINE = __LINE__ + 1;
275 my $wherever = 'NOWHERE';
276 eval { goto $wherever };
277 like($@, qr/Can't find label NOWHERE/, 'goto NOWHERE sets $@');
280 # see if a modified @_ propagates
284 sub DESTROY { my $s = shift; ::is($s->[0], $i, "destroy $i"); }
285 sub show { ::is(+@_, 5, "show $i",); }
286 sub start { push @_, 1, "foo", {}; goto &show; }
287 for (1..3) { $i = $_; start(bless([$_]), 'bar'); }
294 sub AUTOLOAD { $ok = 1 if "@_" eq "foo" }
301 my $wherever = 'FINALE';
304 fail('goto $wherever');
307 # test goto duplicated labels.
313 L4: # not outer scope
320 like($@, qr/Can't "goto" into the middle of a foreach loop/,
321 'catch goto middle of foreach');
324 # ambiguous label resolution (outer scope means endless loop!)
328 is($z, 10, 'prefer same scope (loop body) to outer scope (loop entry)');
332 is($z, 10, 'prefer same scope: second');
340 is($z, 10, 'prefer this scope (block body) to outer scope (block entry)');
344 is($z, 10, 'prefer this scope: second');
351 L3: # not inner scope
355 is($z, 10, 'prefer this scope to inner scope');
359 is($z, 10, 'prefer this scope to inner scope: second');
362 L4: # not outer scope
366 L4: # not inner scope
370 is($z, 1, 'prefer this scope to inner,outer scopes');
374 is($z, 1, 'prefer this scope to inner,outer scopes: second');
380 L2: # without this, fails 1 (middle) out of 3 iterations
385 "same label, multiple times in same scope (choose 1st) $loop");
386 goto L2 if $z == 10 and not $loop++;
391 # deep recursion with gotos eventually caused a stack reallocation
392 # which messed up buggy internals that didn't expect the stack to move
396 no warnings 'recursion';
401 $_[0] ? +1 + recurse1($_[0] - 1) : 0
403 is(recurse1(500), 500, 'recursive goto &foo');
405 # [perl #32039] Chained goto &sub drops data too early.
407 sub a32039 { @_=("foo"); goto &b32039; }
408 sub b32039 { goto &c32039; }
409 sub c32039 { is($_[0], 'foo', 'chained &goto') }
412 # [perl #35214] next and redo re-entered the loop with the wrong cop,
413 # causing a subsequent goto to crash
419 'for ($_=0;$_<3;$_++){A: if($_==1){next} if($_==2){$_++;goto A}}print qq(ok)'
421 is($r, "ok", 'next and goto');
426 'for ($_=0;$_<3;$_++){A: if($_==1){$_++;redo} if($_==2){$_++;goto A}}print qq(ok)'
428 is($r, "ok", 'redo and goto');