If you can manage it (i.e. if you're invoking some command directly, without any shell scripting or advanced redirection shenanigans), the safest thing to do is to avoid passing data through the shell entirely.
In perl 5.8+:
my @output_lines = do {
open my $fh, "-|", $command, @args or die "Failed spawning $command: $!";
<$fh>;
};
If it's necessary to support 5.6:
my @output_lines = do {
my $pid = open my $fh, "-|";
die "Couldn't fork: $!" unless defined $pid;
if (!$pid) {
exec $command, @args or die "Eek, exec failed: $!";
} else {
<$fh>; # This is the value of the C<do>
}
};
See perldoc perlipc
for more information on this kind of business, and see also IPC::Open2
and IPC::Open3
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…