[Solved] Perl, Deep recursion? exit() ignored?

Hi all,

I am calling a subroutine which checks if a log file is writeable and if not, prints something stdout and also log something into the same file, which doesn't work neither of course.
Anyway, there is enough exit() calls, that should stop it working, but the problem is, that I get the message about the file permissions (which I set on intent to produce the problem) in and infinite loop, though there is no loop calling the functions.

In all these thousands lines of messages I found inbetween this hint:

Deep recursion on subroutine "main::log_me" at /opt/somepath/someprog line 230.
Deep recursion on subroutine "main::print_err" at /opt/somepath/someprog line 256.

These two functions are here:

  +233  sub debug {
  +234          ($package, $filename, $line) = caller();
  +235          print "Package:  $package\n";
  +236          print "Filename: $filename\n";
  +237          print "Line:     $line\n";
  +238
  +239          ## Debugging function
  +240          if( $_debug eq lc("on") ){
  +241                  if( ! open( FH_LOG, ">> $W_LOG" ) ){
  +242                          my $err_msg = sprintf("ERROR: Can not write log file [ $W_LOG ]: %s", $!);
  +243                          &print_err($err_msg);
  +244                          &log_me($the_end);
  +245                          exit(3);
  +246                  }
  +247                  print FH_LOG "DEBUG -- @_\n";
  +248                  close(FH_LOG);
  +249          }
  +250  }
  +251
  +252  sub log_me {
  +253          ## Logging function
  +254          if( ! open( FH_LOG, ">> $W_LOG" ) ){
  +255                  my $err_msg = sprintf("ERROR: Can not write log file [ $W_LOG ]: %s", $!);
  +256                  &print_err($err_msg);
  +257                  &log_me($the_end);
  +258                  exit(3);
  +259          }
  +260          print FH_LOG "$date -- @_\n";
  +261          close(FH_LOG);
  +262  }

It should at least hit that exit(3); in log_me(), but it doesn't. Instead I get this spam of messages.

As you can see in the head of debug() I added caller() to get a clue which is calling it. It showed me it is this line:

  +374  &debug("=====> Rotating logs ...");

... which is in the main part of the program and neither in a loop.

Thanks for any hint in forward!

Edit:
I removed the calls of log_me() in both functions because it made no sense of course. Though difference to the original problem.

Well, I am not a perl expert. But in your log_me function you detect that the logfile cannot be opened so you call log_me again.

This second invocation of log_me will also detect that the logfile cannot be opened so it will call log_me again.

This third invocation of log_me will also detect that the logfile cannot be opened so it will call log_me again.

And so on.

Shouldn't actually be infinite though. Each iteration consumes a bit more stack. Try letting it run for a few minutes. You gotta blow the stack sooner or later.

1 Like

Yep saw those useless lines and removed them (edited 1st post), but there is no difference in the behaviour.

I'm concerned about this: $W_LOG

In shell, that would effectively become ${W}_LOG, not ${W_LOG}. Try bracketing it like the latter.

1 Like

@Corona: Changed it, still no difference.

I got it, print_err() has also a call to log_me(), which kept it in that loop. Didn't see that for more than an hour...

Thanks a lot all for having a look into. In the end the problem sits in front of the keyboard, as usual :smiley: :wink: