如何将Perl单行程转换为完整的脚本? [英] How can I convert Perl one-liners into complete scripts?
问题描述
我在网上找到很多Perl单线程。有时我想把这些单行转换成一个脚本,因为否则我会忘记一行的语法。
I find a lot of Perl one-liners online. Sometimes I want to convert these one-liners into a script, because otherwise I'll forget the syntax of the one-liner.
例如,我使用以下命令(来自 nagios.com ):
For example, I'm using the following command (from nagios.com):
tail -f /var/log/nagios/nagios.log | perl -pe 's/(\d+)/localtime($1)/e'
< d将其替换为这样的:
I'd to replace it with something like this:
tail -f /var/log/nagios/nagios.log | ~/bin/nagiostime.pl
但是,我无法想出最好的方法来快速抛出这个东西成一个脚本。有没有人有一个快速的方法把这些一行到一个Bash或Perl脚本?
However, I can't figure out the best way to quickly throw this stuff into a script. Does anyone have a quick way to throw these one-liners into a Bash or Perl script?
推荐答案
通过将它传递到生成Perl源代码的 B :: Deparse
编译器后端,可以将它们转换为完整的脚本:
You can convert any Perl one-liner into a full script by passing it through the B::Deparse
compiler backend that generates Perl source code:
perl -MO=Deparse -pe 's/(\d+)/localtime($1)/e'
输出:
LINE: while (defined($_ = <ARGV>)) {
s/(\d+)/localtime($1);/e;
}
continue {
print $_;
}
手动解码命令行标记的优点是正是Perl解释你的脚本的方式,所以没有猜测。 B :: Deparse
是一个核心模块,因此无需安装。
The advantage of this approach over decoding the command line flags manually is that this is exactly the way Perl interprets your script, so there is no guesswork. B::Deparse
is a core module, so there is nothing to install.
这篇关于如何将Perl单行程转换为完整的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!