abishek mann
abishek mann

Reputation: 45

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,

Upvotes: 1

Views: 6726

Answers (2)

Pedro Silva
Pedro Silva

Reputation: 4700

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

Upvotes: 7

mfollett
mfollett

Reputation: 1814

Are you trying to execute ex1.exe once for each argument? Something similar to:

> ex1.exe arg1
> ex1.exe arg2
> ex1.exe arg3

If so, you would do:

for my $arg (@args)
{
   system( 'ex1.exe', $arg);
} 

Upvotes: 1

Related Questions