Perl脚本停留在'子例程上的深度递归'

时间:2016-12-12 09:43:04

标签: perl

我有perl文件build_ios.pl,有10-20种方法。

当我在构建计算机/usr/bin/perl /Users/snaggs/scripts/build_ios.pl上运行时,出现错误:

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.

以下是错误指向的相关方法:

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'});
}

脚本卡在这个错误上。我的构建计算机使用版本v5.18.2

在本地我运行相同的代码,一切正常v5.16.0

我的代码有什么问题以及如何摆脱这个问题?

[编辑]

我注意到af_exit次调用writeLogAndPrintwriteLogAndPrint可以再次调用af_exit,这会导致无限循环

谢谢,

2 个答案:

答案 0 :(得分:4)

在您的writeLogAndPrint sub中,您正在尝试打开日志文件...如果失败,请拨打再次调用af_exit的{​​{1}} ...有循环。

检查要在哪里打开日志文件的writeLogAndPrint变量。

答案 1 :(得分:0)

添加辅助函数以打破循环再生。

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( @_ );
}

有关递归的更多信息,请参阅A Look at Recursion