1

I want to call a EXE file in Perl which performs some action

I tried calling the exe file via backtick and system but in both the cases i get only the return value

The exe file prints some text on to the console. Is it possible to capture that as well?

I looked into this variable ${^CHILD_ERROR_NATIVE} but I get only the return value and not text

I am using Perl 5.14

Thanks in advance

KK99
  • 1,971
  • 7
  • 31
  • 64

3 Answers3

5

The application might not print its output to STDOUT but STDERR instead, which isn't captured by the backtick operator. To capture both, you could use the following:

my $binary = 'foo.exe';
my $output = `$binary 2>&1`;

For a more fine-tuned capturing, you might want to resort to IPC::Open3 with which you can "control" all of a process' streams (IN, OUT and ERR).

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
1

I used to execute commands from perl script and capture the output this way

sub execute_command() {
  my($host) = @_;
  open(COMMAND_IN, "your_command |"); 
  while (<COMMAND_IN>) 
  { #The COMMAND_IN will have the output of the command
    #Read the output of your command here...
    $ans = $_;
  }
  close(COMMAND_IN);
  return $ans;
}

Check whether it helps you

Raghuram
  • 3,937
  • 2
  • 19
  • 25
1

I recommend the capture and capture_err functions from Scriptalicious.

use Scriptalicious qw(capture);

my $output = capture('my_command', 'arg');
Marius Olsthoorn
  • 405
  • 3
  • 10
  • 2
    The module is four years old (`use 5.006`) and exports everything into the calling namespace (`@EXPORT`). There might be some issues (redefinition of `say` f.ex.) when using this module with more modern versions of perl. – dgw Feb 06 '12 at 10:10
  • Although it is old, the module works pretty well. You should probably imported the functions you need explicitly though. I added an example that does this. – Marius Olsthoorn Feb 06 '12 at 14:49