gw1500se has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to use IPC::Run and am falling flat on my face. There seems to be no examples around that show a simple command with parameters that I can understand. I have tried 2 different ways to do this:
@cmd=("/bin/cpio","-ov",">$tape");
and
@cmd=("/bin/cpio -ov >$tape");
The first gives me a "too many arguments" error and the second tells me "/bin/cpio: invalid option --". How do I set up the command list for these functions? Thanks.
Re: Using IPC::Run
by pc88mxer (Vicar) on Jul 04, 2008 at 03:07 UTC
|
use IPC::Run qw(run);
...
my @cmd = ("/bin/cpio", "-ov");
run \@cmd, '>', $tape or die "cpio: $?"
| [reply] [d/l] |
|
Thanks. That has advanced the ball but I am not at the goal line yet. You cleared up my confusion with respect to arguments and redirection but now I need to better understand how things work. Here is my current code segment:
.
.
.
my @cmd=("/bin/cpio","-ov");
my $h=start(\@cmd,">",$tape,\$in,\$out,\$err);
$thisline=readline(FILELIST);
while (defined $thisline) {
($thisfile,$thisfilesize)=split(/\t/,$thisline
+);
$in="$thisfile\n";
print("Dumping $in");
$h->pump || die putmsg("Dieing - $err");
if (length($out)>0) {
putmsg($out);
}
$total+=$thisfilesize;
$complete=sprintf("%.2f",($total/$dumpsize)*10
+0);
putmsg("$complete%\n");
$thisline=readline(FILELIST);
}
finish $h;
.
.
.
The script now blocks forever on the 'pump' function. At the same time the harness is always pumpable so if I use 'pump_nb' and test 'pumpable' it goes into an unending loop. Finally, even the first 'pump' execution does not cause 'cpio' to do anything. At this point I don't know if it is 'cpio' that I need to understand better or IPC::Run. Any ideas?
The objective is to go through a list of files/directories provided by 'FILELIST' and have 'cpio' write them to tape. After each file dump, output the file/directory name and calculate the percent complete from the file size which was also provided by 'FILELIST'. That means expecting 'cpio' to read a file from STDIN and write the filename to STDOUT when it dumps it. That appears to be the way it works from command line except the entire input must be provided at once. What I expect IPC:Run to do, if I understand it, is to essentially make 'cpio' interactive, programmatically. | [reply] [d/l] |
Re: Using IPC::Run
by sgifford (Prior) on Jul 04, 2008 at 05:29 UTC
|
An important thing to realize is that >tape is not an argument to the program, it's an instruction to the shell. The shell will open up the file tape, and redirect the output of cpio to that file. The command will never see that file on its command line at all.
Many commands that start programs, like system, exec, and IPC::Run::start, if given a list will directly execute the given program with the given arguments, without using the shell at all. Since it's the shell's job to handle output redirection, with no shell it doesn't happen.
It looks like you have one workable solution in your responses already, which is to use IPC::Run's speicial syntax for output redirection, something like IPC::Run::run \@cmd, '>', $tape. The docs say you can also pass a single string to IPC::Run::run, which will be passed to the shell, so IPC::Run::run("/bin/cpio -ov >$tape") might also work. Finally, for something so simple, consider just system.
Hope this helps!
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
The docs on CPAN indicate that IPC::Run can already set up pipelines. It has this example:
# Piping between children
run \@cat, '|', \@gzip ;
| [reply] [d/l] |
Re: Using IPC::Run
by starbolin (Hermit) on Jul 04, 2008 at 03:18 UTC
|
It doesn't seem that IO::Run just concatenates the dereferenced array. At least I didn't see in the doc where they exampled that. The closest I see is something like: run \@cat, '>&', \$out_and_err Which is a different animal entirely. I think with your syntax run() is trying to execute /bin/cpio then a command -iov and then a command >$tape. The second problem is that -iov is neither a command nor an argument. It is part of the command cpio. Try run ["/bin/cpio -iov"], '>', ["$tape"];
s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s
|-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,,
$|=1,select$,,$,,$,,1e-1;print;redo}
| [reply] [d/l] [select] |
|
|