in reply to Inserting content into a file at a particular location

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";

Replies are listed 'Best First'.
Re^2: Inserting content into a file at a particular location
by Corion (Patriarch) on Aug 25, 2010 at 09:38 UTC

    In fact, the Tie::File documentation explicitly warns against inserting elements containing newlines (the default record separator.

    Inserting records that contain the record separator string is not supported by this module. It will probably produce a reasonable result, but what this result will be may change in a future version. Use 'splice' to insert records or to replace one record with several.
Re^2: Inserting content into a file at a particular location
by perlpal (Scribe) on Aug 25, 2010 at 09:50 UTC

    >> does not work

    The script just hangs at the point when it reaches the insertion code.

    >> Note : the file handle is opened in the following mode "+>>".

    Would Tie::File work if the file permissions were changed?

    The reason i'm using TIE::File is to modify the file in place (insert content on finding placeholders within the file) instead of using a temp file to achieve the same.

      Would Tie::File work if the file permissions were changed?
      Well, appropriate file permissions (read, write) are of course required. But file permissions don't solve the issue of the file opened in append mode. Nor the problem of inserting a newline instead of using splice.