这个问题看似简单,但我这几天一直在思考这个问题,找不到答案。
我有多级脚本架构(代码如下所示)
CallingScript.pl(包含顶层库并检查编译器错误)
do "IncludesConsumer.pm";
print "\n callingScript error : $@" if($@ || $!);
do "IncludesConsumer.pm";
print "\n callingScript error : $@" if($@);
do "IncludesConsumer.pm";
print "\n callingScript error : $@" if($@);
IncludesConsumer.pm(添加库INCLUDES.pm,自带函数)
do "INCLUDES.pm";
print "\nin IncludesConsumer";
INCLUDES.pm(一个地方有多个模块,充当库)
use Module;
print "\n in includes";
Module.pm(语法错误)
use strict;
sub MakeSyntaxError
{
print "\nerror in Module
}
1;
在概念上,一旦核心模块(例如Module.pm)可能包含语法错误。所以我需要在 CallingScript.pl 中捕获它们。即:我想在 CallingScript.pl
文件中捕获 Module.pm
(低级别)中的语法错误。
输出:
D:\Do_analysis>CallingScript.pl
in IncludesConsumer
in IncludesConsumer
in IncludesConsumer
为什么 CallingScript.pl 中没有捕获到编译器错误? 请倾诉你的想法。
谢谢!
最佳答案
五个错误,从导致您询问的问题的错误开始:
- 您没有处理来自某些
do
的错误。 - 您有时只检查
$!
。 $!
即使有错误也可以为真。只检查$!
是do
和$@
都是 false。- 除了一个您包含的文件外,其他所有文件都没有表示缺少错误 (
1;
)。您需要返回 true,以便do
返回 true,这样您就知道是否发生了错误。 您
使用
一个没有包
的文件。调用脚本.pl
:do "IncludesConsumer.pm" or die "CallingScript: ".($@ || $!); do "IncludesConsumer.pm" or die "CallingScript: ".($@ || $!); do "IncludesConsumer.pm" or die "CallingScript: ".($@ || $!);
IncludesConsumer.pm
(实际上不是“pm”):do "INCLUDES.pm" or die "IncludesConsumer: ".($@ || $!); print "\nin IncludesConsumer"; 1;
INCLUDES.pm
(实际上不是“pm”):use Module; print "\n in includes"; 1;
Module.pm
(有语法错误)package Module; sub MakeSyntaxError { 1;
确实没有理由以这种方式使用do
。这是一种非常糟糕的编程习惯。请避免使用它以支持模块。
关于perl - Perl 中的多级 'do'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16905152/