Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devel::Declare removes line from script

Tags:

perl

declare

I am trying to learn Devel::Declare so as to attempt to reimplement something like PDL::NiceSlice without source filters. I was getting somewhere when I noticed that it was removing the next line from my script. To illustrate I have made this minimal example wherein one can use the comment keyword to remove the entire line from the code, allowing a compile even though barewords abound on that line.

#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

and

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

use strict;
use warnings;

use Comment;

comment stuff;

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

yields only

Print 2

what am I missing?

P.S. I will probably have a few more questions on D::D coming up if I should figure this one out, so thanks in advance!

like image 213
Joel Berger Avatar asked Oct 11 '22 07:10

Joel Berger


1 Answers

Ok so I got it. Using perl -MO=Deparse test.pl you get:

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

which tells me that if forces the comment function to be called. After some experimentation I found that I could just set the output to call comment() explicitly so that it doesn't try to call comment on whatever is next.

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

so that the deparse is:

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

and the proper output too.

like image 194
Joel Berger Avatar answered Oct 18 '22 05:10

Joel Berger