I think tail call elimination aka optimization is wrong here , because it's not optimized in Perl (you noted it's slow)

One can call it tail-call (without elimination)

TCE in languages like LISP optimize calls to jumped and can replace the necessity for loop constructs.

> by assigning it to $_[0]->{state}), but it never ends up using it.

It does, but only once as Boolean test.

> goto &sub is slower

Really? I expected the same...

But I kind of remember that the implementation is awkwardly cleaning the frame right after it was created...

> I think we could use a loop

I had a similar idea, but I would return names not references, and store current-state inside the loop. This would improve readability a lot and allow to easily trace/log the execution.

Will add tested code tomorrow

  ... To be continued ...

update
use v5.14; use warnings; my ($state,$last); sub initial_state { say "*** Initializing"; return "some_state"; } sub some_state { my $in = shift; if ( not $in ) { return "some_state"; } else { return "final_state"; } } sub final_state { say "*** Finalizing"; return undef; } $state = "initial_state"; my @input = (0,0,0,1); my $log = 1; while ( $state ) { $last = $state; my $in = shift @input; no strict 'refs'; $state = $state->($in); say "$last \t--($in)-> \t$state" if $log && $state; }
-->
*** Initializing initial_state --(0)-> some_state some_state --(0)-> some_state some_state --(0)-> some_state some_state --(1)-> final_state *** Finalizing

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery


In reply to Re^2: Weird syntax. What does this goto statement do? by LanX
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.