How do I pipe some output to screen and some other selected output, out of the Perl script?
By using different file handles.
By default you'll have both
In your terminal
STDOUT is given the file handle
STDERR is given the file handle
Here's an example of printing to each file handle and redirecting each to /dev/null.
$ perl -e 'print STDOUT "foo\n"; print STDERR "bar\n";' foo bar $ perl -e 'print STDOUT "foo\n"; print STDERR "bar\n";' 1> /dev/null bar % perl -e 'print STDOUT "foo\n"; print STDERR "bar\n";' 2> /dev/null foo
- The first command prints both.
- The second command throws away
STDOUTand prints only
- The third command throws away
STDERRand prints only
You can also declare your own file handles (e.g., a log file) and print to that instead. See
perldoc -f open.
you probably want to read the perlipc documentation, in particular the section on Named Pipes.
perlipc - Perl interprocess communication (signals, fifos, pipes, safe subprocesses, sockets, and semaphores)
If you need two-way (read & write) communication with an external program, then the IPC::Open2 or IPC::Open3 modules (standard perl lib, also documented in the URL above) will be useful.