PPI - 修剪 PPI::Token::Whitespace - 问题

发布于 2024-10-03 05:56:14 字数 1068 浏览 0 评论 0原文

当我使用 "$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-10-10 05:56:15

一些空格很重要,通常,如果删除它,您将破坏代码:

use PPI;
$document = PPI::Document->new(\'sub sq{$_[0]**2}');
$document->prune('PPI::Token::Whitespace');
print $document->serialize;

该程序的输出是:

subsq{$_[0]**2}

哎呀。 Perl::Tidy对此无能为力。


删除顶级空白是安全的(我认为 - 无法想到反例)

# just prune top-level whitespace
$document->prune(  sub { $_[1]->parent->isa('PPI::Document') 
                         and $_[1]->isa('PPI::Token::Whitespace') } );

枚举所有其他规则(例如“两个 PPI::Token: 之间的空白: :Word 元素不能安全地删除”)听起来像是一个难题。

Some whitespace is significant and in general you will break your code if you remove it:

use PPI;
$document = PPI::Document->new(\'sub sq{$_[0]**2}');
$document->prune('PPI::Token::Whitespace');
print $document->serialize;

The output of this program is:

subsq{$_[0]**2}

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

# just prune top-level whitespace
$document->prune(  sub { $_[1]->parent->isa('PPI::Document') 
                         and $_[1]->isa('PPI::Token::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文