Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: how to return from a goto ?

by pryrt (Abbot)
on Apr 06, 2022 at 14:19 UTC ( [id://11142733]=note: print w/replies, xml ) Need Help??


in reply to how to return from a goto ?

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.

Replies are listed 'Best First'.
Re^2: how to return from a goto ?
by Fletch (Bishop) on Apr 06, 2022 at 15:57 UTC
    "[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.

Re^2: how to return from a goto ?
by LanX (Saint) on Apr 06, 2022 at 17:09 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11142733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found