Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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

In reply to Working with other processes and programs by vroom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found