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); } }
sub WRITE { warn Dumper(["WRITE",@_]); 1; } sub PRINT { my($obj, @arg) = @_; print { $$obj{"handle"} } @arg; } sub PRINTF { my($obj, $arg, @arg) = @_; printf { $$obj{"handle"} } $arg, @arg; } for my $m (qw"READ READLINE GETC EOF SEEK TELL") { my $mm = $m; my $p = sub { die "error: $mm unimplemented for Tie::Handle::CloseAppend han +dle"; }; { no strict "refs"; *$m = $p; } } sub BINMODE { my($obj, $arg) = @_; binmode $$obj{"handle"}, $arg; } sub FILENO { my($obj) = @_; fileno($$obj{"handle"}); }
} 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);.


In reply to Re: Create PerlIO::via layer to append data at the end of a file by ambrus
in thread Create PerlIO::via layer to append data at the end of a file by silly8888

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.