Perl Declare从脚本中删除行

Perl Declare从脚本中删除行,perl,declare,Perl,Declare,我正在努力学习,以便尝试在没有源代码过滤器的情况下重新实现类似PDL::NiceSlice。当我注意到它正在从我的脚本中删除下一行时,我正在取得进展。为了说明这一点,我举了一个最小的例子,其中可以使用comment关键字从代码中删除整行内容,即使该行中有大量的无用词,也可以进行编译 #Comment.pm package Comment; use strict; use warnings; use Devel::Declare (); sub import { my $class =

我正在努力学习,以便尝试在没有源代码过滤器的情况下重新实现类似
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

只收益

Print 2
我错过了什么


另外,如果我想弄清楚这一点,我可能会有更多关于
D::D
的问题,所以提前谢谢

好的,我知道了。使用
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
以及适当的输出

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