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

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

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

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

    A slow tail call elimination is still a tail call elimination. While it may be used as a speed optimization in other languages, it has another benefit: Not generating a new stack frame. This optimization of memory usage could even be considered the main reason for using it (allowing recursion to be used to create loops without extra memory usage). This is the reason for using tail call elimination that's relevant here, as I mentioned.

    I would return names not references

    Also called symbolic references. So yeah, that works. One doesn't even need to turn off strict if you call them as methods ($self->$method()). I was keeping size and complexity minimal.

    It does, but only once as Boolean test.

    ah, I missed that. Still, the gist of what I said is still accurate.