2

知道我想做的这种事情曾经在 5.8 中工作过。难道我做错了什么?有没有办法在 Perl 5.10 中回到那里?

这是模块:

package TableMod;
use base qw<Exporter>;
our @EXPORT_OK = qw<mod_table>;

use Data::Dumper;
sub mod_table (\%@) { print Dumper( @_ ); }

1;

这是脚本:

use strict;
use warnings;
use Data::Dumper;
use Test::More tests => 4;

sub mod_table_here (\%@) { 
    print Dumper( @_ );
}

use_ok( 'TableMod', 'mod_table' );
can_ok( __PACKAGE__, 'mod_table' );
is( prototype( \&mod_table_here ), '\\%@'
  , q/prototype( \&mod_table_here ) = '\%@'/ 
  );
is( prototype( \&mod_table ), prototype( \&mod_table_here )
   , 'prototypes ARE the SAME!' 
   );
my %table = qw<One 1>;
mod_table_here %table => ( 1, 2, 3, 4 );
#mod_table %table => ( 1, 2, 3, 4 );
mod_table( %table, 1, 2, 3, 4 );

我所要做的就是取消最后一行的注释,我得到:

Useless use of modulus (%) in void context at - line 17.
Useless use of a constant in void context at - line 17.
Useless use of a constant in void context at - line 17.
Useless use of a constant in void context at - line 17.
Bareword "mod_table" not allowed while "strict subs" in use at - line 17.

它不抱怨本地潜艇,但对进口潜艇失去了理智。最重要的是,尽管测试告诉我我已经导入了“mod_table”,但现在严格感到困惑的是它是一个裸词!

不仅如此,尽管测试告诉我原型是相同的,但我不能%table作为 hashref 传递给导入的 sub 。即使我使用最后一行所示的常规语法也不行。

我得到的是:

1..4
ok 1 - use TableMod;
ok 2 - main->can('mod_table')
ok 3 - prototype( \&mod_table_here ) = '\%@'
ok 4 - prototypes ARE the SAME!
$VAR1 = {
          'One' => '1'
        };
$VAR2 = 1;
$VAR3 = 2;
$VAR4 = 3;
$VAR5 = 4;
$VAR1 = 'One';
$VAR2 = '1';
$VAR3 = 1;
$VAR4 = 2;
$VAR5 = 3;
$VAR6 = 4;
4

2 回答 2

10

这是因为use_ok在运行时被调用。如果您添加以下内容,则一切正常:

 use TableMod 'mod_table';

我通常只保留一个带有use_okin 的测试文件(通常是00-load.t00-use.t)。我认为 Ovid 可能已经写了一篇关于这是一个好的做法的博客文章?

更新:找到我所指的 Ovid 的博客文章。

/I3az/

于 2009-12-30T23:09:10.077 回答
7

这是预期的结果。use_ok 调用是在运行时调用的,因此 mod_table 子程序只有在编译过程中遇到对其的“调用”才会编译和导入,因此对 mod_table 的“调用”被解释为非法裸字。

此代码在 5.8 和 5.10 上产生相同的警告/错误。

perl -e'use strict; use warnings; my %table; mod_table %table => (1,2,3,4)'

因为缺少编译时导入可能会以这样的方式影响已编译的测试代码,所以use在所有测试中使用而不是 use_ok 是一个好主意,除了专门用于执行 use_ok 的测试(可能使用BAIL_OUT)。(将 use_ok 放在 BEGIN 块中可以缓解这类问题,但可能会导致其他问题,因此不是一个好主意。)

于 2009-12-31T01:26:50.440 回答