use Win32::Job;
use Data::Dumper;
use IO::File;
my $fh = new IO::File;
tie(*$fh => 'RedirectHandle', \&debug);
$job = Win32::Job->new;
my $pid = $job->spawn("cmd.exe", q{cmd /c dir}, {
stdin => 'NUL',
stdout => \*$fh,
stderr => \*$fh,
});
print $fh $pid;
$ok = $job->run(30);
print $fh Dumper($job->status);
print $fh "Applications finished ok!" if $ok;
exit;
sub debug {
foreach (@_) {
map { print "# $_\n" } split /\n/;
}
}
{
package RedirectHandle;
sub print_handle {
my $this = shift;
my $handle = $this->{HANDLE};
if (ref($handle) eq 'SCALAR') {
$$handle .= $_[0]
}
elsif (ref($handle) eq 'CODE') {
&$handle($_[0])
}
else {
print $handle $_[0]
}
1;
}
sub TIEHANDLE {
my $class = shift;
bless({ HANDLE => $_[0] }, $class);
}
sub PRINT {
my $this = shift;
$this->print_handle(join("", @_));
}
}