tags:

views:

77

answers:

2

I am using use Win32::Process for my application run as below. It runs fine, but I did not get any way to get the output to a .txt file.

I used NORMAL_PRIORITY_CLASS rather than CREATE_NEW_CONSOLE to get the output on the same terminal itself, but I don't know how to redirect it to a txt file.

/rocky

#!/usr/bin/perl

use strict;
use warnings;
use Win32::Process;

Win32::Process::Create(my $ProcessObj,
                       "iperf.exe",
                       "iperf.exe -u -s -p 5001",
                       0,
            NORMAL_PRIORITY_CLASS,
                       ".") || die ErrorReport();
my @command_output;

push @command_output,$ProcessObj;

open FILE, ">zz.txt" or die $!;
print FILE @command_output;
close FILE;

sleep 10;
$ProcessObj->Kill(0);

sub ErrorReport{
    print Win32::FormatMessage( Win32::GetLastError() );
}
+4  A: 

Win32::Process is one of those modules that's basically a straight port of the Win32 API into Perl and thus makes little sense to a Perl programmer. Unless you want to do something very Windows specific, there are better ways to do it.

If all you want is to get the output of a process, you can just run it normally using Perl's backticks.

my $output = `iperf.exe -u -s -p 5001`;

If you want to get fancy, for example backgrounding a process and capturing its output, I'd recommend IPC::Run.

Schwern
+1  A: 

From perlfaq8:

Backticks (``) run a command and return what it sent to STDOUT.

More details can be found in perlop.

Zaid