There are three methods to use shell commands in Perl:
- System Functions
- ''
- QX {}
First, the system function format is as follows:
System ("command ");
If the command runs normally, 0 is returned; otherwise, a non-zero value is returned.
The disadvantage of this method is that the shell command output cannot be directly captured into the Perl program.
Method 2: Use reverse quotation marks ''to directly capture the shell command output to the Perl Program (that is, its return value)
For multi-row output, assign it to the array variable, as shown in figure
@ A = 'LS-al ';
Method 3: Use QX {}. This method is applicable when the command contains multiple backquotes. In addition, curly braces {} can also be replaced with other symbols.
Pipelines in Perl are considered to be readable and writable files, as shown in
Open (rhandle, "ls-Al | sort |"); // rhandle is the Read File handle, and the part that reads the output of the sort command is open (whandle, "| more "); // whandle is the Write File handle. The written content is sent to the more command through the pipeline for paging display.