How Perl calls external commands and how they differ

Source: Internet
Author: User

The main methods are summarized as follows:
1.System ("command");
Using this command will open a child process to execute the command in quotation marks, and the parent process will wait for the child process to end and continue executing the following code.

2.exec ("command");
The effect is similar to the system command, except that the child process is not opened and the parent process is replaced, so the process ends after executing the command in quotation marks. Used in conjunction with fork.

3.' command ';
Calling an external command with an anti-quote can capture its standard output and return it as a row, accompanied by a carriage return at the end of each line. The variables in the counter-quotes are interpolated to their values at compile time.

4.Open LIST "ls-l|";
open More "|more";
@list =<list>;
print more @list;
Close (LIST);
Close (more);
Use a file handle with a pipeline to execute an external command, similar to a read-write file. You can read data from the output of an external command, or you can output the data to an external command as input.

5.defined (my $pid =fork) or die "Can not fork: $!\n";
unless ($pid) {
EXEC ("date");
    }
waitpid ($pid, 0);
Using fork will open the code after the child process executes concurrently with the parent process, where the fork in the parent process returns a nonzero number, and zero is returned in the child process. The above code completes the same functionality as system ("date"). Fork can perform more complex process operations than simply invoking external commands on the system.


Use System, EXEC, readpipe functions in Perl to execute systems commands

Transferred from: http://cn.waterlin.org/

In Perl, you can invoke other scripts, system commands, and so on with the three commands of system, exec, readpipe. The main difference between the three commands is the return value.

1) for system This function, it returns the state after execution, for example

@args = ("command", "Arg1″," Arg2″);
System (@args) = = 0
Or die "System @args failed: $?"

Of course, you can also check the cause of the error with a statement similar to the following:

if ($ = =-1) {
Print "Failed to execute: $!\n";
}
elsif ($ & 127) {
printf "Child died with signal%d,%s coredump\n",
($? & 127), ($?) & 128)? ' with ': ' Without ';
}
else {
printf "Child exited with value%d\n", $? >> 8;
}

2) and for the EXEC function, it is simply a command that executes a system, and generally does not return a value. EXEC returns a value of False only if the system does not have the command you want to execute.

EXEC (' foo ') or print STDERR "couldn ' t exec foo: $!";
{exec (' foo ')}; Print STDERR "couldn ' t exec foo: $!";

3) When we need to save the result of running the system command so that we can analyze it and make further processing, we should use the readpipe function. For example:

@result = readpipe ("LS-L/tmp");
print "@result";

will produce the following results:

Drwxr-xr-x 2 root root 4096 Mar 11:55 TestDir

Of course, you can also put the generated results into a file in order to write the work log, publish the report.

$inject _command = "./configchecker.bat f:/nic/3502/array-4ad2e0573/etc". $device _name;
ChDir "f:/testtools/bin/";
@temp_result = readpipe ($inject _command);
Open (Result_file, ">result.txt");
Print Result_file @temp_result;
Close (Result_file);

In this way, you throw the results of the system running into the Result.txt file in the directory where the system commands are located.

Finish!

How Perl calls external commands and how they differ

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.