4
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Steven Jeuris Mar 2 '11 at 18:16
  • \$\begingroup\$ @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. \$\endgroup\$ – Scott Hoffman Mar 2 '11 at 18:55
  • \$\begingroup\$ 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. \$\endgroup\$ – Steven Jeuris Mar 2 '11 at 19:11
  • \$\begingroup\$ @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. \$\endgroup\$ – Scott Hoffman Mar 2 '11 at 19:18
  • \$\begingroup\$ 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. \$\endgroup\$ – Steven Jeuris Mar 2 '11 at 19:33
3
\$\begingroup\$

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 )
{
   headOfQueue = queue.peek();
   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.

\$\endgroup\$
  • 1
    \$\begingroup\$ It's Perl not PERL. \$\endgroup\$ – Brad Gilbert Dec 16 '11 at 22:54
  • \$\begingroup\$ 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 ) \$\endgroup\$ – Brad Gilbert Aug 18 '13 at 0:09
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Scott Hoffman Mar 2 '11 at 18:56
  • \$\begingroup\$ 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? \$\endgroup\$ – Scott Hoffman Mar 2 '11 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.