Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Inserting content into a file at a particular location

by perlpal (Scribe)
on Aug 25, 2010 at 05:32 UTC ( #857078=perlquestion: print w/ replies, xml ) Need Help??
perlpal has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

My requirement is to insert content (an array of variables to be more specific) into a file at the location with the place holder "USER NOTE".

I have tried to use TIE::File but seem to be missing out on something pretty fundamental as it does not work for me currently.

Excerpt of the perl code that i am using

tie my @lines, 'Tie::File', $ut_fh;#$ut_fh is a file handle for (@lines) { if (/.*USER\s+NOTE.*/i){ $_ .= "\n@$global_variables_list\n"; last; } }

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

The above code snippet causes my script to hang.

Thanks in advance!

Comment on Inserting content into a file at a particular location
Download Code
Re: Inserting content into a file at a particular location
by murugu (Curate) on Aug 25, 2010 at 06:16 UTC
    Code is untested.
    my ($offset) = grep {$lines[$_]=~/USER\s+NOTE/} (0..$#lines); splice(@lines,$offset+1,0,"@$global_variables_list");

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

      Used this piece of code but for some reason my script still hangs at this point.

Re: Inserting content into a file at a particular location
by JavaFan (Canon) on Aug 25, 2010 at 09:32 UTC
    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 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.

      >> 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://857078]
Approved by murugu
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-09-16 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (156 votes), past polls