# Is calling a function only twice a good use of recursion?

Is this, technically, a recursive function? It only gets called once by itself, but it cleaned up my code quite a bit, I think. Or, does it needlessly complicate the code?

sub check_backlog {
my $topic = shift; my$prev_backlog = shift;
my $backlog = undef; my @status = $CMD -topicstat $topic; foreach (@status) { if (/Message count: (\d+)/) {$backlog = $LAST_PAREN_MATCH; if ( !$prev_backlog && $backlog > 1 ) { sleep$topic_wait_time;
check_backlog( $topic,$backlog );
}
elsif ( $backlog >$prev_backlog ) {
check_over_thresholds($backlog); } } } return$backlog;
}

• Could you describe the intent of the code as well? I don't know enough PERL to entirely understand what you are actually doing, so I can't tell whether recursion is considered a good solution here. – Steven Jeuris Mar 2 '11 at 18:16
• @Steven Jeuris The intent is to query a queue, to determine if it is not moving data through it. This was written in perl because the queue is a vendor supplied JMS queue. – Scott Hoffman Mar 2 '11 at 18:55
• To see whether data is moving through, can't you just check the head element and see whether it changes over time? If I interpret that correctly, recursion isn't a good solution IMHO. – Steven Jeuris Mar 2 '11 at 19:11
• @Steven Jeuris - As for checking the head element, I don't know how I'd do that, outside of running this as Java instead. Again, it's not my code administering the queue. Any code examples would be appreciated. – Scott Hoffman Mar 2 '11 at 19:18
• I updated my answer with pseudo code with what I believe would be the proper solution for how I understand the intent of this code at the moment. To get help on how to achieve this in PERL, you probably have more luck on Stack Overflow. – Steven Jeuris Mar 2 '11 at 19:33

Yes, every function which calls itself is considered a recursive function.

Recursion is really useful when traversing trees and such.

As I don't fully understand Perl, I can't really tell whether this case is proper use of recursion, perhaps also provide the intent of your code.

UPDATE

If you simply want to see whether data is passing (or not passing) through a queue, I wouldn't find recursion to be a suitable implemention.

I would expect a function which returns either true or false after a given amount of time, or immediately when data is passing through.

Consider the following pseudo code:

function isDataPassing( queue, time )
{
dataPassing = false;
while ( timePassed < time )
{
if ( headOfQueue != queue.peek() )
{
dataPassing = true;
break;
}
}

return dataPassing;
}


I can't help you with how you would go about implementing this in Perl. StackOverflow is a better location to ask such a question.

• It's Perl not PERL. – Brad Gilbert Dec 16 '11 at 22:54
• In Perl instead of setting a variable in the loop, and later returning it; you would just write return 1; ( 1 being a true value ) – Brad Gilbert Aug 18 '13 at 0:09

I don't know Perl well enough, but this looks suspicious to me. On the one hand, you return a value from check_backlog with:

return $backlog;  But when calling check_backlog you don't use the return value at all! check_backlog($topic, $backlog );  The only result of the recursive call is that it may sleep some time, but it will not affect returned value. But again, I know only a little Perl, so I might be wrong. • The code should flow like this: call #1 - get any count of backlog, call #2 - see that it's been called before, then check if the number of messages queued has grown. If it has, call the threshold check function. – Scott Hoffman Mar 2 '11 at 18:56 • I see what you're saying now. When it does return the$backlog var, it will return the first result, not the second, as it probably should? – Scott Hoffman Mar 2 '11 at 19:16