Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,

I would like to be able to re-direct the STDOUT of a system call to a specific file. I'm using the following code:

#!Perl/bin/perl.exe -w use strict; use File::Spec::Win32; use FileHandle; # print "Enter the model handle\n"; # print " mh=: "; chomp (my $mh = <STDIN>); my $fh=FileHandle->new(">Model_Information.txt"); &phork; sub phork { open(STDOUT, '> ' . File::Spec->$fh); open(STDERR, '> ' . File::Spec->devnull); } system "SomeSystemCall.exe";
I'm having no luck...any suggestions?

Thanks

Replies are listed 'Best First'.
Re: system and STDOUT
by robartes (Priest) on Nov 15, 2002 at 23:31 UTC
    Two of your options are:
    • As long as you're using system, redirect your STDOUT using standard shell redirects:
      my $file="camel.out"; system("cat camel.txt >$file");
    • Use backticks to capture the output, and write that to the file:
      my @output=split `cat camel.txt`; open FILE ">camel.out" or die "My camel is sick: $!\n"; print FILE for @output; # This code is untested, so beware

      CU
      Robartes-