abishek mann abishek mann - 2 months ago 7
Perl Question

Pass arguments through pipe to external command with system one after another

I am trying to open a external command from Perl using system call. I am working on Windows. How can I pass arguments to it one after another?

For example:

system("ex1.exe","arg1",arg2",....);


Here
ex1.exe
is external command and i would like it to process arg1 first and then arg2 and so on...

I would appreciate for your reply,

Answer

Use a pipe open:

use strict; 
use warnings;

{
    local ++$|;

    open my $EX1_PIPE, '|-', 'ex1.exe' 
        or die $!;

    print $EX1_PIPE "$_\n"
        for qw/arg1 arg2 arg3/;

    close $EX1_PIPE or die $!;
}

I'm assuming you want to pipe data to ex1.exe's STDIN; for example, if ex1.exe is the following perl script:

print while <>;

Then if you run the above code your output should be:

arg1
arg2
arg3