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

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.
  • Comment on Re^2: Weird syntax. What does this goto statement do?

Replies are listed 'Best First'.
Re^3: Weird syntax. What does this goto statement do?
by ikegami (Patriarch) on Jan 01, 2024 at 22:12 UTC

    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?

Re^3: Weird syntax. What does this goto statement do?
by hippo (Archbishop) on Dec 30, 2023 at 16:21 UTC
    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...

        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.

        Because in the code you originally quoted in bold, the v5.10-and-newer state function is not being called. The confusion came because your followon post incorrectly described what you were seeing, and thus the subsequent correct explanation that state can be a built-in function didn't help you understand that specific code correctly. Specifically, you said,

        But there's also an equal sign in this line and an arrow -> and a bareword "state"

        But you didn't mention that the -> arrow was followed by the bareword "state" in braces . And ->{...} is the Arrow Notation , which even existed in a Perl as ancient as v5.8.

        Thus, looking at the code you originally bolded:

        goto &{$_[0]->{state} = \&stateReadLit};

        The "bareword" state is not an instance of the state function, but rather the key to a hash referenced by the hashref $_[0], so the bareword is interpreted as a string "state", not the function state .

        That one line of code is equivalent to the following, which might be more understandable to you:

        $_[0]->{state} = \&stateReadLit; # assign the coderef to the stateRea +dLit subroutine to the 'state' element of the hash referenced by $_[0 +] goto &stateReadLit; # or goto &{ $_[0]->{state} };

        To sum up: there is no surprise that Perl 5.8 can correctly interpret the bareword state in a hashref arrow-notation syntax construction, just like there is no surprise for $h{state} to refer to the state key of the %h hash.