in reply to Inserting content into a file at a particular location
does not workDoes 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 | |
|
Re^2: Inserting content into a file at a particular location
by perlpal (Scribe) on Aug 25, 2010 at 09:50 UTC | |
by JavaFan (Canon) on Aug 25, 2010 at 10:00 UTC |