develooper Front page | perl.perl5.porters | Postings from April 2016

What could cause print to fail before calling write(2)?

Thread Next
Maxwell Carey
April 19, 2016 19:27
What could cause print to fail before calling write(2)?
Message ID:
Interesting question on Stack Overflow:

The OP is able to print to a file but not to STDOUT or STDERR. strace shows
that write is only called when printing to the file.

perl -e'print "foo" or die $!' exit code is 9, so print is reporting EBADF.
Looking at the 5.14.2 source, I see two places that could be setting this
in pp_print:

    if (!io) {
        if ( gv && GvEGVx(gv) && (io = GvIO(GvEGV(gv)))
            && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar)))
            goto had_magic;
        goto just_say_no;
    else if (!(fp = IoOFP(io))) {
        if (IoIFP(io))
            report_wrongway_fh(gv, '<');
        goto just_say_no;

But I can't figure out what would trigger either of these conditions. I
tried closing STDOUT before running perl and strace shows

    write(1, "foo", 3)                      = -1 EBADF (Bad file descriptor)

so it's obviously getting past that point even if STDOUT is closed. Any
ideas what could cause this?

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About