perl_boy has asked for the wisdom of the Perl Monks concerning the following question:

I m trying to know hot to properly return from a goto ?
That is,is there an instruction/statement/command that would return to the exact place (after the goto) from inside a goto ?
That instruction/statement/command would be in line 4 of both goto-0.pl goto-1.pl replacing goto CONT; AND retn; respectivly
I have read a few files about the goto statement
I have Google search for a perl statement to get out of a goto label and resume where it left off
perl exit goto
perl return goto
perl resume goto
perl quit goto
but did't find anything usefull

I finally came accross retn and made the goto-1.pl test program printed bellow but there is 1 major flaw with it;it prints 012345678910111213141516171819 WITHOUT printing ---- and a NEWLINE after each number that is,print "----\n"; line 8 is NEVER executed. So I presume retn just jumps back BEFORE goto LOOP; rather than AFTER
So, as a last resort I have done a version (goto-0.pl) whereas I use another label CONT to goto where I jump right after goto from within LOOP label and works but gets warning messages (just bellow)
I have tested the goto-0.pl test program and got the warning message
Use of "goto" to jump into a construct is deprecated at goto-0.pl line 4.
for use of a goto (line 4) in the middle of a while (line 7) but the program work fine
but found nothing about how to return (no return (return can only be used within a sub), no exit, no end WITHIN a goto) from where I left,that is just before the CONT label so I used it.
is there a way a way to return from where we left as in the return statement when using a sub ?
Failling the above how I can hush the above warning message using something else than
perl goto-0.pl 2> /dev/null
that works from the outside. I better like to hush from within using command-line-argument/option/switch and searched man perl the only command-line-argument/option/switch I have found is -U but still outputs to STDERR

I have Google searched
perl CLA command-line-argument option switch hush inhibit warning
but found nothing

goto-0.pl test program
$a=0; LOOP: print $a++; goto CONT; while ($a < 20) { goto LOOP; CONT: print "\n"; }

goto-1.pl test program
$a=0; LOOP: print $a++; retn; while ($a < 20) { goto LOOP; print "----\n"; }

Althought both programs work fine I m a bit concerned about the above warning messages for jumping in the middle of a while

Replies are listed 'Best First'.
Re: how to return from a goto ?
by Corion (Patriarch) on Mar 05, 2022 at 17:29 UTC

    Usually, when you want to return to the original call site, you want to simply perform a subroutine call:

    #!perl use strict; my $counter = 0; sub print_next_counter { print $counter++; }; while ($counter < 20) { print_next_counter(); print "----\n"; }
Re: how to return from a goto ?
by tybalt89 (Monsignor) on Mar 05, 2022 at 23:12 UTC

    No, both programs do not work fine, as you would realize if you added 'use strict;' and 'use warnings;' to your programs.

    Also 'retn' is just a bareword (in void context) and does nothing.

    'goto' has no way to return, perhaps you are confusing it with the Basic command 'GOSUB' ?

Re: how to return from a goto ?
by LanX (Saint) on Mar 05, 2022 at 18:07 UTC
    FWIW: Strictly speaking it's possible, but I doubt it's a good idea:

    goto $EXP

    with $EXP = "label-name"

    so

    sub retn { goto $EXP }

    would do if you've set a global $EXP before.

    Please note that you can't goto into a sub or a foreach loop, so returning there is not possible.

    BUT in 99.9% of all cases there are far better solutions than that ...

    ... maybe you just need to try a continue block?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: how to return from a goto ?
by pryrt (Abbot) on Apr 06, 2022 at 14:19 UTC
    perl_boy, per Re: can't read the 2nd input file;12 new versions of PORT-STATE-SERVICE+IP;14 in all;3 disk,11 memory (part 2) where you said that you "got no replies as an instruction/statement/command that would allow me to return FROM WITHIN the label", you still seem to not have gotten the point: that's not something you need to be able to do. If you think you want to have a goto and return , you haven't thought through the problem correctly, because goto-followed-by-return is the ancient programming practice which was superseded by functions/subroutines. If you think you need a goto at all in Perl, you probably haven't thought through the problem correctly.

    I'm not the expert on such things, and thought that everyone else already explained it well enough. But since you are still confused, I am giving it a go.

    In your first example, goto-0.pl, I cannot imagine how you think it would work. What is the print sequence that you expect if the "goto-label / return-from-label" worked as you want it to?

    In your second example, goto-1.pl, I believe you want to set a to 0, then skip over the "LOOP" and its two lines of commands, then run the while loop. In the while loop, you want to jump into the "LOOP" section, print and increment, then return to the while. If I have interpreted correctly, this would print

    0---- 1---- 2---- ... 17---- 18---- 19----
    But that convoluted logic could be more simply written as
    #!perl use 5.012; # strict, // use warnings; my $a = 0; while($a < 20) { mySubroutine(); print "----\n"; } sub mySubroutine { print $a++; # implied "return" from subroutine; you could make it explicit on +this line if you really wanted to } exit;
    Here, the logic is much more explicit. Basically, I moved your "LOOP" label with its two lines of code to be a subroutine instead of a label.

    Actually, my guess is that both goto-0 and goto-1 were supposed to accomplish the same task. In which case my implementation works for either.

    I cannot go to your mooo URL, because that domain is blocked my IT department, so I cannot see a more practical example of what you're trying to accomplish. But I am quite certain that nearly anything you claim you want to do with goto-labels and return-from-labels would be more practically implemented with subroutines. (There are a few "goto-like" constructs that would be better with nested loops, labels on loops, and judicious use of last LABEL, next LABEL, and redo LABEL) .... But since I cannot see your website (and I probably wouldn't have understood the examples there anyway, if your goto-0 is any indication the examples there), I cannot offer concrete implementations for other constructs if my working example doesn't do what you need.

    I think if you really want more help finding an alternative to the non-existent goto/return pair, you will have to provide pseudocode showing the logic you think you need, using only prints and use goto and pseudoReturn in your pseudocode, and maybe one variable to show where you are in the flow or provide decision-making. Then show us the example output you desire from that pseudocode. Then we can, if we understand your example, show actual working perl code which will provide the same output, using either subroutines or nested while loops (or both). Or maybe give pseudocode with embedded line numbers, then list the sequence of line numbers that you think would run based on that pseudocode. But if you just direct us to an external website, then some of us (including me), will not be able to do anything more for you.

      "[A] fool cannot be protected from his folly. If you attempt to do so, you will not only arouse his animosity but also you will be attempting to deprive him of whatever benefit he is capable of deriving from experience. Never attempt to teach a pig to sing; it wastes your time and annoys the pig." — RAH

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      I think in such a stubborn case it's more efficient to just point him repeatedly to perlintro#Writing-subroutines

        Writing subroutines

        Writing subroutines is easy:

        sub logger { my $logmessage = shift; open my $logfile, ">>", "my.log" or die "Could not open my.log: $!" +; print $logfile $logmessage; }

        Now we can use the subroutine just as any other built-in function:

        logger("We have a logger subroutine!");

        What's that shift? Well, the arguments to a subroutine are available to us as a special array called @_ (see perlvar for more on that). The default argument to the shift function just happens to be @_. So my $logmessage = shift; shifts the first item off the list of arguments and assigns it to $logmessage.

        We can manipulate @_ in other ways too:

        my ($logmessage, $priority) = @_; # common my $logmessage = $_[0]; # uncommon, and ugly

        Subroutines can also return values:

        sub square { my $num = shift; my $result = $num * $num; return $result; }

        Then use it like:

        $sq = square(8);

        For more information on writing subroutines, see perlsub

      He should first tell us whats wrong with that.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: how to return from a goto ?
by perl_boy (Novice) on Mar 19, 2022 at 18:11 UTC
      short answer: do not use goto, period.

      long answer: if you have to ask how to use it, you should def not be using it!!!