views:

1149

answers:

4

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?)
+14  A: 
  • 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.

dfa
pipes is a more fine grained control of backticks. backticks return the STDOUT, as in: $date = `date`. With pipes, you can decide when and how to read the output, or to send signals to the process.
Mathieu Longtin
+1  A: 

system is also returning the exit value of the application (ERRORLEVEL in Windows). Pipes are a bit more complicated to use, as reading from them and closing them adds extra code. Finally, they have different implementation which was meant to do different things. Using pipes you're able to communicate back with the executed applications, while the other commands doesn't allow that (easily).

Moshe
+1  A: 

Getting the program's exit status is not limited to system(). When you call close(PIPE), it returns the exit status, and you can get the latest exit status for all 3 methods from $?.

Please also note that

readpipe('...')

is the same as

`...`
pts
could you complete this sentence?Please also note that readpipe('...') instead of ... .
Nathan Fellman
I fixed the formatting to make it clearer. The use of backticks to denote fixed-type makes it difficult to type *real* backticks...
ephemient
As a further note, `...` and qx/.../ (and qx(...) and qx#...# and any other delimiters) are also equivalent.
ephemient
+1  A: 

The perlipc documentation explains the various ways that you can interact with other processes from Perl.

brian d foy