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

Does anyone know how to get <STDERR> to write to <STDOUT>? Thank You Much.

Replies are listed 'Best First'.
(jcwren) Re: Getting STDERR to write to STDOUT?
by jcwren (Prior) on Jul 10, 2000 at 04:06 UTC
    You can do this:
    *STDERR = *STDOUT;
    There may be better ways, but that works.

    --Chris

    e-mail jcwren
Re: Getting STDERR to write to STDOUT?
by turnstep (Parson) on Jul 10, 2000 at 14:51 UTC

    There is always the canonical:

    open(STDERR, ">&STDOUT");
    Which merely says to send STDERR's output to wherever STDOUT was going (i.e. the screen or the browser). The example jcwren gave pretty much does the same thing as well: this is a case where it is pretty safe to reassign the whole typeglob, because most coders are not going to be using something like @STDERR or even $STDERR. If you are, stop it right now! :) Save it for an obfuscated code entry.

    If this is for a cgi script, as is often the case with this question, a better thing than redirecting to STDOUT (in my opinion) is to send it to a file:

    open(STDERR, ">/home/AM/stderr.out");

    That way, you avoid "mixing" the outout of STDOUT and STDERR. The two are usually quite distinguishable, but when it gets stuck in the middle of some HMTL, all bets are off. :)

RE: Getting STDERR to write to STDOUT?
by autark (Friar) on Jul 10, 2000 at 04:14 UTC
    I assume you want to catch the STDERR of a program, then you can do this:
    open(HANDLE, "my_prg 2>&1 |") || die "error: $!\n";
    or
    $output = `my_prg 2>&1`;
RE: Getting STDERR to write to STDOUT?
by Russ (Deacon) on Jul 10, 2000 at 07:40 UTC
    CGI::Carp provides this functionality for you.
    BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or die("Unable to open mycgi-log: $!\n"); carpout(LOG); }
    BTW, in case you are doing this to see error messages in the browser window, you should use:
    use CGI::Carp qw(fatalsToBrowser);
    near the top of your CGI script.

    Russ