Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the differences between system and backticks and pipes in Perl?

Perl supports three ways (that I know of) of running external programs:

system:

   system PROGRAM LIST 

as in:

system "abc"; 

backticks as in:

`abc`; 

running it through a pipe as in:

open ABC, "abc|"; 

What are the differences between them? Here's what I know:

  1. You can use backticks and pipes to get the output of the command easily.
  2. that's it (more in future edits?)
like image 472
Nathan Fellman Avatar asked Apr 28 '09 10:04

Nathan Fellman


People also ask

What does Backtick do in Perl?

In list context, backticks returns a list of lines that were the output of the command. In scalar context, it returns them as a single string joined with newlines.

What is the difference between system and exec in Perl?

exec - Perldoc Browser. The exec function executes a system command and never returns; use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system's command shell (see below).

What is System command in Perl?

Perl's system command provides a way to execute a shell command on the underlying platform. For example, if you are running on a Unix platform, the command: system("ls -l") will print a long listing of files to stdout.


2 Answers

  • system(): runs command and returns command's exit status
  • backticks: runs command and returns the command's output
  • pipes : runs command and allows you to use them as an handle

Also backticks redirects the executed program's STDOUT to a variable, and system sends it to your main program's STDOUT.

like image 101
dfa Avatar answered Oct 09 '22 14:10

dfa


The perlipc documentation explains the various ways that you can interact with other processes from Perl, and perlfunc's open documentation explains the piped filehandles.

  • The system sends its output to standard output (and error)
  • The backticks captures the standard output and returns it (but not standard error)
  • The piped open allows you to capture the output and read it from a file handle, or to print to a file handle and use that as the input for the external command.

There are also modules that handle these details with the cross-platform edge cases.

like image 43
brian d foy Avatar answered Oct 09 '22 16:10

brian d foy