开发者

How to do escaping in this Perl code?

#!/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 d开发者_JAVA百科o this?


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


system can take @args (array form):

system ( 'dialog', @args );


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜