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

Hello harangzsolt33,

From goto:

The goto &NAME form is quite different from the other forms of goto. In fact, it isn't a goto in the normal sense at all, and doesn't have the stigma associated with other gotos. Instead, it exits the current subroutine (losing any changes set by local) and immediately calls in its place the named subroutine using the current value of @_. This is used by AUTOLOAD subroutines that wish to load another subroutine and then pretend that the other subroutine had been called in the first place (except that any modifications to @_ in the current subroutine are propagated to the other subroutine.) After the goto, not even caller will be able to tell that this routine was called first.

NAME needn't be the name of a subroutine; it can be a scalar variable containing a code reference or a block that evaluates to a code reference.

I’ve seen this in the documentation, but never had any need to use it.

Hope that helps,

Athanasius <°(((><contra mundum סתם עוד האקר של פרל,

Replies are listed 'Best First'.
Re^2: Weird syntax. What does this goto statement do? ( 'goto &NAME' use cases)
by LanX (Saint) on Dec 30, 2023 at 17:09 UTC
    > 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

      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

      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

      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]
Re^2: Weird syntax. What does this goto statement do?
by harangzsolt33 (Deacon) on Dec 30, 2023 at 16:17 UTC
    Thank you! That's amazing..