【问题标题】:Devel::Declare removes line from scriptDevel::Declare 从脚本中删除行
【发布时间】:2011-08-08 20:58:56
【问题描述】:

我正在尝试学习Devel::Declare,以便尝试在没有源过滤器的情况下重新实现PDL::NiceSlice 之类的东西。当我注意到它正在从我的脚本中删除下一行时,我正在到达某个地方。为了说明这一点,我做了一个最小的示例,其中可以使用 comment 关键字从代码中删除整行,即使该行上有很多裸词也允许编译。

#Comment.pm
package Comment;

use strict;
use warnings;

use Devel::Declare ();

sub import {
  my $class = shift;
  my $caller = caller;

  Devel::Declare->setup_for(
      $caller,
      { comment => { const => \&parser } }
  );
  no strict 'refs';
  *{$caller.'::comment'} = sub {};

}

sub parser {
  #my $linestr = Devel::Declare::get_linestr;
  #print $linestr;

  Devel::Declare::set_linestr("");
}

1

#!/usr/bin/env perl
#test.pl

use strict;
use warnings;

use Comment;

comment stuff;

print "Print 1\n";
print "Print 2\n";

只产生

Print 2

我错过了什么?

附:如果我应该解决这个问题,我可能会在D::D 上提出更多问题,所以提前谢谢!

【问题讨论】:

    标签: perl declare


    【解决方案1】:

    好的,我明白了。使用perl -MO=Deparse test.pl 你会得到:

    use Comment;
    use warnings;
    use strict 'refs';
    comment("Print 1\n");
    print "Print 2\n";
    test.pl syntax OK
    

    这告诉我 if 强制调用 comment 函数。经过一些实验,我发现我可以将输出设置为显式调用comment(),这样它就不会尝试在接下来的任何事情上调用comment

    sub parser {
      Devel::Declare::set_linestr("comment();");
    }
    

    所以deparse是:

    use Comment;
    use warnings;
    use strict 'refs';
    comment();
    print "Print 1\n";
    print "Print 2\n";
    test.pl syntax OK
    

    还有正确的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 2018-06-23
      相关资源
      最近更新 更多