Perl system exec quotation marks

Source: Internet
Author: User

'), System, and exec in Perl

In Perl, the check mark (''), system, and exec are used to execute the command order. This article will show us how they are used, approached, and differentiated.

 

I. utilization methods

1. check mark ('')

First of all, we have a number order Input operator, also known as the Anti-hook operator, because it looks like this:

$ Info = 'finger $ user ';

A variable is used to replace the variable at the beginning of a string caused by a backslash (ACCENT), just like a string caused by double quotation marks. The obtained results are then viewed as a command line by the system, and the output of the command line is a pseudo text value. (This is a module that is similar to a Unix shell .) In the scalar condition, return a string that includes all output data. In the list, a column value is returned, and each row outputs a value. (You can set $/through the process to take advantage of the line escape operator of different points .)

This command is executed every time the pseudo text is taken into account. The number status value of this order is retained at $? (For details, see Chapter 1. Get $? $ Child_error ). What is different from the CSH version of this command is that no conversion is made to the returned data-the line break is still a line break. What is different from all shell statements is that the single quotes in Perl do not bury the variables on the number line to stop replacement. To notify shell of $, you must use a backslash to bury it. In the above finger example, $ user is replaced by Perl instead of shell. (For details about shell disposal, refer to Chapter 23rd for details about Ping .)

The general situation of the anti-check mark is QX // (meaning "caused by the Implementation"), but the effect of this operator is the same as that of the popular anti-check mark. You only need to select your character. One thing is similar to the pseudo function: If you have chosen a single quotation mark as your separator, the number line will not be replaced by double quotation marks;

$ Perl_info = qx (PS $); # Here $ is the Perl processing tool $ perl_info = QX 'ps $ '; # Here $ is the shell processing tool

The following is an example:

In the author's F disk, there is a perl file F: // demo3.pls. Its role is to be misappropriated by another French F: // demo1.pls, And then F: // demo3.pls read data from log3.log.

 

Demo3.pls

#! /Usr/bin/perl-W

 

Use strict;

Use warnings;

 

Unless (open (file_h ,"

Print "can not open the file ";

}

 

My @ STR =;

 

My $ COUNT = @ STR;

Close (file_h );

 

For (my $ I = 0; $ I <$ count; $ I ++ ){

Print "$ STR [$ I]";

}

Demo. pls

#! /Usr/bin/perl-W

My @ STR = QX/perl F: // demo3.pls/; # 'perl F: // demo3.pls 'returns the same result #

Print "@ Str ";

Log3.log

First of all, we have a number order Input operator, also known as the Anti-hook operator, because it looks like this:

 

$ Info = 'finger $ user ';

 

A variable is used to replace the variable at the beginning of a string caused by a backslash (ACCENT), just like a string caused by double quotation marks. The obtained results are then viewed as a command line by the system, and the output of the command line is a pseudo text value. (This is a module that is similar to a Unix shell .) In the scalar condition, return a string that includes all output data. In the list, a column value is returned, and each row outputs a value. (You can set $/through the process to take advantage of the line escape operator of different points .)

 

Code interpretation: Demo is implemented. when PLS, my @ STR = QX/perl F: // demo3.pls/; this sentence will be misappropriated by the operating system and demo3.pls will be started. Then demo3.pls will read the data in log3.log.

 

Our primary concern is my @ STR = QX/perl F: // demo3.pls/; the returned value @ STR in this sentence. We all know that @ STR is a list, returns the print value in demo3.pls with the reversed check mark. Although we can also use scalar conditions, for example, my $ STR = QX/perl F: // demo3.pls /; this can also take out all the data at a time.

 

Conclusion: The Reverse Mark returns the print value returned by the number line. For more information, see the above statement.

 

2. System

L system pathname list

L system list

This function executes the program in any system for you and returns the exit status of the program-not its output. To capture the output on the number line, you should use the reverse check mark QX //. The operation of the system function is very similar to exec, but the system does not first make a fork, and then expect the implementation of the French compile after exec. That is to say, it runs the program for you and returns it after it completes, while exec replaces the program you run with the new one, so that it never returns if it replaces the music.

The processing of parameters varies with the number of parameters, just as in exec, bao Luo determines whether to use shell and whether other pathnames are declared using other functions.

Because of system and inverse mark obstruction SIGINT and sigquit, to is to those who are such running French send one of these signals (like through a control-C) will not interrupt your main French. But the other program you run simply receives this signal. Check the return value of the system to determine whether the program you run Exits normally.

@ ARGs = ("command", "arg1", "arg2 ");

System (@ ARGs) = 0

Or die "system @ ARGs failed: $? "

The returned value is the same exit condition as that returned by the function through the wait (2) system. In traditional semantics, to obtain the actual exit value, divide it by 256 and shift it to the right by eight digits. This is because there are some other tools in the Lower 8 bits. (In reality, there are two other tools .) The minimum seven mark to kill the process of the signal number (if any), and the eighth mark whether the process dumped the focus. You can go through the process $? ($ Child_error) to check all the failure of the approximate, Bao Luo signal and focus dumping:

$ Exit_value = $? > 8;

$ Exit_value = $? & Amp; 127; # approximately 0x7f, 0177, 0b0111161111

$ Dumped_core = $? & Amp; 128; # approximately 0x80,020 0, 0b1000_0000

If the French is through the process system shell (Note: Definition of/bin/sh probably Ren An in your platform on the meaning of the tool, but not the shells that users can use at a certain time .) This is probably because you only have one parameter and the parameter contains shell metacharacters. Therefore, the return code is affected by the strangeness and efficacy of the shell. In other words, in this environment, you may not be able to obtain the specific information of our front morphology.

 

3. Exec

O exec pathname list

O exec list

The exec function is executed in order and an external number order is executed and never returned !!! If you wish to exercise restraint after the order is withdrawn, you should use system. The exec function fails and returns false only when the number order does not exist and the number order is directly implemented but not implemented through the shell (which will be discussed below) of your system.

If there is only one scalar parameter, exec checks whether the parameter is a shell metacharacter. If the metacharacter is found, all the parameters it represents are reported to the system's scale number line interpreter (in UNIX, It is/bin/sh ). If there is no such metacharacters, then the parameter is split into words and then directly implemented, out of obedience to the amount of thinking, such as to bypass all shell disposal of the load. And if the French do not exit, it also gives you more control over the rules of errors.

If there is more than one parameter in the list, or if the list is an array that spans a value, the system shell will never be used. This also bypasses shell's disposal of the order. Whether or not to present metacharacters in the parameter does not affect the trigger feature of this list. This is also a good way to compare the strength of the program, because it will not reveal itself in the latent shell escape.

The following example replaces the currently running Perl program with the echo program, and then prints the current parameter list:

Exec 'echo ', 'Your arguments are:', @ argv;

The following example shows that you can exec a pipeline, not just a French program:

Exec "Sort $ OUTFILE | uniq"

Or die "can't do sort/uniq: $! /N ";

In all cases, exec never returns -- even if it returns, it always returns false, and you should check $! Find out what tools are wrong. In earlier versions of Perl, exec (and System) does not change your output buffer, in this case, you need to set $ | enable the number order buffering function on one or more file handles to prevent the loss of output in the exec environment, the output is probably out of order in the system environment. In Perl 5.6, the environment is almost the same.

If you want the operating system to run a new French program in an existing process (like the exec function of Perl), you need to report the French program to be implemented over there, but you also reported what the new French (the first parameter through the process) implements it. In terms of customs, you report that its name is only a copy of the French location, but this is not necessary because there are two self-reliant parameters at the level of C. If the name is not a copy, then you may see a strange result: the new French thought that it was running in a name that was totally different from the actual path name of its location. All such for those confused French do not have any questions, but some French simply considerate name, and according to their own name change will have different personality. For example, the VI editor will check whether it is "Vi" or "View. If it is used as a "View", it will take the initiative to open the read-only mode, as if it is used with the-R command line option.

This time is the place where the optional pathname parameter of exec takes effect. In terms of semantics, it is placed in the indirect tool location, just like the file handle of print and printf. Yes, it does not need to have a tool on the back, because it is not actually a department of the parameter list. (In a sense, Perl is the opposite of the method adopted by the operating system. It thinks that the first parameter is the most important, and if it is different, you can click the path name .) For example:

$ Editor = "/usr/bin/VI ";

Exec $ Editor "View", @ files # Trigger read-only mode

Or die "couldn't execute $ Editor: $! /N ";

Like any other indirect tool, you can also replace the simple Scalar of the above reserved French name with a block of the free code, so that you can simplify the previous example:

Exec {"/usr/bin/VI"} "View" @ files # Trigger read-only mode

Or die "couldn't execute $ Editor: $! /N ";

As mentioned above, exec regards a separated parameter list as a tag that should bypass shell disposal. However, there is still a place to mix you up. Exec misappropriation (and System) cannot distinguish a Single Scalar Parameter from a list with only one element.

@ ARGs = ("Echo surprise") # only one element exists inside and outside the column.

Exec @ ARGs # shell escape

Or die "Exec: $! "; # Because @ ARGs = 1

To stop this environment, you can use the pathname syntax to clearly copy the first parameter as the path name, so that other parameters are forced to be interpreted as a list, even though there is only one element in reality:

Exec {$ ARGs [0]} @ ARGs # A list of only one element is safe.

Or die "can't exec @ ARGs: $! ";

The first version that does not contain the arc, run the Echo program, and inform it of "surprise" as the parameter. The second version is not like this-it tries to run a program literally called Echo surprise, but it cannot be found (we hope so), and then put $! Set to a non-zero value to indicate failure.

Because the exec function is used immediately after fork, it assumes that the work to be generated at any time when the previous Perl process is stopped is compromised. In exec hours, Perl will not use your end block, nor destroy methods related to any tool. Otherwise, the liquidation tasks you plan to perform in the parent course will be done during your child journey. (We hope this is true in our lives .)

It is a very common error to regard Exec As a system application. If you use the popular-W command line switch to run it, you probably use the use warnings QW (Exec syntax) if the statement on the back of exec is not die, warn, or exit, Perl will warn you. If you really want to follow some other statements on the back of exec, you can use one of the following two spirits to stop warning:

Exec ('foo) or print stderr "couldn't exec FOO: $! ";

{Exec ('foo')}; print stderr "couldn't exec FOO: $! ";

As shown in the second line above, if the time for misappropriating exec is the last statement in a block, you can avoid warning.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.