in reply to Create PerlIO::via layer to append data at the end of a file

The layers are popped after the file is closed, so POPPED is not useful here.

int Perl_PerlIO_close(pTHX_ PerlIO *f) { const int code = PerlIO__close(aTHX_ f); while (PerlIOValid(f)) { PerlIO_pop(aTHX_ f); } return code; }

CLOSE would seem to be the proper place, but Perl closes the layers from starting at the bottom (system level) on up. The file is already closed by the time your CLOSE handler is called.

IV PerlIOBase_close(pTHX_ PerlIO *f) { IV code = -1; if (PerlIOValid(f)) { PerlIO *n = PerlIONext(f); code = PerlIO_flush(f); PerlIOBase(f)->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_OPEN); while (PerlIOValid(n)) { const PerlIO_funcs * const tab = PerlIOBase(n)->tab; if (tab && tab->Close) { if ((*tab->Close)(aTHX_ n) != 0) code = -1; break; } else { PerlIOBase(n)->flags &= ~(PERLIO_F_CANREAD | PERLIO_F_CANWRITE | PERLIO_F_ +OPEN); } n = PerlIONext(n); } } else { SETERRNO(EBADF, SS_IVCHAN); } return code; }

I don't see a PerlIO solution.

Replies are listed 'Best First'.
Re^2: Create PerlIO::via layer to append data at the end of a file
by ikegami (Patriarch) on Jun 15, 2010 at 21:30 UTC

    I appeared to have been mistaken. PerlIONext gets the next layer towards the bottom (system level), so PerlIOBase_close closes the layers starting at the top.

    The problem actually lies in PerlIO::via.

    IV PerlIOVia_close(pTHX_ PerlIO * f) { PerlIOVia *s = PerlIOSelf(f, PerlIOVia); IV code = PerlIOBase_close(aTHX_ f); SV *result = PerlIOVia_method(aTHX_ f, MYMethod(CLOSE), G_SCALAR, Nullsv); if (result && SvIV(result) != 0) code = SvIV(result); PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); return code; }

    It calls the rest of the chain (down to the system level) before calling your custom handler.

    1. utf8
    2. encoding(UTF-8)
    3. via
    4. perlio
    5. unix
    6. Mine ← !!

    It should be calling your custom handler in order.

    1. utf8
    2. encoding(UTF-8)
    3. via
    4. Mine ←
    5. perlio
    6. unix

    Reported as Perl RT#75780, including a trivial fix.

      Thanks for your thorough investigation of the matter. I already did a workaround as it seems that PerlIO::via cannot be used for my purpose.