A simple state machine (with information missing):

INITIAL_STATE SOME_STATE FINAL_STATE +-----+ +-----+ +-----+ start--->| |---+--->| |------->| |--->accept +-----+ | +-----+ +-----+ | | +-------+

We can imagine a state machine as a bunch of gotos.

INITIAL_STATE: { ... goto SOME_STATE; } SOME_STATE: { ... if ( ... ) { goto SOME_STATE; } else { goto FINAL_STATE; } } FINAL_STATE: { ... }
We could replace those gotos with sub calls.
sub initial_state { ... return some_state(); } sub some_state { ... if ( ... ) { return some_state(); } else { return final_state() } } sub final_state { ... return; } initial_state();

The stack will keep growing and growing unless we perform tail call elimination, which can be done using goto &SUB

sub initial_state { ... goto &some_state; } sub some_state { ... if ( ... ) { goto &some_state; } else { goto &final_state; } } sub final_state { ... return; } initial_state();

This is the approach used by the code in question. goto &sub is slow, though. (Slower than a sub call.) I think we could use a loop to speed things up.

sub initial_state { ... return \&some_state; } sub some_state { ... if ( ... ) { return \&some_state; } else { return \&final_state; } } sub final_state { ... return undef; } my $state = \&initial_state; while ( $state ) { $state = $state->(); }

The code in question appears to have some code place to support this. It "returns" the new handler sub (by assigning it to $_[0]->{state}), but it never ends up using it.


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.