Learning Perl

Learning PerlSearch this book
Previous: 14.4 Using forkChapter 14
Process Management
Next: 14.6 Sending and Receiving Signals
 

14.5 Summary of Process Operations

Table 14.1 summarizes the operations that you have for launching a process.


Table 14.1: Summary of Subprocess Operations

Operation

Standard Input

Standard Output

Standard Error

Waited for?

system()

Inherited from program

Inherited from program

Inherited from program

Yes

Backquoted string

Inherited from program

Captured as string value

Inherited from program

Yes

open() command as filehandle for output

Connected to filehandle

Inherited from program

Inherited from program

Only at time of close()

open() command as filehandle for input

Inherited from program

Connected to filehandle

Inherited from program

Only at time of close()

fork, exec, wait, waitpid

User selected

User selected

User selected

User selected

The simplest way to create a process is with the system function. Standard input, output, and error are unaffected (they're inherited from the Perl process). A backquoted string creates a process, capturing the standard output of the process as a string value for the Perl program. Standard input and standard error are unaffected. Both these methods require that the process finish before any more code is executed.

A simple way to get an asynchronous process (one that allows the Perl program to continue before the process is complete) is to open a command as a filehandle, creating a pipe for the command's standard input or standard output. A command opened as a filehandle for reading inherits the standard input and standard error from the Perl program; a command opened as a filehandle for writing inherits the standard output and standard error from the Perl program.

The most flexible way of starting a process is to have your program invoke the fork, exec, and wait or waitpid functions, which map directly to their UNIX system call namesakes. Using these functions, you can select whether you are waiting or not, and configure the standard input, output, and error any way you choose.[5]

[5] Although it might also help to know about open(STDERR,">&STDOUT") forms for fine tuning the filehandles. See the open entry in Chapter 3 of Programming Perl, or in perlfunc (1).


Previous: 14.4 Using forkLearning PerlNext: 14.6 Sending and Receiving Signals
14.4 Using forkBook Index14.6 Sending and Receiving Signals