如何在这段 Perl 代码中进行转义?
#!/usr/bin/perl
use warnings;
system ("dialog --menu Customize 10 70 50 'Flush rules' 'Clear all the rules' 'Show rules' 'Shows the current rules' 2> /tmp/tmp.txt ")
我想以更易读的形式编写上面的代码,如下所示
#!/usr/bin/perl
use warnings;
system ("dialog --menu Customize 10 70 50
'Flush rules' 'Clear all the rules'
'Show rules' 'Shows the current rules'
'more options' '........' 2> /tmp/tmp.txt ")
我该怎么做?
#!/usr/bin/perl
use warnings;
system ("dialog --menu Customize 10 70 50 'Flush rules' 'Clear all the rules' 'Show rules' 'Shows the current rules' 2> /tmp/tmp.txt ")
I want to write the above code in a more readable form like this
#!/usr/bin/perl
use warnings;
system ("dialog --menu Customize 10 70 50
'Flush rules' 'Clear all the rules'
'Show rules' 'Shows the current rules'
'more options' '........' 2> /tmp/tmp.txt ")
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Perl 提供了一个字符串连接运算符,您可以使用它来构建大字符串:
Perl provides a string concatentation operator that you can use to build up big strings:
system
可以采用@args
(数组形式):system
can take@args
(array form):哎呀,tadmc 很快。是的,使用
.
连接命令。我建议您在单独的字符串中创建命令,然后执行该命令。我还建议使用
qq
命令进行引用。这样,您就不必担心单引号和双引号:使用
qq
让您不必担心我是否需要使用双引号来允许变量插值或单引号,或者有逃避引号。例如,我能够混合使用双引号和单引号,并且我可以使用 Perl 变量,而不必担心是否必须从单引号更改为双引号。例如,我使用/tmp/temp.$$
。$$
是进程 ID,因此如果执行该命令两次,则会使用两个不同的临时文件。通过为我的命令创建一个单独的变量,我现在可以稍后使用它——就像我的系统命令中出现错误一样。
顺便说一句,您应该始终检查
system
命令的返回。如果由于某种原因您无法执行系统命令,您很可能想出错或至少记下该问题。问题之一是系统命令的输出与大多数 Perl 函数相反。在大多数 Perl 函数中,返回零表示失败,返回非零表示成功。然而,
system
函数却恰恰相反。零意味着成功,非零意味着失败。这可能会导致奇怪的 if 结构:
这看起来像是我在说,如果我的系统命令成功,我应该死,但它的真正含义与此相同:
这在语法上更有意义。
Dang, tadmc is fast. Yes, use the
.
concatenation command.I would recommend that you create your command in a separate string and then execute that. I also recommend using the
qq
command to do quoting. That way, you don't have to worry about single vs. double quotes:Using
qq
allows you not to worry about whether I need to use double quotes to allow for variable interpolation or single quotes, or having to escape quotes. For example, I was able to mix double and single quotes, and I can use Perl variables without worrying whether or not I have to change from single to double quotes. For example, I use/tmp/temp.$$
. The$$
is the process ID, so if this command is executed twice, there are two different temp files used.By creating a separate variable for my command, I can now use it later -- like if there was an error in my system command.
By the way, you should always check the return of your
system
command. There's probably a good chance that if for some reason you can't execute your system command, you probably want to error out or at least note the issue.One of the problems is that the output of the system command is the opposite of most Perl functions. In most Perl functions, a return of zero indicates a failure while a return of non-zero indicates success. However, the
system
function is the exact opposite. Zero means success, non-Zero means failure.That can lead to strange if constructs:
This looks like I'm saying that if my system command succeeds, I should die, but it really means the same as this:
That syntactically makes a lot more sense.