Perl Process Management - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Perl Process Management

Description:

Perl programs can execute shell commands (Bourne shell) using the system function. ... variable named $where, you need to put a backslash in front of the dollar sign. ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 13
Provided by: andrew184
Category:

less

Transcript and Presenter's Notes

Title: Perl Process Management


1
Software Tools
  • Perl Process Management

2
system
  • 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
system
  • 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 143003 HKT 1999

4
system
  • How to redirect the I/O?
  • Answer Same as the shell
  • cat system2
  • !/usr/local/bin/perl5 -w
  • if(system("date gtnow"))
  • die "cannot create file now"
  • system2
  • cat now
  • Thu Apr 15 143003 HKT 1999

5
system
  • 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) gtwhere "
  • system3
  • cat now
  • Thu Apr 15 145856 HKT 1999
  • horner pts/0 Apr 15 1428 (csz096.cs.ust.hk)

6
system
  • In the previous example, where is replaced with
    its value (by Perl, not the shell).
  • If you want to reference a shell variable named
    where, you need to put a backslash in front of
    the dollar sign.
  • The invoked shell command will inherit the
    current working directory from the Perl program.

7
system
  • 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")

8
Backquotes
  • 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).

9
Backquotes
  • Another example
  • cat back1
  • !/usr/local/bin/perl5 -w
  • where pwd
  • print "The current directory is where"
  • back1
  • The current directory is /homes/horner

10
Backquotes
  • 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/0 Apr 15 1428
    (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 1428
    (csz096.cs.ust.hk)

11
Using Processes as Filehandles
  • 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 email it to different
    recipients
  • cat mail1
  • !/usr/local/bin/perl5 -w
  • open(FILE, "letter1") open the file
  • _at_letter ltFILEgt read the contents into
    _at_letter
  • close(FILE)
  • _at_emailList qw(horner_at_cs.ust.hk
    horner_at_ust.hk)
  • foreach email (_at_emailList)
  • open(MAIL, "mail -s \"hi!\" email") open
    message
  • print MAIL "Hi email,\n" add header to
    message
  • print MAIL _at_letter add content to message
  • close MAIL send message!
  • mail1

12
Using Processes as Filehandles
  • This example prints the letter
  • cat print1
  • !/usr/local/bin/perl5 -w
  • open(FILE, "letter1") open the file
  • _at_letter ltFILEgt read the contents into
    _at_letter
  • close(FILE)
  • _at_nameList qw(Andrew Bill Bob)
  • foreach name (_at_nameList)
  • open(PRINT, "lpr Pcll2a") open print job
  • print PRINT "Hi name,\n" add header to
    job
  • print PRINT _at_letter add content to job
  • close PRINT send print job!
  • print1
Write a Comment
User Comments (0)
About PowerShow.com