English 中文(简体)
如何正确处理Perl的错误
原标题:How to correctly handle errors in Perl

我正试图在档案开启有错误时,让我的指挥迅速关闭。 我已经尝试了死亡的指挥,但是,在发出信号后,立即关闭指挥系统。 我想能够打印错误信息,等待几秒,然后关闭或退出。 现在,我可以坐下来,等待迅速结束。

open (LOG1, $file1) || (print "
# Error in opening $file1, make sure it exists! #
", sleep(3), exit);
最佳回答

页: 1 而是使用半殖民地。

问题回答

我认为,你正在使用Windows系统。 如果是的话,你可以尝试在你的文字中增加以下代码,显示: • 出版任何关键材料,以便在文字出走之前继续......。

END { system  pause  }

我怀疑sleep 产出缓冲没有变化,因此可以解释为什么你不看到任何印刷材料;你可以尝试:

open (LOG1, $file1) || ((print "
# Error in opening $file1, make sure it exists! #
"), $|++, sleep(3), exit);

无论如何,在我看来,最好确定一个<条码>dieAfterDelay的功能,如:

sub dieAfterDelay {
    my $msg = shift;
    my $delay = shift;
    my $errNo = shift;
    print $msg . "
", $|++;
    sleep($delay);
    exit($errNo);
}

您可以这样做:

open (LOG1, $file1) || dieAfterDelay("
# Error in opening $file1, make sure it exists! #
", 3, -1);




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签