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

My below script on Windows puts error or any messages in a txt file. The only thing it displays to screen are the copy command errors which probably will never happen but I want to make sure they dont show up on screen if it does ever happen. Anyway to eliminate the copy commmand errors so they show up in my error txt file or something.

I thought I could use the output to null: copy($fileOne,$fileTwo) > null

That wont work. Please advise.
use File::Copy; .... my $errorLog = "copyError.txt"; open STDERR, ">$errorLog" || die "Cant open $errorLog:\n$!"; #Show output to below file my $information = "$copy.txt"; open(FH, ">$information") || die "Cant open $information:\n$!"; select(FH); copy($fileOne,$fileTwo) || die "Did not copy: $!"; close(STDERR) || die "Cant close STDERR: $!"; close(FH) || die "Cant close File Handle: $!";

Replies are listed 'Best First'.
Re: Error output to screen
by derby (Abbot) on Aug 01, 2006 at 13:51 UTC

    File::Copy calls croak when an error happens. The easiest way to prevent croak from outputting to STDERR is to wrap the call in an eval:

    eval { copy($fileOne,$fileTwo); }; if( $@ ) { die "Did not copy: $@"; }

    -derby

    Update: then again ... since you've re-opened STDERR to a file and since croak calls die and die outs to STDERR ... there's really no need for any of this (unless your platform does something funky when re-opening STDERR).

      Thanks,

      I tried as suggested and it still shows copy errors to my dos screen and doesnt show in STDERR file. Any other suggestions?
Re: Error output to screen
by imp (Priest) on Aug 01, 2006 at 14:43 UTC
    It's good that you are testing the return values of each call, and dying when a critical error occurs is generally a good idea. But in this case you don't want to actually die, as this also prints to STDERR - you just want to exit. For example,
    eval { copy($fileOne,$fileTwo) or die "Failed to copy $fileOne to $fileTw +o - $!"; }; if ($@) { # Record the log information in here exit 1; }
    My personal preference when developing on windows nt/xp systems is to use the event log instead of a log file. I have had good luck with Win32::EventLog::Carp.