Chapter 14. Process Management
One of the best parts of being a programmer is launching someone else’s code so you don’t have to write it yourself. It’s time to learn how to manage your children[309] by launching other programs directly from Perl.
Like everything else in Perl, There’s More Than One Way To Do It, with lots of overlap, variations, and special features. If you don’t like the first way, read on for another page or two for a solution more to your liking.
Perl is very portable. Most of the rest of this book doesn’t need many notes saying that it works this way on Unix systems, that way on Windows, and the other way on VMS. But when you’re starting other programs on your machine, different programs are available on a Macintosh than you’ll likely find on a Cray. The examples in this chapter are primarily Unix-based; if you have a non-Unix system, expect to see some differences.
The system Function
The simplest way to launch a child process in Perl to run a program is the system
function. For example, to invoke the Unix date
command from within Perl, it looks like this:
system "date";
The child process runs the date
command, which inherits Perl’s standard input, standard output, and standard error. This mean the normal short date-and-time string generated by date
ends up wherever Perl’s STDOUT
was going.
The parameter to the system function
is generally whatever you’d normally type at the shell. If it were a more complicated command, like "ls -l $HOME
“, we’d just have put all ...
Get Learning Perl, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.