Chapter 15. Process Management

One of the best parts of being a programmer is launching someone else’s code so that you don’t have to write it yourself. It’s time to learn how to manage your children—child processes, that is—by launching other programs directly from Perl.

And like everything else in Perl, There’s More Than One Way To Do It, with lots of overlap, variations, and special features. So, if you don’t like the first way, just 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 in Unix systems and that way in Windows and some other way on VMS. But when you’re starting other programs on your machine, different programs are available on a Macintosh than what you’ll likely find on an old Cray (which used to be a “super” computer). The examples in this chapter are primarily Unix based; if you have a non-Unix system, you can expect to see some differences.

The system Function

The simplest way to launch a child process in Perl to run a program is with the system function. For example, to invoke the Unix date command from within Perl, you tell system that’s the program you want to run:

system 'date';

These commands depend on your system, what it provides, and how it implements those commands. They aren’t Perl, but something Perl asks the system to do for your program. The same Unix command might have different calling conventions and options on different versions ...

Get Learning Perl, 8th 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.