perl - Perl 中的多级 'do'?

标签 perl perl-module

这个问题看似简单,但我这几天一直在思考这个问题,找不到答案。

我有多级脚本架构(代码如下所示)

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/

相关文章:

bash - 如何用多行搜索和替换多行

linux - 安装 HTML::TreeBuilder::XPath 失败

linux - 如何在 Ubuntu 上安装 Net::SFTP 模块?

perl - perl(语法错误)-全局符号 “@arr1”需要明确的软件包名称

perl - 在perl中声明全局变量

perl - 如何在编译时从外部文件导入 Perl 代码?

perl - Cwd.c : loadable library and perl binaries are mismatched (got handshake key 0xdb00080, 需要 0xde00080)

Perl 包 : how to import classes into the 'use' r's namespace?

regex - Perl Regex - 打印匹配的条件正则表达式

perl - 我如何最好地处理 Perl/Tk 所需的补丁?