Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
构建perl模块_Perl_Design Patterns - Fatal编程技术网

构建perl模块

构建perl模块,perl,design-patterns,Perl,Design Patterns,我有一个.pm模块,它有五个函数,每个函数返回0或1,我想添加另一个函数,将来返回0或1(这些函数是测试函数,1表示ok,0表示失败) 我想从脚本中调用那些基于.pm模块的函数 我希望脚本调用.pm模块上的每个函数(如果返回0),然后继续下一个函数。如果它返回1,那么它应该打印一些东西来记录并停止处理记录 假设我将更新.pm模块并向其添加新函数,是否可以在不做任何更改的情况下保留脚本代码?我不想在每次向.pm模块添加测试时都添加if条件?该.pm模块应该提供一种检索要测试的函数列表的方法 我认为

我有一个.pm模块,它有五个函数,每个函数返回0或1,我想添加另一个函数,将来返回0或1(这些函数是测试函数,1表示ok,0表示失败)

我想从脚本中调用那些基于.pm模块的函数

我希望脚本调用.pm模块上的每个函数(如果返回0),然后继续下一个函数。如果它返回1,那么它应该打印一些东西来记录并停止处理记录


假设我将更新.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 { ... }
或:

在测试代码中:

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;
}

尽管如此,合同是一样的:
MyModule
为测试代码提供了一种方法,以获得应该测试的功能列表。

这听起来像是一个简单的测试框架/工具,您是否已经看过了?该模块似乎正是您想要做的,但通过稍后使用它,您可以转到Test::More并获取其他功能。请参阅此线程,您想获取模块中所有子例程的名称吗?Hi IF$full_name->(@params)获取@params作为非空数组为什么我不能获取真实数组function@sam-我不确定你想说/问什么
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;
}