14. Process Management
Contents:
14.1 Using system and execWhen you give the shell a command line to execute, the shell usually creates a new process to execute the command. This new process becomes a child of the shell, executing independently, yet coordinating with the shell. Similarly, a Perl program can launch new processes, and like most other operations, has more than one way to do so.
The simplest way to launch a new process is to use the
system("date");
We're ignoring the return value here, but it's not likely that the date command is going to fail anyway. Where does the command's output go? In fact, where does the input come from, if it's a command that wants input? These are good questions, and the answers to these questions are most of what distinguishes the various forms of process-creation.
For the system("date >right_now") && die "cannot create right_now";
This time, we not only send the output of the
date
command into a file with a redirection to the shell, but also check the return status. If the
return status is true (nonzero), something went wrong with the shell command, and the
The argument to Here's an example of generating a date and who command to the shell, sending the output to a filename specified by a Perl variable. This all takes place in the background so that we don't have to wait for it before continuing with the Perl script: $where = "who_out.". ++$i; # get a new filename system "(date; who) >$where &";
The return value from A child process inherits many things from its parent besides the standard filehandles. These include the current umask, current directory, and of course, the user ID. Additionally, all environment variables are inherited by the child. These variables are typically altered by the csh setenv command or the corresponding assignment and export by the /bin/sh shell. Environment variables are used by many utilities, including the shells, to alter or control the way that utility operates.
Perl gives you a way to examine and alter current
environment variables through a special hash called
For example, here's a simple program that acts like printenv : foreach $key (sort keys %ENV) { print "$key=$ENV{$key}\n"; }
Note the equal sign here is not an assignment, but simply a text character that the
Here's a program snippet that alters the value of
$oldPATH = $ENV{"PATH"}; # save previous path $ENV{"PATH"} = "/bin:/usr/bin:/usr/ucb"; # force known path system("grep fred bedrock >output"); # run command $ENV{"PATH"} = $oldPATH; # restore previous path That's a lot of typing. It'd be faster just to set a local value for this hash element.
Despite its other shortcomings, the { local $ENV{"PATH"} = "/bin:/usr/bin:/usr/ucb"; system "grep fred bedrock >output"; }
The
system "grep 'fred flintstone' buffaloes"; # using shell system "grep","fred flintstone","buffaloes"; # avoiding shell
Giving Here's another example of equivalent forms: @cfiles = ("fred.c","barney.c"); # what to compile @options = ("-DHARD","-DGRANITE"); # options system "cc -o slate @options @cfiles"; # using shell system "cc","-o","slate",@options,@cfiles; # avoiding shell |
|