Another interesting approach to IPC is making your single program go
multiprocess and communicate between (or even amongst) yourselves. The
open() function will accept a file argument of either "-|"
or "|-"
to do a very interesting thing: it forks a child connected to the
filehandle you've opened. The child is running the same program as the
parent. This is useful for safely opening a file when running under an
assumed UID or GID, for example. If you open a pipe to minus, you can
write to the filehandle you opened and your kid will find it in his
STDIN. If you open a pipe from minus, you can read from the filehandle
you opened whatever your kid writes to his STDOUT.
Another common use for this construct is when you need to execute something without the shell's interference. With system(), it's straigh-forward, but you can't use a pipe open or backticks safely. That's because there's no way to stop the shell from getting its hands on your arguments. Instead, use lower-level control to call exec() directly.
Here's a safe backtick or pipe open for read:
And here's a safe pipe open for writing:
Note that these operations are full Unix forks, which means they may not be correctly implemented on alien systems. Additionally, these are not true multithreading. If you'd like to learn more about threading, see the modules file mentioned below in the SEE ALSO section.