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

&{ ... } is a dereference.

goto &... is a sub call identical to &..., except that the current stack frame is removed from the call stack. This makes the the caller of the sub the caller of the current sub. In this case, I suspect it's used to prevent the call stack from endlessly growing.

Replies are listed 'Best First'.
Re^2: Weird syntax. What does this goto statement do?
by harangzsolt33 (Deacon) on Dec 30, 2023 at 16:14 UTC
    Wouldn't it be easier to just write: goto &stateReadLit; ? There appears to be a sub by the name stateReadLit, so if he wants to jump there, why not just goto and then insert the sub name? But there's also an equal sign in this line and an arrow -> and a bareword "state". I don't even know why this is legal. Nowhere in the script is "state" defined or declared. I mean if it was a scalar variable, I would expect that there would be a my $state = 0; somewhere, but no. The word "state" is just a word that pops up all of a sudden, and I don't know why Perl recognizes a random word that's not a keyword. I mean I looked in Perl functions list, and "state" is not a builtin Perl function.

      Wouldn't it be easier to just write: goto &stateReadLit;?

      All you did was remove the assignment to $_[0]->{state} (then collapsed a remaining reference-dereference). Why do you think removing the assignment is ok?

      I mean I looked in Perl functions list, and "state" is not a builtin Perl function.

      Yes, it is.


      🦛

        Now I am even more amazed. lol

        But it says "The 'state' feature is enabled automatically with a use v5.10 (or higher) declaration in the current scope." Keep in mind the code I pasted above runs on Perl 5.8. And on my computer, I have a copy of Perldoc 5 version 8.8, which is what I use. I couldn't find state in there obviously, because it's an older documentation, and state seems to be a newer feature. But I don't understand why it's working on an older perl. Hmm...