Managing Processes in Perl

The system Function

Calls other programs

Creates a child process to run the program being called

Perl inherits the standard input, output and error

  • Therefore if the program is printing something, that will automatically go through Perl’s STDOUT

Perl waits for the child process to finish

  • Like done on the shell, Perl can also launch programs into the background, thereby not having to wait for it
  • system “programname.exe &”;

Also note that error messages from the system call can be tracked through the $! variable

  • The exec Function

Shares similar syntax and semantics as the system function

Difference is that exec function does not create a child process, but the current Perl process makes the execution

  • Consider this function a type of “goto” statement
  • Most of the time though, system function is better option to go with
exec "program_name.pl", "-options", "arg1 arg2 arg3", @ARGV;
  • Environment Variables

PATH # colon-separated list of directories to programs referenced

Special hash variable contains all environment variables # %ENV

# Environment Variables
 while (($key, $value) = each %ENV) { print "$key = $value \n"; }
  • Back-quotes to Capture Output

Use back-quotes to capture the output of a system call

$mydate = `data`;
 # in order to keep true value of the output:
 chomp($mydate = `date`);
  • Also note that when using the back-quotes, the output could be found in the default variable $_

If the output has multiple lines, the back-quote call will receive it all as one string with newline characters embedded

To avoid this, a list can be used to store the output

@myvar = `who`; # output of online users is now separated into elements based on new line

# Backquote examples
 $mydate = `date`;
 print "The date captured was: $mydate \n";
 @myfiles = `dir`;
 print "Directory Listing Captured as: \n";
 foreach (@myfiles) { print; }
 # Note that the above also captures the newlines, therefore chomp could be useful
  • Processes as Filehandles

Perl can launch a child process that stays alive and continue to communicate with Perl as it completes

Use the OPEN / CLOSE commands, like accessing files

When calling the program, use the pipe character before or after the program name

  • After # open DATE, “date|”
  • The program is called to be read
  • Before # open MAIL, “|mail someone”
  • The program is called to be written
# Processes as Filehandlers
 open DATE, "|date" or die "Cannot pipe from date: $!";
 $mydate = <DATE>;
 print "Value Captured was: $mydate \n";
 print DATE "\n"; # In windows, the date command asks user to enter new date. We hit enter (newline)
 close DATE;
 print "End\n";
  • Sending and Receiving Signals

In UNIX, the Ctrl-C signal is SIGINT

This can be sent to child processes running under Perl program

  • In Perl the SIGINT is signal number 2. Therefore, if you know the process id, one could kill it by:
kill 2, 4201 or die "Cannot Signal 4201 with SIGINT: $!";
    • Perl program can catch system signals a follows:
$SIG{'INT'} = 'my_int_handler';
      • The above shows a special hash variable SIG, and the key value of ‘INT’ is the Ctrl-C signal, which is what the code above is catching.
      • When the signal is caught, it will initiate the ‘my_int_handler’ sub-routine