Tailcall Optimization

Learn how to do tailcall optimization in Perl.

Tailcalls

A tailcall occurs when the last expression within a function is a call to another function. The outer function’s return value becomes the inner function’s return value:

Press + to interact
sub log_and_greet_person {
my $name = shift;
# log the greeting into some log file...
return greet_person( $name );
}
sub greet_person {
my $name = shift;
# ...
say "Hi $name, and welcome!";
}
log_and_greet_person "Sam";

Tailcall optimization

Returning from greet_person() directly to the caller of log_and_greet_person() is more efficient than returning to ...