Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Using IPC::Run

by gw1500se (Beadle)
on Jul 04, 2008 at 02:36 UTC ( [id://695501]=perlquestion: print w/replies, xml ) Need Help??

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.

Replies are listed 'Best First'.
Re: Using IPC::Run
by pc88mxer (Vicar) on Jul 04, 2008 at 03:07 UTC
    Use:
    use IPC::Run qw(run); ... my @cmd = ("/bin/cpio", "-ov"); run \@cmd, '>', $tape or die "cpio: $?"
      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.
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!

      Finally, for something so simple, consider just system.
      The point to using IPC::Run is that you can invoke another program with arguments and some I/O-redirection without having anything re-interpreted by a shell. For instance, system("/bin/cpio -ov >$tape") won't work as desired if $tape has a space in it.

      Using the multi-argument form of system allows you to specify a command's arguments without worrying about shell re-interpretation. IPC::Run extends this capability to include simple I/O-redirection. The next step would be the ability to set up complete pipelines of commands without having to use a shell.

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}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://695501]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-19 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found