Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Processes as Filehandles So far, you've seen ways to deal with synchronous processes, where Perl stays in charge, launches a command, (usually) waits for it to finish, then possibly grabs its output. But Perl can also launch a child process that stays alive, communicating 22 to Perl on an ongoing basis until the task is complete. The syntax for launching a concurrent (parallel) child process is to put the command as the "filename" for an open call, and either precede or follow the command with a vertical bar, which is the "pipe" character. For that reason, this is often called a piped open. In the two-argument form, the pipe goes before or after the command that you want to run: open DATE, 'date|' or die "cannot pipe from date: $!"; open MAIL, '|mail merlyn' or die "cannot pipe to mail: $!"; In the first example, with the vertical bar on the right, Perl launches the command with its standard output connected to the DATE filehandle opened for reading, similar to the way that the command date | your_program would work from the shell. In the second example, with the vertical bar on the left, Perl connects the command's standard input to the MAIL filehandle opened for writing, similar to what happens with the command your_program | mail merlyn. In either case, the command continues independently of the Perl process. 23 The open fails if Perl can't start the child process. If the command itself does not exist or exits erroneously, Perl will not see this as an error when opening the filehandle, but as an error when closing it. We'll get to that in a moment.