16.2. Running Another ProgramProblemYou want to run another program from your own, pause until the other program is done, and then continue. The other program should have same STDIN and STDOUT as you have. Solution
Call $status = system("vi $myfile");
If you don't want the shell involved, pass $status = system("vi", $myfile); Discussion
The
Like system("cmd1 args | cmd2 | cmd3 >outfile"); system("cmd args <infile >outfile 2>errfile");
To avoid the shell, call $status = system($program, $arg1, $arg); die "$program exited funny: $?" unless $status == 0;
The returned status value is not just the exit value: it includes the signal number (if any) that the process died from. This is the same value that
The if (($signo = system(@arglist)) &= 127) { die "program killed by signal $signo\n"; }
To get the effect of a if ($pid = fork) { # parent catches INT and berates user local $SIG{INT} = sub { print "Tsk tsk, no process interruptus\n" }; waitpid($pid, 0); } else { die "cannot fork: $!" unless defined $pid; # child ignores INT and does its thing $SIG{INT} = "IGNORE"; exec("summarize", "/etc/logfiles") or die "Can't exec: $!\n"; }
A few programs examine their own program name. Shells look to see whether they were called with a leading minus to indicate interactivity. The
expn
program at the end of Chapter 18 behaves differently if called as
vrfy
, which can happen if you've installed the file under two different links as suggested. This is why you shouldn't trust that
If you want to fib to the program you're executing about its own name, specify the real path as the "indirect object" in front of the list passed to $shell = '/bin/tcsh'; system $shell '-csh'; # pretend it's a login shell Or, more directly: system {'/bin/tcsh'} '-csh'; # pretend it's a login shell
In the next example, the program's real pathname is supplied in the indirect object slot as # call expn as vrfy system {'/home/tchrist/scripts/expn'} 'vrfy', @ADDRESSES;
Using an indirect object with @args = ( "echo surprise" ); system @args; # subject to shell escapes if @args == 1 system { $args[0] } @args; # safe even with one-arg list
The first version, the one without the indirect object, ran the
echo
program, passing it See Also
The section on
"Cooperating with Strangers"
in
Chapter 6
of
Programming Perl
, or
perlsec
(1); the
|
|