perl process

by dan 10/05/06 12:00:00 Article perl.


$ENV{EnvVar};

# get/set env var.

$old=$ENV{""PATH""};
$ENV{""PATH""}=""/bin;/usr/bin"" ;
$ENV{""PATH""}=$old;        #restore env

PROCESS MANAGEMENT

Die (""death msg"") ;

  • exits and prints msg.
  • if msg string has ""n"" at end, it won't print name,line # of perl script.
Die (""death msg\n"") ; # no script name or line #.
#$!= most recent OS error
die ""Error:$!"" ;    #Very useful.
Warn(""warning msg"") ;
# same as die() except it doesn't die.

[system(...)]

  • synchronous- perl waits until cmd is finished.
  • Returns: 0 if success. Not 1.
  • Uses the current perl script's ENV unless changed via $ENV{}.

system(""shellCmd"")

  • execute /bin/sh & returns 0 if successful.
  • System (""date>txt"") && die ""..."";
{ local $ENV{""PATH""}=""/bin/special"" ;
system ""...""; #  execute using newer $ENV
} # once out of scope, local $ENV is restored
 # automatically. No need to restore.

system (""cmd"",""arg1"",""arg2"",...)

  • To avoid launching shell, pass the arg as list, not as one str.
  • Waits for the app to finish. Not 100% sure.
System ""grep"",""abc"",""myfile""; # avoid shell
System ""grep 'abc' myfile"";  # launches another shell.
`app` ;
just like system() except that the output is returned.
$output=`date` ;  # $output has the output of date.
@output=`who` ; # array stores multiple-line output.
If  `rm fred 2>&1` die ""rm spoke"" ;
# if rm outputs any text (even in stderr), it dies.

PROCESS HANDLE

runs app in background, asynchronous, must close()

open(PROCHANDLE,""proc|"")

open process for reading (has ""|"" on right side)

open(WHO,""who|"")

open(PROCHANDLE,""|proc"")

System Message: WARNING/2 (<string>, line 82); backlink

Inline substitution_reference start-string without end-string.

open process for writing (has ""|"" on left side)

open(LPR,""|lpr -pswriter"") ;
open(PR,""ls |tail -r|"") ;
# ls to tail to PR process.
print PROCHANDLE "" "" ;
write to a process which is open for writing
close (PROCHANDLE)
all opened proc must be closed or they may continue to run in bkg
[fork()]
creates an identical process, returns 0 for child proc, non-zero = parent
if (!defined($child_pid=fork())) {die}
elsif ($child_pid) { } # I am parent
else { } # I am child

exec (""app"")

  • replaces current process w/ shell. After process ends...
  • process ends, including perl.
  • system() is like a fork() followed by exec()
unless (fork) { exec(""date"") } #child
# same as system(""date"")
wait()
wait for process to finish
waitpid($pid)
wait for process to finish
$child_pid=fork() ;
if ($child_pid) { waitpid($child_pid) } #parent must wait
else { exec ""date"" }  #child exec date
exit() ;
causes immediate end of current process.
Unless ($pid=fork()) { unlink<*>; exit } #child dies after finishing its duty
Waitpid($pid,0) ; # parent continues here...

SUMMARY OF PROC

  1. system(), ` ` waits for process to end. To get stdout, use backtick (` `) or qx//.
  2. open
  3. ()fork,exec,wait,waitpid : asynch, very flexible

SIGNALS

#msg sent to other process/kernel

$SIG{'sigType'}='functionName' ;
intercepts signal and calls functionName().
$SIG{'INT'}='mysigint' ;
sub mysigint() { ... } # when SIGINT is received
$SIG{'sigType'}='DEFAULT' ;
restores sig to default func
kill(SIG,pid1,pid2,..) ;

Send signal to other process

kill ('INT',234,237) ; #send SIGINT to process 234,237"



Digg! del.icio.us reddit furl