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

Hello monks, I need to write STDOUT to a file. My program calls system(...) and the called program writes some lines to SDTDOUT but I want to write them to a file. How can I do that? There program called has no option to write the output to a file. I need to make it manually. Thank you in advance.

Replies are listed 'Best First'.
Re: How to print STDOUT to a file
by tirwhan (Abbot) on Nov 14, 2005 at 11:22 UTC
    system("script.sh >> filename");
    if you're on a *NIX. This appends to the file (so doesn't clobber it), if you want to start anew with every call to system use ">" instead of ">>".

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      system("script.sh >> filename");
      calls the shell interpreter under the hood, something that can be very inconvenient (and even dangerous). For instance, the script arguments have to be properly quoted or they could be wrongly splitted by the shell

      An alternative way to redirect the output for the called program is to reopen STDOUT:

      open my $oldout, ">&STDOUT" or die "Can't dup STDOUT: $!"; open STDOUT, ">", $fn or die "Can't open $fn: $!"; system $script, @args; open STDOUT, ">&", $oldout;
      Or on Unix/Linux systems, you can use fork and exec instead of system and perform the redirection after the fork:
      my $pid = fork; if (defined $pid and $pid == 0) { open STDOUT, ">", $fn or die; exec $script, @args; exit 1; }
        Works fine. Thank you very much.
Re: How to print STDOUT to a file
by Moron (Curate) on Nov 14, 2005 at 13:50 UTC
    If all the STDOUT output of your program should be redirected, then, from the command line prompt:
    YourProgram.pl > YourProgram.out

    -M

    Free your mind

Re: How to print STDOUT to a file
by prasadbabu (Prior) on Nov 14, 2005 at 11:23 UTC

    You have to use a file handle.

    open (FOUT, "filename") || die("you cant open"); print FOUT $str;

    or another way is to use >filename.txt in the command prompt to redirect the output from printing STDOUT, provided if your are using windows os.

    Prasad

      Sorry but both solutions dont work, or I misunderstood you. Writing to a file would just result in printing the status of system(). Using system(prog, options, > file) results in a failure telling me that i dont use prog rigth an listing the options available. What did i wrong? I am working on Windows.

        Could you show us the exact system command you are using so that we can identify the mistake exactly.

        Prasad

Re: How to print STDOUT to a file
by kwaping (Priest) on Nov 14, 2005 at 17:45 UTC
    You can also capture output from a system call using backticks (``). From "perldoc -f system":

    This is not what you want to use to capture the output from a command, for that you should use merely backticks or "qx//", as described in "`STRING`" in perlop.

    Using the backticks method, you can save the output in a variable, which can then be printed to file or otherwise manipulated.
Re: How to print STDOUT to a file
by perlfan (Parson) on Nov 15, 2005 at 02:51 UTC
    Redirecting to STOUT at the commandline is definitely the way to go. You may use "print STDOUT $foo" and "print STDERR $bar" to show stuff on the screen and redirect to a file.
    #!/usr/bin/env perl print STDOUT "this goes gets redirected"; print STDERR "this doesn't";
    Note that STDERR may be redirected into STDOUT or into its own file using various methods provided by the shell.

    I personally like Unix's "everything is a filter" approach where you can built the utilites so that they are made to be piped together. This includes accepting STDIN using diamond operator, "<>". Check out "Data Munging with Perl". It covers this approach very well.