PPI - 修剪 PPI::Token::Whitespace - 问题
当我使用 "$module->prune( 'PPI::Token::Whitespace' );" 时并将结果保存在 $file 中是否有一种简单的方法可以返回到已保存代码的工作代码?我尝试了“Perl::Tidy”,之后看起来好多了,但 id 并不能解决所有问题。
#!/usr/bin/env perl
use warnings;
use 5.012;
use PPI;
my $file = 'my_file.pm';
my $module = PPI::Document->new( $file );
$module->prune( 'PPI::Token::Pod' );
$module->prune( 'PPI::Token::Comment' );
$module->prune( 'PPI::Token::Whitespace' );
# ...
# ...
$module->save( $file ) or die $!;
编辑:
我不再能够重建我最初拥有的代码。启用 prune-whitespace 后,我可以使用类似的东西,
$a = $module->find( sub {
$_[1]->isa('PPI::Statement') and
$_[1]->content eq q(if($@){$have_Term_ReadKey=0;$have_Term_Size=1;eval'require "Term/Size.pm"';if($@){$have_Term_Size=0;}})
});
而不是
$a = $module->find( sub {
$_[1]->isa('PPI::Statement') and
$_[1]->schild(0)->content eq q(if) and
$_[1]->schild(1)->isa('PPI::Something') and
...
...
});
找到一个点来附加一些东西。 但重试后我认为它无法工作(除了事实之外,我无法在没有空格的情况下恢复代码)。
When I use "$module->prune( 'PPI::Token::Whitespace' );" and save the results in $file is there an ease way back to working code for the saved code? I tried "Perl::Tidy" and it looks much better after this but id does not fix all.
#!/usr/bin/env perl
use warnings;
use 5.012;
use PPI;
my $file = 'my_file.pm';
my $module = PPI::Document->new( $file );
$module->prune( 'PPI::Token::Pod' );
$module->prune( 'PPI::Token::Comment' );
$module->prune( 'PPI::Token::Whitespace' );
# ...
# ...
$module->save( $file ) or die $!;
edit:
I am no longer able to reconstruct my code that I had in the first place. With prune-whitespace enabled I could use something like this
$a = $module->find( sub {
$_[1]->isa('PPI::Statement') and
$_[1]->content eq q(if($@){$have_Term_ReadKey=0;$have_Term_Size=1;eval'require "Term/Size.pm"';if($@){$have_Term_Size=0;}})
});
instead of
$a = $module->find( sub {
$_[1]->isa('PPI::Statement') and
$_[1]->schild(0)->content eq q(if) and
$_[1]->schild(1)->isa('PPI::Something') and
...
...
});
to find a point to append something.
But know after retrying I think it can not work (apart from the fact, that I can not restore the code without withespaces).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些空格很重要,通常,如果删除它,您将破坏代码:
该程序的输出是:
哎呀。
Perl::Tidy
对此无能为力。删除顶级空白是安全的(我认为 - 无法想到反例)
枚举所有其他规则(例如“两个
PPI::Token: 之间的空白: :Word
元素不能安全地删除”)听起来像是一个难题。Some whitespace is significant and in general you will break your code if you remove it:
The output of this program is:
Oops. There's nothing
Perl::Tidy
can do about that.It would be safe (I think -- can't think of a counterexample) to remove the top-level whitespace
Enumerating all the other rules (like "whitespace between two
PPI::Token::Word
elements is not safe to remove") sounds like a hard problem, though.