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

Hi,

Got a quick question that I was wondering if you guys might be able to help with.

I had a case recently where I was working on some code where I was using warn, however STDERR was closed. warn() seemed to pick up the next opene file handle in my application and print to that.

I'm not good at explaining things so the following one liner shows the behaviour I am talking about

$ perl -le 'close(STDERR); open AA, ">/tmp/stderrtest"; warn "Hello"; +close AA;'; cat /tmp/stderrtest Hello at -e line 1. $

As you can see, warn wasn't able to output to STDERR, but instead printed out to the AA filehandle and printed the Hello message into /tmp/stderrtest

Outputting to another file handle because STDERR is closed seems like dangerous behaviour to me, and could lead to very strange problems and data corruption, if you have a lot of file handles open. I would have thought an error would have been more appropriate.

My question was really just if anyone was aware of this behaviour? Should this happen (or is it a bug)? and if it's not a bug does anyone know the reasoning behind warn() working like this?

Thanks for your help
Al

Replies are listed 'Best First'.
Re: Using warn() when STDERR is closed
by shawnhcorey (Friar) on Jul 15, 2010 at 16:38 UTC

    You should never close STDERR. This first time your program tries to print to it, it can't, which creates another error, which it can't print, which creates another error, ...

    Try this instead:

    use File::Spec; open STDERR, '>', File::Spec->devnull() or exit( 1 );

      Or more commonly, redirect at the shell:

      perl -le 'open AA, ">test.txt"; warn "Hello"; print AA "foo\n";' 2> +/dev/null

      -derby

        Not everyone runs Linux.

        It's a sad, sad world we live in. :(

      This first time your program tries to print to it, it can't, which creates another error, which it can't print, which creates another error, ...

      So, closing STDERR will lead to an infinite loop if an error occurs. As is easily demonstrated:

      % perl close STDERR; die "ZOMG, infinite loop!"; __END__ %

      Or not. :)

      Actually, writing to a closed file handle doesn't generate an error in most situations:

      % perl close STDOUT; print "Generate error!\n"; __END__ %

      If you enable warnings and use Perl code to write to a closed file handle, then you do generate a warning:

      % perl -w close STDOUT; print "Generate error!\n"; __END__ print() on closed filehandle STDOUT at - line 2. %

      But the code that puts out that warning isn't Perl code and so doesn't generate a warning if the handle that it is writing to is closed. So not even the following causes an infinite loop:

      % perl -w close STDOUT; close STDERR; print "Generate error!\n"; __END__ %

      I can even go rather far trying to facilitate an infinite loop without causing one:

      % perl -w my $loop; $SIG{__WARN__}= $loop= sub { $SIG{__WARN__}= $loop; print STDERR "Got: ", @_; print "Warn: ", @_; }; close STDOUT; print "Generate warning!\n"; die "Done.\n"; __END__ Got: print() on closed filehandle STDOUT at - line 7. print() on closed filehandle STDOUT at - line 4. Done. %

      But, yes, close()ing STDERR is indeed a bad idea (for other reasons, which I covered in another reply).

      - tye        

Re: Using warn() when STDERR is closed
by jethro (Monsignor) on Jul 15, 2010 at 16:14 UTC
    > perl -le 'open AA, ">/tmp/stderrtest"; close(STDERR); warn "Hello"; +close AA;' ; cat /tmp/stderrtest >

    As you can see it is not the next filehandle/file descriptor that warn() prints to, but just to file descriptor 2. When you close STDERR the newly opened file gets the now vacated file descriptor

      ...but just to file descriptor 2.

      Still, it's a little weird that it only prints to file descriptor 2 (hard-wired), if STDERR is closed. I.e., if you reopen STDERR after fd 2 has been taken by something else, so STDERR is then associated with fd 3 for example, the warning is nicely printed to fd 3, not fd 2...

      In other words, it seems this behaviour is explicitly coded somewhere as a fallback for when STDERR is closed.

        Still, it's a little weird that it only prints to file descriptor 2 (hard-wired), if STDERR is closed.
        Uhm, the kernel itself doesn't know anything about stderr. It's a convention used by utilities. And the convention is, "file descriptor 2 is stderr". (Just as file descriptor 1 is stdout, and file descriptor 0 is stdin).

        Making the file descriptor variable would break many things, not in the least the expectation of users.