Yet another way is to cheese together a tied filehandle that simply prepends whatever you write onto the beginning of the file. Kinda like what you're doing, but with a simpler interface.
package Beginning; use Fcntl; sub TIEHANDLE { my($class)=@_; return bless { fh=> undef }, $class; } sub OPEN { my($self)=shift; return sysopen($self->{fh}, $_[0], O_RDWR | O_CREAT, 0666); } sub CLOSE { close($_[0]->{fh}) if ($_[0]->{fh}); $_[0]->{fh}=undef; } sub PRINT { my($self,$len)=shift; local $/; $_=$self->{fh}; seek $_, 0, 0 or warn "$!"; my $buf=<$_>; seek $_, 0, 0 or warn "$!"; print $_ @_; print $_ $buf; }
Then to use it just:
tie *FH, "Beginning"; open(FH, "Hello"); # This will actually be read/write print FH "Look ma, I'm first", scalar(localtime), "\n"; close(FH);
It's incomplete and not scalable, just something to think about. A fun modification might be to have the PRINT callback "shuffle" the blocks of the file forward with each write. This would save wear-and-tear on $buf (but not your disk!).

In reply to Re: Re: begining of a file by clintp
in thread begining of a file by sOKOle

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.