如何美化 Perl 生成的 Perl 代码?
我有一个用 Perl 编写的测试生成器。 它生成连接到模拟器的测试。 这些测试本身是用 Perl 编写的,并通过其 API 连接到模拟器。 我希望生成的代码是人类可读的,这意味着我希望它能够正确缩进和格式化。 有没有好的办法呢?
详细信息如下,或者您可以跳至下面的实际问题。
这是一个示例:
my $basic = ABC
TRIGGER => DELAY(
NUM => 500,
),
)
BASIC
my $additional = STATE_IS(
STATE => DEF,
INDEX => 0,
),
ADDITIONAL
我希望在调用 &event
后延迟 500 秒执行命令 ABC
(单位现在不相关),索引0的状态是DEF。 有时我还想等待索引 1、2、3 等...
对于只有一个索引,我想在测试中看到这一点:
&event(
CMD => ABC
TRIGGER => DELAY(
NUM => 500,
TRIGGER => STATE_IS(
STATE => DEF,
INDEX => 0,
),
),
)
对于两个索引,我想看到:
&event(
CMD => ABC
TRIGGER => DELAY(
NUM => 500,
TRIGGER => STATE_IS(
STATE => DEF,
INDEX => 0,
TRIGGER => STATE_IS(
STATE => DEF,
INDEX => 1,
),
),
),
)
所以基本上我要添加一个块:
TRIGGER => STATE_IS(
STATE => DEF,
INDEX => 0,
),
对于每个索引,并且索引号发生变化。
这是我的做法:
for $i (0..$num_indeces) {
# update the index number
$additional =~ s/(INDEX\s*=>\s*)\d+,/$1 $i,/;
$basic =~ s/(
(\),\s*) # capture sequences of ),
+ # as many as possible
\)\s* # end with ) without a ,
} )/$additional $1/sx; # replace with the additional data
这是实际问题
这里的问题是代码的缩进效果很差。 我想通过这样的美化器运行生成的 $basic
:
&prettify($basic, "perl");
这将根据 Perl 的最佳实践很好地格式化它。 有什么好的方法可以做到这一点吗?
I have a test generator written in Perl. It generates tests that connect to a simulator. These tests are themselves written in Perl and connect to the simulator via its API. I would like the generated code to be human-readable, which means I'd like it to be properly indented and formatted. Is there a good way to do it?
Details follow, or you can skip to the actual question below.
This is an example:
my $basic = ABC
TRIGGER => DELAY(
NUM => 500,
),
)
BASIC
my $additional = STATE_IS(
STATE => DEF,
INDEX => 0,
),
ADDITIONAL
I'd like the command ABC
to be executed with a delay of 500 (units aren't relevant just now) after I call &event
, and the state of index 0 is DEF
. Sometimes I'll also want to wait for indeces 1, 2, 3 etc...
For only one index I'd like to see this in my test:
&event(
CMD => ABC
TRIGGER => DELAY(
NUM => 500,
TRIGGER => STATE_IS(
STATE => DEF,
INDEX => 0,
),
),
)
For two indeces I'd like to see:
&event(
CMD => ABC
TRIGGER => DELAY(
NUM => 500,
TRIGGER => STATE_IS(
STATE => DEF,
INDEX => 0,
TRIGGER => STATE_IS(
STATE => DEF,
INDEX => 1,
),
),
),
)
So basically I'm adding a block of:
TRIGGER => STATE_IS(
STATE => DEF,
INDEX => 0,
),
for each index, and the index number changes.
Here's how I'm doing it:
for $i (0..$num_indeces) {
# update the index number
$additional =~ s/(INDEX\s*=>\s*)\d+,/$1 $i,/;
$basic =~ s/(
(\),\s*) # capture sequences of ),
+ # as many as possible
\)\s* # end with ) without a ,
} )/$additional $1/sx; # replace with the additional data
Here's the actual question
The problem here is that the code comes out poorly indented. I'd like to run the resulting $basic
through a prettifier like this:
&prettify($basic, "perl");
Which would format it nicely according to Perl's best practices. Is there any good way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PerlTidy 使您的代码不仅整洁,而且非常漂亮。 您可以根据当地的编码标准轻松调整它。
PerlTidy makes your code not only tidy, but really beautiful. You can easily tweak it according to your local coding standards.
我用过这个:
I have used this: