Hello Limbomusic and welcome to the monastery and to the wonderful world of Perl!

I know nothing about what you are telling (madlibs) nor I understand the web related part (it seems a very crude type of web interaction) but if you are trying to prepend some text to an already existing file.. no there is no an automatic way to do it, as far as i know.

You need a two steps work. Between different possibilities you can work with a temporary file: you open a tempfile, you write $new_lines to it then you open the existing original file for read and write it's lines to the temp one. Finally you swap files.

Or (if your existing file is not huge) you can avoid the tempfile overwriting the original (a security copy can be handy anyway; if something go bad you can loose everything!). Something like:

use strict; use warnings; my @new_lines = some_function; # something that retrieve new lines my $file_path = 'file_path.ext'; open my $orig_file, '<', $file_path or die "unable to open '$file_path +' for read!"; my @orig_lines = <$orig_file>; close $orig_file; #reopen it wiping the content open my $new_file, '>', $file_path or die "unable to open '$file_path' + for write!"; #print first new get lines and then old ones print $new_file $_ for @new_lines,@orig_lines;

L*

PS this it is discussed in Perl FAQs: append to the beginning of a file.

Also at SO brian_d_foy tells us a bit more detailed answer: prepend to a file.

See also rename to swap files and sysopen and it's constant if you want more granularity on opening only yet existing file.

HtH

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: appending to html at beginning by Discipulus
in thread appending to html at beginning by Limbomusic

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.