I hope I understood your problem right. I think your problem is that
$command may contain characters that are special for the
shell, and they cause the open to fail.
Answers to these problems can be found in man perlipc and
perldoc -f exec. What you need to do is a "safe pipe open",
opening a pipe that doesn't involve the shell. The way to do this is
forking your process, with a pipe from the child to the parent, and
then doing an exec to $command in the child.
Forking a child and opening a pipe between them can be done in a
single Perl command:
my $pid = open my $kid => "-|";
This forks the program, returning the child PID in the parent, while
opening a pipe from the child to the parent. If the fork fails,
$pid is undefined.
The next tricky thing in the exec. If we would do a simple
exec $command, Perl would call the shell if $command
contains special characters, and that is what we are trying to avoid.
If the command had arguments, we could supply exec with a
list (of more than one element) and exec would avoid calling
the shell, but we don't have that option. But there is another way we
can have exec avoid calling the shell, and that is by giving
it a block as first argument. The result of the block will be how the
program we are going to call is named, so we can just supply $command.
This would give us:
exec {$file} $file or die "exec() failed: $!\n";
A complete program that does a safe pipe open:
#!/usr/bin/perl
use strict;
use warnings;
my $file = '....'; # Command with special characters.
my $pid = open my $kid => "-|";
die "fork() failed: $!\n" unless defined $pid;
unless ($pid) {
exec {$file} $file or die "exec() failed: $!\n";
}
while (<$kid>) {
print;
}
__END__
Abigail
|