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 ...
-->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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |