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

Hi,
I am running a few jobs using crontab.
If any of those jobs produces output it is automatically being sent to some email addresses (this is a good thing for me). I am trying to get STDERR to my perl program without it going through files if possible (directly to a list or a scalar) so that crontab won't send every error by mail (usually i can handle them). thanks.

UPDATE:
I just re-read my post and I think it was a bit confusing. here are some better sorted details:

* The job im running in cron is a perl script (every few minutes).
* I have the cron variable MAILTO (which is the problem - I can't remove it - it's needed for other jobs).
* Im not the only one in the MAILTO list.

what im trying to do is :
1. stop the standard error from being printed so that it won't cause mails to be sent constantly.
2. read the standard error to recover from problems and ignore things that are not problems (like CVS printing things to STDERR for example).

the actual command that im running is :
system("CVS -d :ext:....");
and i would like to get it's STDERR to a variable and (if possible) not through a file.

Replies are listed 'Best First'.
Re: Capturing STDERR to a variable
by CountZero (Bishop) on Nov 02, 2009 at 07:35 UTC
    On Perl 5.8 or newer you can do:
    use strict; my $err_string; close STDERR; open STDERR, '>', \$err_string or die "Could not redirect STDERR: $!"; print STDERR 'This is an error!'; print "***$err_string***\n";
    See perldoc -f open for the details.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Capturing STDERR to a variable
by Anonymous Monk on Nov 02, 2009 at 06:51 UTC
    make your crontab entry something resembling
    foobar.pl 2>/path/to/my/stderr.txt
    or you could
    open STDERR, '>', '/path/to/my/stderr.txt';
    although that might not work for processes you exec/system
Re: Capturing STDERR to a variable
by ambrus (Abbot) on Nov 02, 2009 at 09:39 UTC

    You can also define

    MAILTO=""
    in your crontab, which will instruct cron not to send any output in mail.
Re: Capturing STDERR to a variable
by oron (Novice) on Nov 02, 2009 at 19:58 UTC
    I just re-read my post and I think it was a bit confusing.
    here are some better sorted details:

    * The job im running in cron is a perl script (every few minutes).
    * I have the cron variable MAILTO (which is the problem - I can't remove it - it's needed for other jobs).
    * Im not the only one in the MAILTO list.

    what im trying to do is :
    1. stop the standard error from being printed so that it won't cause mails to be sent constantly.
    2. read the standard error to recover from problems and ignore things that are not problems (like CVS printing things to STDERR for example).

    the actual command that im running is :
    system("CVS -d :ext:....");
    and i would like to get it's STDERR to a variable and (if possible) not through a file.

      If you're ok with converting the program's arguments into shell literals and combining STDOUT and STDERR,

      my $output = `CVS -d :ext:.... 2>&1`;

      If not,

      use IPC::Run3 qw( run3 ); run3( [ 'CVS', '-d', ':ext:....' ], \undef, # /dev/null \my $out_fr_chld, \my $err_fr_chld, );