If it's a recursion, it needs to be written as a for loop or some kind of loop. If you see that your sub-routine calls itself, double check that. That's often a program design error.
You absolutely don't need a for() or any other type of loop to perform recursion. Recursion in and of itself kind of eliminates that need:
use warnings;
use strict;
use feature 'say';
my $i = 0;
recurse();
sub recurse {
say ++$i;
recurse() if $i < 5;
}
If the developer ends up with recursion where they didn't mean to, it's almost always a mistake, not a design flaw. A design flaw would be intentionally implementing recursion, but incorrectly (which most of the time crashes due to infinite recursion).
There are very valid uses for recursion, and it works wonderfully when the proper checks and balances are put into place. |