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

I am trying to create a PerlIO::via layer (to be used with file handles opened for writing only) that appends a string (eg the string 'END') at the end of a file. So for example you should be able to do something like
open my $fh '>:via(Append)','/some/file'; print $fh "Lorem ipsum dolor sit amet "; close $fh;
and that would output the string "Lorem ipsum dolor sit amet END" to the file. So far I have tried the following:
package PerlIO::via::Append; sub PUSHED { my ($class,$mode,$fh)=@_; return -1 if $mode eq 'r' || $mode eq 'r+'; my $buf=''; bless \$buf,$class; } sub WRITE { my ($self,$buf,$fh)=@_; $$self.=$buf; length($buf); } sub FLUSH { my ($self,$fh)=@_; print $fh $$self or return -1; $$self=''; 0; } sub POPPED { my ($self,$fh)=@_; print $fh "\n"; }
but that doesn't work, the print in POPPED fails. Any ideas?

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

    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.

      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.
Re: Create PerlIO::via layer to append data at the end of a file
by ambrus (Abbot) on Jun 16, 2010 at 13:49 UTC

    Instead of an IO layer, would a tied handle do for you? Here's an example.

    use warnings; use strict; use 5.006; { package Tie::Handle::CloseAppend; sub TIEHANDLE { my($class, $string, $handle) = @_; my %obj; $obj{"handle"} = $handle; # this argument to tie is optional $obj{"string"} = $string; $obj{"open"} = defined($handle); bless \%obj, $class; } sub OPEN { my($obj, $arg, @arg) = @_; $$obj{"open"} = 1; open $$obj{"handle"}, $arg, @arg; } sub _preclose { my($obj) = @_; printf { $$obj{"handle"} } "%s", $$obj{"string"} or warn "warning: could not print closing string to Tie::Handle:: +CloseAppend handle: $!"; } sub CLOSE { my($obj) = @_; _preclose($obj); $$obj{"open"} = 0; close $$obj{"handle"}; } sub DESTROY { my($obj) = @_; if ($$obj{"open"}) { _preclose($obj); } }
    } use Symbol; my $APP = gensym(); # Create the lexical handle the old style way, # for open can autovivify globs but tie can't. tie *$APP, Tie::Handle::CloseAppend::, "END"; open $APP, ">-" or die "error opening"; print $APP "Lorem ipsum dolor sit amet "; # note: perl will close handle when $APP goes out of scope __END__

    Update: changed line in TIEHANDLE from $obj{"open"} = 0; to $obj{"open"} = defined($handle);.