tags:

views:

104

answers:

2

Hi All,

I am trying to open a external command from Perl using system call. How can I pass arguments to it one after another? ( forgot to add this i am running perl on windows machine )

ex:- 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,

-Abishek

+5  A: 

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
Pedro Silva
Hi Pedro... Thank you so much. Last question :) what if my arguments are commands to ex1.exe like arg1= config file.ini; arg2 = log test.txt; arg3 = run test .... and so on should i store all of them in an array and then use that array instead...
Abishek
Yeah, just store them all in an array: `@args = ("config file.ini", "log test.txt", "run test", ...)`, and do `print $EX1_PIPE $_ for @args`.
Pedro Silva
thanks Pedro.... but after executing first command from the array teh script goes mad and i get >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> all over my console window ????
Abishek
Post `ex1l.exe`'s code, so we can see what's the problem.
Pedro Silva
ok if i want to pass enter key also with each argument what would be the argument to pass to ex1.exe....
Abishek
my bad :) its \n ...
Abishek
Yeah, sorry, that was a typo on my part. I fixed it, I guess you saw it.
Pedro Silva
thanks a lot Pedro... Also what if I want to open 2 or 3 .exe's at once.. for example ex1.exe ex2.exe ex3.exe and so on...
Abishek
+1  A: 

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);
} 
mfollett