如何在这段 Perl 代码中进行转义?

发布于 2024-12-02 00:24:57 字数 462 浏览 0 评论 0原文

#!/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 技术交流群。

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

发布评论

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

评论(3

泪眸﹌ 2024-12-09 00:24:57

Perl 提供了一个字符串连接运算符,您可以使用它来构建大字符串:

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 ");

Perl provides a string concatentation operator that you can use to build up big strings:

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 ");
苦笑流年记忆 2024-12-09 00:24:57

system 可以采用 @args (数组形式):

system ( 'dialog', @args );

system can take @args (array form):

system ( 'dialog', @args );
平生欢 2024-12-09 00:24:57
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 ");

哎呀,tadmc 很快。是的,使用 . 连接命令。

我建议您在单独的字符串中创建命令,然后执行该命令。我还建议使用qq命令进行引用。这样,您就不必担心单引号和双引号:

my $command = qq(dialog --menu Customize 10 70 50 )
   . qq("Flush rules" 'Clear all the rules' )
   . qq('Show rules' 'Shows the current rules' )
   . qq'more options' '........' 2> /tmp/temp.$ );

my $error = system $command;

使用 qq 让您不必担心我是否需要使用双引号来允许变量插值或单引号,或者有逃避引号。例如,我能够混合使用双引号和单引号,并且我可以使用 Perl 变量,而不必担心是否必须从单引号更改为双引号。例如,我使用 /tmp/temp.$$$$ 是进程 ID,因此如果执行该命令两次,则会使用两个不同的临时文件。

通过为我的命令创建一个单独的变量,我现在可以稍后使用它——就像我的系统命令中出现错误一样。

顺便说一句,您应该始终检查 system 命令的返回。如果由于某种原因您无法执行系统命令,您很可能想出错或至少记下该问题。

问题之一是系统命令的输出与大多数 Perl 函数相反。在大多数 Perl 函数中,返回零表示失败,返回非零表示成功。然而,system 函数却恰恰相反。零意味着成功,非零意味着失败。

这可能会导致奇怪的 if 结构:

if (system $command) {
    die qq(Can't execute command "$command"\n);
};

这看起来像是我在说,如果我的系统命令成功,我应该死,但它的真正含义与此相同:

my $error = system $command;

if ($error) {
   die qq(Can't execute command "$command"\n);
}

这在语法上更有意义。

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 ");

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:

my $command = qq(dialog --menu Customize 10 70 50 )
   . qq("Flush rules" 'Clear all the rules' )
   . qq('Show rules' 'Shows the current rules' )
   . qq'more options' '........' 2> /tmp/temp.$ );

my $error = system $command;

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:

if (system $command) {
    die qq(Can't execute command "$command"\n);
};

This looks like I'm saying that if my system command succeeds, I should die, but it really means the same as this:

my $error = system $command;

if ($error) {
   die qq(Can't execute command "$command"\n);
}

That syntactically makes a lot more sense.

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