Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Process Management

Similar presentations


Presentation on theme: "Perl Process Management"— Presentation transcript:

1 Perl Process Management
Learning Objectives: To learn the different Perl’s commands for invoking system process To learn how to perform process management in a Perl program Running Shell Commands – system Backquotes Using Processes as Filehandles

2 Running shell command - system (1)
Perl programs can execute shell commands (Bourne shell) using the system function. system("date"); When the command is finished, the system function returns 0 (not 1!) if the command completed successfully. if(system("date") != 0){ print "Wah! Date failed!\n"; }

3 Running shell command - system (2)
Where does the command output go? Answer: The display screen, unless it is redirected. $ cat system1 #!/usr/local/bin/perl5 -w system("date"); $ system1 Thu Apr 15 14:30:03 HKT 1999 $ How to redirect the I/O? Answer: Same as the shell $ cat system2 #!/usr/local/bin/perl5 -w if(system("date >now")){ die "cannot create file now"; } $ system2 $ cat now Thu Apr 15 14:30:03 HKT 1999 $

4 Running shell command - system (4)
system allows multiple commands, separated by semicolons or newlines. Processes that end in & are run as background jobs (Perl continues and does not wait for the job to finish). $ cat system3 #!/usr/local/bin/perl5 -w $where = "now"; # Perl variable, not shell variable system "(date; who) >$where &"; $ system3 $ cat now Thu Apr 15 14:58:56 HKT 1999 horner pts/0 Apr 15 14:28 (csz096.cs.ust.hk) $

5 Running shell command - system (6)
How would you look for the string “Bill Gates” in the file bill? $ grep "Bill Gates" bill Answer: system allows the first argument to be the command, and the remaining arguments to be the arguments to the command. system("grep", "Bill Gates", "bill");

6 Backquotes (1) How do you assign the output of a command to a variable? Answer: Use backquotes. $now = `date`; The value of $now will contain the result of the date commend (including the trailing newline).

7 Backquotes (2) Another example: $ cat back1 #!/usr/local/bin/perl5 -w
$where = `pwd`; print "The current directory is: $where"; $ back1 The current directory is /homes/horner $

8 Backquotes (3) If the backquoted command is used in a list context, you get a list of strings, each one being a line (terminated in a newline). $ who horner pts/ Apr 15 14:28 (csz096.cs.ust.hk) $ cat back2 #!/usr/local/bin/perl5 -w foreach $_ (`who`){ ($who, $where, $when) = /(\S+)\s+(\S+)\s+(.*)/; print "$who on $where at $when\n"; } $ back2 horner on pts/0 at Apr 15 14:28 (csz096.cs.ust.hk) $

9 Using Processes as Filehandles (input)
If we use a pipe instead of backquote, the Perl program reads input from the previous pipe stage and is therefore more memory efficient and need not wait for the previous program to finish. $ cat whopipe #!/usr/local/bin/perl5 -w open(INFILE, "who|") || die("Cannot run who…\n"); while (<INFILE>) { ($who, $where, $when) =/(\S+)\s+(\S+)\s+(.*)/; print "$who on $where at $when\n"; }

10 Using Processes as Filehandles (1)
Another way to run a shell command looks similar to filehandles, using open, close, and print. The following example allows you to customize a letter and automatically it to different recipients: $ cat mail1 #!/usr/local/bin/perl5 -w open(FILE, "letter1"); # open the file @letter = <FILE>; # read the contents close(FILE); @ List = foreach $ open(MAIL, "|mail -s \"hi!\" $ "); #open message print MAIL "Hi $ ,\n"; # add header to message print # add content to message close MAIL; # send message! } $ mail1

11 Using Processes as Filehandles (2)
This example prints the letter: $ cat print1 #!/usr/local/bin/perl5 -w open(FILE, "letter1"); # open the file @letter = <FILE>; # read the contents close(FILE); @nameList = qw(Andrew Bill Bob); foreach $name open(PRINT, "|lpr -Pcll3"); # open print job print PRINT "Hi $name,\n"; # add header to job print # add content to job close PRINT; # send print job! } $ print1


Download ppt "Perl Process Management"

Similar presentations


Ads by Google