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

Please advise if I am doing this correct. I do need to close my STDERR?? I have been running this script and it runs great but just added the "close STDERR". Is my "close STDERR" statement needed?
open STDERR, "> $errlog" || die "Cant redirect error: $!\n"; print STDERR "Permission/Error Log\n", scalar(localtime), "\n\n"; open(FH, ">> $statspage") || die "Cant open file: $!\n"; select(FH); #get data here etc... close(FH); close(STDERR); #is this close needed here??

Replies are listed 'Best First'.
Re: close on STDERR
by Zaxo (Archbishop) on Jul 10, 2003 at 14:28 UTC

    It's not needed for simple situations. The handle will be closed when the script exits, or when STDERR is opened elsewhere..

    It is good practice, however, to localize global handles, in which case I usually close the local instance:

    { open local(*STDERR), '>>', $errlog or die q(Can't redirect error: ),$!; print STDERR "Permission/Error Log\n", scalar( localtime), "\n\n"; # ... close STDERR or die $!; }
    I changed your open mode for the log to append, so old entries are not truncated away.

    After Compline,
    Zaxo

Re: close on STDERR
by archon (Monk) on Jul 10, 2003 at 15:17 UTC
    no need to open or close it manually. just use warn() instead.
      I agree with that however it appears that Zaxo wants all his warnings and errors to go to a specific file. Warn just sends it to STDERR. I do not believe that opening and closing STDERR will cause any real problems though in his situation (redirection of standard error). However, the Camel book says that standard error is, "The default output stream for nasty remarks that don't belong in standard output. Represented within a Perl program by the filehandle STDERR. You can use this stream explicitly, but the die and warn built-ins write to your standard error stream automatically."

      However it also says, "Although filehandles STDIN, STDOUT, and STDERR start out with file descriptors of 0,1, and 2 (the Unix standard convention), even they can change if you start closing and opening them with wild abandon."

      I usually just pipe errors warnings to my log files using command line notation, ie:
      /usr/bin/perl script.pl 2>>/path/logfile
Re: close on STDERR
by pzbagel (Chaplain) on Jul 10, 2003 at 23:37 UTC

    Are you attempting to capture all output to STDERR to the file?(including warns and dies) or are you writing out only your own error messages to the STDERR handle(as your code snippet implies)? If you are controlling what is output to the STDERR filehandle through print statements, then there is no reason to use a standard filehandle name and risk conflicts and complexity due to clobbering the existing STDERR. Just call it ERR and be done with it. No fuss, no muss. And yes, it is nice of your to close it when you are done.

    HTH

A reply falls below the community's threshold of quality. You may see it by logging in.