<?xml version="1.0" encoding="windows-1252"?>
<node id="489953" title="Re: inline file modification" created="2005-09-07 13:50:32" updated="2005-09-07 09:50:32">
<type id="11">
note</type>
<author id="61400">
joeface</author>
<data>
<field name="doctext">
&lt;p&gt;
Look at the &lt;a href="http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm"&gt;Tie::File&lt;/a&gt; module.  It allows you to edit a file in-place as if it were an array.
&lt;/p&gt;

&lt;p&gt;
This example replaces any line containing $idtag with a line containing $replacement.
&lt;/p&gt;

&lt;code&gt;
use Tie::File;

my $file = 'filename';
my @tied_file;

my $idtag = 'idTag';
my $replacement = 'replacement line';

# Create the array @tied_file from $file
tie(@tied_file, 'Tie::File', $file);

# Preserve the index so we can work directly on lines of @tied_file
foreach my $i (0 .. $#tied_file) {

    if ($tied_file[$i] =~ /$idtag/) {

        # Do something to this line of @tied_file
        $tied_file[$i] = $replacement;

    }

}

untie(@tied_file);
&lt;/code&gt;

</field>
<field name="root_node">
489913</field>
<field name="parent_node">
489913</field>
</data>
</node>
