我有一个.pm模块,它有五个函数,每个函数返回0或1,我想添加另一个函数,在将来reouln 0或1(这些函数是测试函数1表示ok 0表示失败).
我希望我的脚本调用.pm模块上的每个函数,如果它返回0然后继续到下一个函数.如果它返回1那么它应该打印到LOG的东西并停止处理记录.
假设我将更新.pm模块并向其添加新功能,是否可以保持脚本代码不做任何更改?我每次将测试添加到.pm模块时都不想添加if条件?
解决方法
.pm模块应该提供一种检索要测试的函数列表的方法.
我认为最好的方法是它是一个子程序调用,但你也可以使用包中定义的变量(例如列表变量).
例子:
package MyModule; sub testable_functions { qw(fun1 fun2 fun3) } sub fun1 { ... } sub fun2 { ... } sub fun3 { ... } sub not_going_to_be_tested { ... }
要么:
package MyModule; our @testable_functions = qw(fun1 fun2 fun3);
在您的测试代码中:
my @funs_to_be_tested = MyModule->testable_functions; # or = @MyModule::testable_functions if you're using a list for my $fun (@funs_to_be_tested) { my $full_name = "MyModule::" . $fun; $full_name->() or die "function $full_name Failed\n"; }
如果你想获得幻想,你可以翻阅包装的符号表:
package MyModule; sub testable_functions { my @funs; for my $name ( keys %MyModule:: ) { next if $name eq "testable_functions"; # can add more conditions here my $full_name = "MyModule::".$name; next unless *{$full_name}{CODE}; # avoid non-subs push(@funs,$name); } return @funs; }