Search Perl pages
Professions, Sciences, Humanities, Business, ...
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...
Conversions, tests, processing, manipulation,...
Integer, Floating point, Matrix, Statistics, Boolean, ...
Algorithms, Memory, Process control, Debugging, ...
Data storage, Integrity, Encryption, Compression, ...
Networks, protocols, Interprocess, Remote, Client Server, ...
Timing, Calendar and Clock, Audio, Video, Printer, Controls...
Management, Filtering, File & Directory access, Viewers, ...
Next: Why doesn't open() return an error when a pipe open fails?
There are three basic ways of running external commands:
system $cmd; # using system()
$output = `$cmd`; # using backticks (``)
open (PIPE, "cmd |"); # using open()
STDERR will go the same place as the script's versions of these, unless the command redirects them. Backticks and
STDOUT of your command.
With any of these, you can change file descriptors before the call:
or you can use Bourne shell file-descriptor redirection:
$output = `$cmd 2>some_file`;
open (PIPE, "cmd 2>some_file |");
You can also use file-descriptor redirection to make
STDERR a duplicate of
$output = `$cmd 2>&1`;
open (PIPE, "cmd 2>&1 |");
Note that you cannot simply open
STDERR to be a dup of
STDOUT in your Perl program and avoid calling the shell to do the redirection. This doesn't work:
$alloutput = `cmd args`; # stderr still escapes
This fails because the
STDERR go to where
STDOUT was going at the time of the
open(). The backticks then make
STDOUT go to a string, but don't change
STDERR (which still goes to the old
Note that you must use Bourne shell (sh(1)) redirection syntax in backticks, not
csh(1)! Details on why Perl's
backtick and pipe opens all use the Bourne shell are in http://www.perl.com/CPAN/doc/FMTEYEWTK/versus/csh.whynot
You may also use the IPC::Open3 module (part of the standard perl
distribution), but be warned that it has a different order of arguments
from IPC::Open2 (see Open3).
Source: Perl FAQ: System Interaction
Copyright: Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
Previous: Why can't I get the output of a command with system()?
(Corrections, notes, and links courtesy of RocketAware.com)
Up to: Local Process Communication
Search | About | Comments | Submit Path: RocketAware > Perl >
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments