我有以下类型的Perl的问题:如何使用Perl异常?
$object1 = $ABC->Find('test1');
然后,我想打电话给一个叫CheckResult
子程序Report.pm
:
$Report->CheckResult($object, "Finding the value");
在另一种情况下,我要报案,如果一个特定的命令是执行的,所以我做这样的事情:
$Report->CheckResult($ABC->Command(100,100), "Performing the command");
现在Report.pm
:
sub CheckResult {
my ($result, $information) = @_;
# Now, I need something like this
if ($result->isa('MyException')) {
# Some code to create the report
}
}
如何使用异常类以及如何检查是否引发异常,如果有,请执行必要的任务?
编辑:
截至目前,我有什么样的模块:
package MyExceptions;
use strict;
use warnings;
use Exception::Class (
'MyExceptions',
'MyExceptions::RegionNotFound' => {isa => 'MyExceptions'},
'MyExceptions::CommandNotExecuted' => {isa => 'MyExceptions'}
);
其他模块:
package ReportGenerator;
use strict;
use warnings;
sub CheckResult {
my ($result, $info) = @_;
# Here is want to check of $result and throw an exception of the kind
# MyExceptions::RegionNotFound->throw(error => 'bad number');
# I'm not sure how to do this
}
1;
用户将反过来脚本像这样:
$Report->CheckResult($ABC->Command(100,100), "Tapping Home");
有人可以帮忙吗?对不起我的无知,我没有做过任何例外。
我觉得异常::类将满足我的需要,可能会有人告诉我如何如果你的代码需要一个模块来使用它 – MarsMax 2011-04-20 09:34:25