Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Working with other processes and programs

by vroom (His Eminence)
on Jan 04, 2000 at 23:31 UTC ( #1698=perltutorial: print w/replies, xml ) Need Help??

Perl makes it very easy to interface between other programs. In this tutorial you'll learn how to do some very basic things like writing to another program, reading the output from another program, just running an outside program.

The easiest and most often used way to run a program is to use the system function. When system is called a child process is made and executed, once it is finished it returns to the parent program (your script) and continues with its execution.
For example if you had an image processing script you might want to allow a user to view the resulting image at some point. You could do this with a call like the following:
system "xv $imagename";
When the system call was made in the program it would launch the program in this case xv. The execution of our program would stop until after we had closed our xv program. After we close xv however our Perl script would continue to run the code after the system statement.
Now lets say you want to collect the output from a program and do something to it. There are at least two ways to do this. One is with backticks (usually on the key to the left of your 1 key). This allows you to collect all of the output from a program into a variable;
$output=`more datafile`;
Another way is to use open with a pipe. Basically this works the same as working with a filehandle that you're reading from. All you do is something like:
$pid=open READER, "programname arguments|" or die "Can't run the progr +am: $!\n"; while(<READER>){ $output.=$_; } close READER;
This open call returns the process id of the process it spawns. Then you just read from the handle with the <> operator and close it when you're finished. If you think about how you write to files you can probably guess how you write to processes.
$pid=open WRITETOME, "|programname arguments" or die "Couldn't fork pr +ocess"; print WRITETOME "write this\n"; close WRITETOME;
All you have to do is open the process with the pipe on the left side, and then handle it like you would handle printing to a file.

If you want to read and write to the same process take a look at IPC::Open2 if you want to handle stderr in addition to that check out IPC::Open3

Replies are listed 'Best First'.
Re: Working with other processes and programs
by Linuxboy (Novice) on Sep 18, 2002 at 18:25 UTC
    Here are a few additional related details:

    My pid is in $$ or is returned by getpgrp(0)

    My parent's pid is getppid()

    Launching a process and *not* waiting is done with fork but read the fork docs before using it because it doesn't work like you might expect... Using open instead of fork is probably a better choice much of the time.

    Closing the opened handle without reading the output dumps the output to stdout. (this could be implementation specific??)

    You can get the exit code of the child process from $? >>8


      Other thoughts to consider when evaluating return codes can include how the process was created. For example, if one uses the IPC::Open3 module to run the external command and the processes (pid) is killed using 'kill( 9, $pid )' the '$? >> 8' evaluation may result in 0. This is probably not the expected value, seeing how the processes was terminated. However, if one inspects the '${^CHILD_ERROR_NATIVE}' the result would show an exit value of 9.

      According to PerlDoc:

      If the filehandle came from a piped open, close returns false if one of the other syscalls involved fails or if its program exits with non-zero status. If the only problem was that the program exited non-zero, $! will be set to 0. Closing a pipe also waits for the process executing on the pipe to exit--in case you wish to look at the output of the pipe afterwards--and implicitly puts the exit status value of that command into $? and ${^CHILD_ERROR_NATIVE}.

      Admittedly this may be a result of how things are terminated, closed and cleaned up. But it is a thought to consider.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://1698]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2023-10-01 17:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found