CJ7
CJ7

Reputation: 23295

How to redirect STDERR within script?

I can redirect STDERR from the command line by doing:

perl myscript.pl 2> err.txt

How can I do this within the script so that the person running the script doesn't have to do it?

Upvotes: 0

Views: 213

Answers (1)

zdim
zdim

Reputation: 66964

This is what I do

open STDERR, '>', "$errfile"
    or warn "Cannot redirect STDERR to $errfile: $!";

If this is in a library, call carp instead of warn. That requires use Carp;, for core module Carp.

In case the STDERR stream will need to be restored first save it, as shown in open.

Upvotes: 3

Related Questions