snaggs
snaggs

Reputation: 5713

Perl script stucks on 'Deep recursion on subroutine'

I have perl file build_ios.pl with 10-20 methods.

When I run on my build machine /usr/bin/perl /Users/snaggs/scripts/build_ios.pl, I get an error:

Deep recursion on subroutine "main::writeLogAndPrint" at /Users/snaggs/scripts/build_ios.pl line 179.
Deep recursion on subroutine "main::af_exit" at /Users/snaggs/scripts/build_ios.pl line 167.

Here are relevant methods that error point to:

sub writeLogAndPrint
{
    my $command = shift || "";

    open(my $fh, '>>', $VERBOSE_LOG_FILE_ABS) or  # line 167
        af_exit(%ERROR_CODE_12);
        #die "Can't open the file: $!";
    print $fh getTimeLoggerHelper() . ": " . $command."\n";
    close $fh;
    print $command . "\n";
}


sub af_exit
{
    my %_error = @_;
    writeLogAndPrint($_error{'id'});  # line 179
    exit($_error{'id'});
}

And script stucks on this errors. My build machine uses version v5.18.2.

Locally I run same code and everything works fine v5.16.0.

Whats wrong with my code and how to get rid of this?

[EDIT]

I noticed that af_exit calls writeLogAndPrint and writeLogAndPrint can call af_exit again that can lead to infinite loop

Thanks,

Upvotes: 1

Views: 3263

Answers (2)

shawnhcorey
shawnhcorey

Reputation: 3601

Add a helper function to break the cyclic resursion.

sub _af_exit
{
    my %_error = @_;
    exit($_error{'id'});
}

sub writeLogAndPrint
{
    my $command = shift || "";

    open(my $fh, '>>', $VERBOSE_LOG_FILE_ABS) or
        _af_exit(%ERROR_CODE_12);
        #die "Can't open the file: $!";
    print $fh getTimeLoggerHelper() . ": " . $command."\n";
    close $fh;
    print $command . "\n";
}

sub af_exit
{
    my %_error = @_;
    writeLogAndPrint($_error{'id'});
    _af_exit( @_ );
}

For more information on recursion, see A Look at Recursion.

Upvotes: 0

dgw
dgw

Reputation: 13666

Within your writeLogAndPrint sub your are trying to open a logfile ... and if that fails you call af_exit which calls writeLogAndPrint again ... There is the loop.

Check your $VERBOSE_LOG_FILE_ABS variable where you want to open the logfile.

Upvotes: 4

Related Questions