Devel :: Declare从脚本中删除行

Joe*_*ger 5 perl declare

我试图学习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
Run Code Online (Sandbox Code Playgroud)

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

use strict;
use warnings;

use Comment;

comment stuff;

print "Print 1\n";
print "Print 2\n";
Run Code Online (Sandbox Code Playgroud)

只收益率

Print 2
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

PS D::D如果我想把这个问题拿出来,我可能会有更多的问题,所以提前谢谢!

Joe*_*ger 4

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

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

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

sub parser {
  Devel::Declare::set_linestr("comment();");
}
Run Code Online (Sandbox Code Playgroud)

这样,deparse 就是:

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

以及正确的输出。