k*******3 发帖数: 1909 | 1 ==========================TryTiny.pl CODE BEGIN====================
#!/usr/bin/perl
use warnings;
use strict;
eval
{
my $i = $foo;
};
if ($@)
{
print "There is an error caught!!!!";
}
==============================CODE END=============================
想用上面代码测试perl错误捕捉,bash下运行显示
Global symbol "$foo" requires explicit package name at TryTiny.pl line 8.
Execution of TryTiny.pl aborted due to compilation errors.
为什么没有捕捉到错误运行print "There is an error caught!!!!";这句呢?
Thanks! |
j*****a 发帖数: 436 | 2 #!/usr/bin/perl
use warnings;
use strict;
eval 'my $i = $foo;';
if ($@)
{
print "There is an error caught!!!!";
} |
k*******3 发帖数: 1909 | 3 为什么一定要单引号呢?
eval后面不是也可以用block吗?
下面链接说两种方法应该都可以啊
http://bbs.chinaunix.net/archiver/tid-484028.html
[code]
eval { 程序段 };
if ($@) { DOIT}
[/code]
[code]
eval ( 作为执行的字串 );
if ($@) { DOIT}
[/code]
【在 j*****a 的大作中提到】 : #!/usr/bin/perl : use warnings; : use strict; : eval 'my $i = $foo;'; : if ($@) : { : print "There is an error caught!!!!"; : }
|
j*****a 发帖数: 436 | 4 eval block is used for run-time error, but your code is a compiler error. |
k*******3 发帖数: 1909 | 5 Many thanks!
那eval 字符串能catch run time error吗? |
j*****a 发帖数: 436 | |