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

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

Yes, it is.


🦛

  • Comment on Re^3: Weird syntax. What does this goto statement do?

Replies are listed 'Best First'.
Re^4: Weird syntax. What does this goto statement do?
by harangzsolt33 (Deacon) on Dec 30, 2023 at 16:34 UTC
    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.

        Oh, thank you for clearing that up! That makes sense now.