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 #99850, which is similar - freeing the subroutine we are about to
209 # go(in)to during a FREETMPS call should not crash perl.
213 DESTROY { undef &reftype }
214 eval { sub { my $guard = bless []; goto &reftype }->() };
216 like $@, qr/^Goto undefined subroutine &_99850::reftype at /,
217 'goto &foo undefining &foo on sub cleanup';
219 # bug #22181 - this used to coredump or make $x undefined, due to
220 # erroneous popping of the inner BLOCK context
223 for ($count=0; $count<2; $count++) {
229 is($ok, 1, 'goto in for(;;) with continuation');
231 # bug #22299 - goto in require doesn't find label
233 open my $f, ">Op_goto01.pm" or die;
243 $r = runperl(prog => 'use Op_goto01; print qq[DONE\n]');
244 is($r, "OK\nDONE\n", "goto within use-d file");
245 unlink_all "Op_goto01.pm";
247 # test for [perl #24108]
250 sub i_return_a_label {
252 return "returned_label";
254 eval { goto +i_return_a_label; };
258 is($count, 1, 'called i_return_a_label');
259 ok($ok, 'skipped to returned_label');
261 # [perl #29708] - goto &foo could leave foo() at depth two with
262 # @_ == PL_sv_undef, causing a coredump
267 'sub f { return if $d; $d=1; my $a=sub {goto &f}; &$a; f() } f(); print qq(ok\n)',
270 is($r, "ok\n", 'avoid pad without an @_');
273 fail('goto moretests');
278 is(curr_test(), 9, 'eval "goto $x"');
280 # Test autoloading mechanism.
283 my ($pack, $file, $line) = caller; # Should indicate original call stats.
284 is("@_ $pack $file $line", "1 2 3 main $::FILE $::LINE",
285 'autoloading mechanism.');
290 no warnings 'redefine';
291 sub one { pass('sub one'); goto &two; fail('sub one tail'); }
297 $::LINE = __LINE__ + 1;
301 my $wherever = 'NOWHERE';
302 eval { goto $wherever };
303 like($@, qr/Can't find label NOWHERE/, 'goto NOWHERE sets $@');
306 # see if a modified @_ propagates
310 sub DESTROY { my $s = shift; ::is($s->[0], $i, "destroy $i"); }
311 sub show { ::is(+@_, 5, "show $i",); }
312 sub start { push @_, 1, "foo", {}; goto &show; }
313 for (1..3) { $i = $_; start(bless([$_]), 'bar'); }
320 sub AUTOLOAD { $ok = 1 if "@_" eq "foo" }
327 my $wherever = 'FINALE';
330 fail('goto $wherever');
333 # test goto duplicated labels.
339 L4: # not outer scope
346 like($@, qr/Can't "goto" into the middle of a foreach loop/,
347 'catch goto middle of foreach');
350 # ambiguous label resolution (outer scope means endless loop!)
354 is($z, 10, 'prefer same scope (loop body) to outer scope (loop entry)');
358 is($z, 10, 'prefer same scope: second');
366 is($z, 10, 'prefer this scope (block body) to outer scope (block entry)');
370 is($z, 10, 'prefer this scope: second');
377 L3: # not inner scope
381 is($z, 10, 'prefer this scope to inner scope');
385 is($z, 10, 'prefer this scope to inner scope: second');
388 L4: # not outer scope
392 L4: # not inner scope
396 is($z, 1, 'prefer this scope to inner,outer scopes');
400 is($z, 1, 'prefer this scope to inner,outer scopes: second');
406 L2: # without this, fails 1 (middle) out of 3 iterations
411 "same label, multiple times in same scope (choose 1st) $loop");
412 goto L2 if $z == 10 and not $loop++;
417 # deep recursion with gotos eventually caused a stack reallocation
418 # which messed up buggy internals that didn't expect the stack to move
422 no warnings 'recursion';
427 $_[0] ? +1 + recurse1($_[0] - 1) : 0
430 $SIG{__WARN__} = sub { ++$w };
431 is(recurse1(500), 500, 'recursive goto &foo');
432 is $w, 0, 'no recursion warnings for "no warnings; goto &sub"';
433 delete $SIG{__WARN__};
435 # [perl #32039] Chained goto &sub drops data too early.
437 sub a32039 { @_=("foo"); goto &b32039; }
438 sub b32039 { goto &c32039; }
439 sub c32039 { is($_[0], 'foo', 'chained &goto') }
442 # [perl #35214] next and redo re-entered the loop with the wrong cop,
443 # causing a subsequent goto to crash
449 'for ($_=0;$_<3;$_++){A: if($_==1){next} if($_==2){$_++;goto A}}print qq(ok\n)'
451 is($r, "ok\n", 'next and goto');
456 'for ($_=0;$_<3;$_++){A: if($_==1){$_++;redo} if($_==2){$_++;goto A}}print qq(ok\n)'
458 is($r, "ok\n", 'redo and goto');
461 # goto &foo not allowed in evals
465 like($@, qr/Can't goto subroutine from an eval-string/, 'eval string');
467 like($@, qr/Can't goto subroutine from an eval-block/, 'eval block');
469 # goto &foo leaves @_ alone when called from a sub
470 sub returnarg { $_[0] };
472 local *_ = ["ick and queasy"];
474 }->("quick and easy"), "ick and queasy",
475 'goto &foo with *_{ARRAY} replaced';
477 sub { local *_ = \@__; goto &utf8::decode }->("no thinking aloud");
478 is "@__", chr 256, 'goto &xsub with replaced *_{ARRAY}';
480 # And goto &foo should leave reified @_ alone
481 sub { *__ = \@_; goto &null } -> ("rough and tubbery");
482 is ${*__}[0], 'rough and tubbery', 'goto &foo leaves reified @_ alone';
485 # [perl #36521] goto &foo in warn handler could defeat recursion avoider
490 prog => 'my $d; my $w = sub { return if $d++; warn q(bar)}; local $SIG{__WARN__} = sub { goto &$w; }; warn q(foo);'
492 like($r, qr/bar/, "goto &foo in warn");
496 local $TODO = "[perl #43403] goto() from an if to an else doesn't undo local () changes";
497 our $global = "unmodified";
498 if ($global) { # true but not constant-folded
499 local $global = "modified";
502 ELSE: is($global, "unmodified");
512 F1:++$x and eval 'return if ++$y == 10; goto F1;';
514 'labels outside evals can be distinguished from the start of the eval');
518 die "You can't get here";
521 ouch_eth: pass('labels persist even if their statement is optimised away');
534 skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
536 open(CHOLET, ">", \$cholet);
539 $foo .= "(".$cholet.")";
540 is($foo, "(0)(1)(wellington\n)", "label before format decl");
549 sub alderney { return "tobermory"; }
551 $foo .= "(".alderney().")";
552 is($foo, "(A)(B)(tobermory)", "label before sub decl");
554 $foo = "[0:".__PACKAGE__."]";
561 $foo .= "[1:".__PACKAGE__."]";
562 $foo .= "[2:".__PACKAGE__."]";
564 $foo .= "[3:".__PACKAGE__."]";
565 is($foo, "[0:main][1:Tomsk][2:Tomsk][3:main]", "label before package decl");
567 $foo = "[A:".__PACKAGE__."]";
574 $foo .= "[B:".__PACKAGE__."]";
576 $foo .= "[C:".__PACKAGE__."]";
577 is($foo, "[A:main][B:Cairngorm][C:main]", "label before package block");
586 BEGIN { $obidos = "x"; }
587 $foo .= "{1$obidos}";
588 is($foo, "{0}{1x}", "label before BEGIN block");
590 $foo = "{A:".(1.5+1.5)."}";
597 $foo .= "{B:".(1.5+1.5)."}";
598 is($foo, "{A:3}{B:2}", "label before use decl");
608 is($foo, "<0><1><2>", "first of three stacked labels");
618 is($foo, "<A><B><C>", "second of three stacked labels");
628 is($foo, ",0.,1.,2.", "third of three stacked labels");
630 # [perl #112316] Wrong behavior regarding labels with same prefix
631 sub same_prefix_labels {
637 if ( !$first_time ) {
651 same_prefix_labels(),
652 "perl 112316: goto and labels with the same prefix doesn't get mixed up"
655 eval { my $x = ""; goto $x };
656 like $@, qr/^goto must have label at /, 'goto $x where $x is empty string';
658 like $@, qr/^goto must have label at /, 'goto ""';
660 like $@, qr/^goto must have label at /, 'argless goto';
662 eval { my $x = "\0"; goto $x };
663 like $@, qr/^Can't find label \0 at /, 'goto $x where $x begins with \0';
665 like $@, qr/^Can't find label \0 at /, 'goto "\0"';