Reputation: 889
I am using Cygwin Perl on Windows XP.
I have this code that will run a third party program.
sub main {
print("Start Running\n");
@return = run_exe($execApp, $parm1, $parm2);
my $returncode = pop @return;
if ($returncode == 0) {
print("Success\n");
}
else {
print("Error\n");
}
}
sub run_exe {
my ($exe, @parm) = @_;
my (@output, @return_output);
@output = system($exe, @parm);
foreach (@output) {
next if !/\S/; # white space line
s/^(\s*)//g; # trim from the front for $_
s/(\s*)$//g; # trim from the end for $_
push (@return_output, $_);
}
push (@return_output, $?>>8);
@output = ();
return (@return_output);
}
This would print if Success:
Start Running
Return Code: 0
Success
What I want is not to print the output from running run_exe subroutine (i.e Return Code: 0):
Start Running
Success
How could I achieve this? Please help me, I have search in Google but I found nothing.
Upvotes: 2
Views: 5335
Reputation: 29
I just ran into the same issue today and remembered I'd found the solution many, many brain cells ago (about 4 year's worth of brain cells ago). After spending a good while searching my old code, I found the solution. Here it is. Simply put your system call inside back-ticks, and append "2>&1". Here's an example.
my $results=nslookup -type=ns stackoverflow.com 2>&1
;
The results of your system call are assigned to $results, and not printed. I can't remember exactly why or how that is, but it is.
This is important to me because quite often I'm running a script in my PERL editor, and if I allow a lot of printing (i.e., if the above command is repeated thousands of times) it will use a lot of system memory, and eventually all the system memory.
I know this is an old topic, but hopefully it well be of value to the next poor, unfortunate soul maniacally googling for a solution to the problem, as I was tonight.
Upvotes: 1
Reputation: 126772
You have used system
in your program, which lets the program you are running print to STDOUT and returns the exit status of the program (a number).
So what you have at present makes no sense, as @output
will contain just a single number and you cannot parse it as output from the program you are running.
What you probably need is backticks
`$exe @parm`
or, more clearly,
qx{$exe @parm}
which will return the output from the program, as you seem to be expecting, and avoid it being printed to STDOUT.
Note that you may need to pay attention to the contents of $exe
and @parm
, as the resulting string must be valid at the command line, and the fields may need quoting or escaping appropriately.
Update
Looking again at your code again it seems that you may be expecting the exit status from system
. But putting it an array, looping over the array, and extracting non-blank elements is very convoluted! The result is the same value as $?
that you add to the returned array.
To divert the output of a program you need to run it from the command shell instead of directly, so to send the output to the Windows nul device your code would look like
system('cmd /C', $exe, @parm, '>nul');
return $? >> 8;
In fact you may as well drop the subroutine and change the lines
@return = run_exe($execApp,$parm1,$parm2);
my $returncode = pop @return;
to
system('cmd /C', $execApp, $parm1, $parm2, '>nul');
my $returncode = $? >> 8;
Upvotes: 4