_learning perl_, ch14, 14.I.2002, nh PROCESS MANAGEMENT launch a new process: use the `system` function ^^^^^^ * hands a single string to a brand-new /bin/sh shell to be executed * STDIN, STDOUT, and STDERR are inherited from perl process (also inherits current umask, current directory, and uid) * NONZERO EXIT VALUES MEAN IT DID **NOT** WORK! * double-quoted strings variable-interpolated - also inherits current environment variables - can modify them through special hash %ENV keys are the names ov env variables -- PATH, USER, &c. - `local` can be used to set an element of %ENV a temp value: { local $ENV{"PATH"} = "/bin:/usr/bin:/usr/ucb"; # more commands here } or use ``s -- backquotes * put a /bin/sh command between `s` $now = "the time is now " . `date`; * can evaluate backquoted commands within a list context: foreach $_ (`who`) { ($who,$where,$when) = /(\S+)\s+\S+)\s+(.*)/; print "$who on $where at $when\n"; } normally, ou just get the standard output of the commands within the backquotes as the value of the backqouted-string: die "rm spoke!" if `rm fred 2>&1`; --process dies if rm says anything, to STDOUT *or* to STDERR using processes as filehandles ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ can create a process-filehandle that either caputres the output from *or* provides input to the process. (but not both.) open(WHOPROC,"who|"); # open who for reading - `|` on the right side of 'who' means that this `open` is not about a filename but a command to be started - all normal file I/O operators apply to this process-filehandle - can also use | to executed a command that expects input: open (LPR,"|lpr -Psnoriprint"); print LPR @report; close(LPR); # waits until process ends # if you don't close it, the process can keep # running beyond the execution of the perl script - to discard error messages: open(LPR,"|lpr -Psnoriprint >/dev/null 2>&1"); - can do multiple commands at once: open(WHOPR, "ls | tail -r |"); fork | | | ~~~~ | | | --clones current perl process ("child") \|/ | -- return value is zero for the child; nonzero (the | child's process ID) for parent (or "undef" if the | system fails) exec ~~~~ just like `system` except it *replaces* the current perl process with the shell wait ~~~~ waits for the child to complete waitpid ~~~~~~~ waits for a specific child to complete waitpid($kidpid,0); # cleans up after dead kid exit ~~~~ - causes an immediate exite from the current perl process - takes an optional parameter, which serves as the numeric exit value that can be noticed by the parent process. default is 0. sending & receiving signals ~~~~~~~~~~~~~~~~~~~~~~~~~~~ - a small number, one-bit message - respone to a signal is the signal's "action"