Regexp::Debugger Perl 模块似乎不能在另一个 Perl 模块中工作

Regexp::Debugger Perl module doesn't seem to work within another Perl Module

我正在编写一个 Perl 模块,其中涉及一些非常复杂的正则表达式,如果没有工具帮助我几乎无法调试这些正则表达式。我认为 Regexp::Debugger 模块将是完成这项工作的完美工具,但它似乎只能在 .pl 脚本中工作,并且似乎不能在 .pm 模块中工作。


例如,这个有效:

test.pl

use strict;
use warnings;
use Regexp::Debugger;

my $text = "text";
if ($text =~ /tex/) {
    print "Match!";
}

我得到了预期的调试功能。


但是第二次我引入了一个 Perl 模块,它不再起作用了:

test.pl

use strict;
use warnings;
use TestModule;

TestModule::func();

TestModule.pm

package TestModule;

use strict;
use warnings;
use Regexp::Debugger;

sub func {
    my $text = "text";
    if ($text =~ /tex/) {
        print "Match!";
    }
}

1;

尝试 运行 此代码会出现以下错误:

Can't use an undefined value as a HASH reference at Regexp/Debugger.pm line 160

简单地在 "test.pl" 文件中包含 Regexp::Debugger 也不起作用,因为它不会尝试调试包含的模块中的正则表达式。

我怎样才能让它工作,以便我可以在我正在处理的模块中调试正则表达式?


更新:

错误报告已提交,新版本 (v0.002001) 现在可以在 CPAN 上使用,可以按预期工作。 :)

这似乎是 Regexp::Debugger 在调用范围中检查词法提示的方式中的错误,但我不确定如何修复它。作为参考,https://metacpan.org/source/DCONWAY/Regexp-Debugger-0.002000/lib/Regexp/Debugger.pm#L160 是:

my $lexical_scope = (caller 1)[10]{'Regexp::Debugger::lexical_scope'};

也许它应该 ((caller 1)[10] || {})->{'Regexp::Debugger::lexical_scope'} 而不是?


无论如何,我能够重现您的问题并且找到了解决方法。在 test.pl 中,执​​行:

use strict;
use warnings;
no Regexp::Debugger;
use TestModule;

即在 use TestModule 之前加载 Regexp::Debugger,但使用 no 而不是 use 以避免为主程序激活它。

通过此更改,我得到:

TestModule.pm did not return a true value at test.pl line 4.
BEGIN failed--compilation aborted at test.pl line 4.

... 这仍然是一个错误,但这是一个很好的错误。这意味着 Regexp::Debugger.

没有问题

如果我在 TestModule.pm 的末尾添加一行 1,一切正常。


更新: OP reported the problem and it was fixed Regexp::Debugger.

版本 0.002001