does not work
Does not work in which sense? USER NOTE doesn't get replaced? The file is garbled? The program dies? It never gets out of bed? What? Be specific!

Note : the file handle is opened in the following mode "+>>".
Uhm, I'd be surprised if Tie::File works if all writes are forced to be at the end of the file. I'm also not convinced adding newlines in the middle of lines is a good idea with Tie::File - if you want to tie the file to an array, and the basic idea is to add a line, splice your line in.

But why are you using Tie::File anyway? What's the benefit? It seems to me you can do this in a single pass. Untested:

use autodie; open my $in, "<", "your-file"; open my $out, ">", "your-file.tmp"; while (<$in>) { print $out $_; if (/USER\s+NOTE/i) { print $out "@$global_variables_list\n"; last; } } while (<$in>) { print $out $_; } close $out; close $in; rename "your-file.tmp", "your-file";

In reply to Re: Inserting content into a file at a particular location by JavaFan
in thread Inserting content into a file at a particular location by perlpal

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.