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

I have a problem in getting the error code from within in an eval to print out
$logf="logfile"; select STDERR; $|=1; unless (open STDERR, "|-") { eval { open ERRLOG, ">$logf" or die "Can't open $logf: $!\n"; while(<>) { print ERRLOG $_; print STDERR $_; } }; exit(0);
I would like the error produced by the die to appear in STDERR and/or STDOUT, but can't seem to get it to work
Any help appreciated

Replies are listed 'Best First'.
Re: eval error
by phaylon (Curate) on Oct 18, 2005 at 15:25 UTC
    Have a look at perldoc -f eval, You'll find the error in $@.

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: eval error
by Roy Johnson (Monsignor) on Oct 18, 2005 at 15:25 UTC
    What are you expecting your reopen of STDERR to do?

    Caution: Contents may have been coded under pressure.
      I was hoping that the reopen would allow STDERR to go to both the logfile and screen
      This actually works, although from subsequent comments I suspect it's not perhaps the best way of doing things
      I tried looking at the value $@ but this didn't help in this case
      So the question is how do I get STDERR to go to both screen and logfile simultaneously ?
        I think you want to look at IO::Tee

        Caution: Contents may have been coded under pressure.
Re: eval error
by ioannis (Abbot) on Oct 18, 2005 at 20:26 UTC
    The STDERR is reopened during the fork, this time as stdin to the child; the child, however, has no code to excecute (because of the unless during the fork. So what is the purpose reopening STDERR to talk to a child that in most cases has already terminated?
      See my reply above. It allows the simultaneous writing of STDERR/STDOUT to screen and logfile in real time.
      This is a Unix system by the way