in reply to Re: Weird syntax. What does this goto statement do?
in thread Weird syntax. What does this goto statement do?

> but never had any need to use it.

In most cases it's used in combination with AUTOLOAD because after delegating the call you don't want to return to the AUTOLOAD routine, hence a call frame must be skipped.

Some use it to implement continuations or coroutines, but it's not very performant compared to languages like LISP.

Similarly you can implement case/switch mechanism using it with named subroutines instead of labels, but without much of a spead gain compared to a classical dispatch table.

Edit

The author in question is implementing a state machine via dispatch tables and doesn't want the call stack to fill up with every state switch.

He could also have used classic goto LABLE; , but (obviously) using subs gives him more flexibility in maintenance than spaghetti code would.

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

  • Comment on Re^2: Weird syntax. What does this goto statement do? ( 'goto &NAME' use cases)
  • Download Code

Replies are listed 'Best First'.
Re^3: Weird syntax. What does this goto statement do? ( 'goto &NAME' use cases)
by Darkwing (Scribe) on Jan 03, 2024 at 16:05 UTC

    Some years ago, i used the goto when i needed a throw method with a stack trace that starts where the method was called. I used Exception::Class exceptions, and the method looked something like this:

    sub throw { my $self = shift; my %args = @_; @_ = ('Local::Exception', # ... more params taken from %args and from the object ... ); goto &Exception::Class::Base::throw; }
      Yep that's a case of wrapping a function call which inspects the call chain.

      😊👍🏼

      I did similar things too but with carp routines or recently with a self made logger.

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

Re^3: Weird syntax. What does this goto statement do? ( 'goto &NAME' use cases)
by ikegami (Patriarch) on Jan 03, 2024 at 17:07 UTC

    You said "coroutines", but I think you meant "tail call elimination"?

      I meant Co-routines, I saw someone demoing them in Perl many years ago in tinita's German Perl Board.

      Not very efficient tho.

      And my LISP skills are restricted to dabbling with emacs.

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

        Implementing co-routines (co-operative multitasking) requires

        • The ability to create a context (call stack and execution pointer).
        • The ability to swap the active context with another.
        • The ability to free a context.

        (Example of a C co-routine implementation achieved using makecontext, swapcontext, getcontext and setcontext.)

        goto &foo does not help with any of that.

        On the other hand, it can be used to eliminate tail calls, and tail call elimination is something LISP does.

        The OP uses goto &sub for tail call elimination.

Re^3: Weird syntax. What does this goto statement do? ( 'goto &NAME' use cases)
by choroba (Cardinal) on Jan 03, 2024 at 21:55 UTC
    I used it in Karel to implement WHILE and REPEAT (basically, if the loop should continue, the step that evaluated the condition is directly followed by the step that runs the first command in the loop body).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]