in reply to match a line and edit it

One way to modify a file in-place is to use Tie::File. The following will replace all lines which have the string 'foo' with the line 'This is now bar':
use strict; use warnings; use Tie::File; tie my @array, 'Tie::File', 'file.txt' or die $!; for (@array) { if (/foo/) { $_ = 'This is now bar'; } } untie @array;

Replies are listed 'Best First'.
Re^2: match a line and edit it
by Anonymous Monk on Mar 17, 2010 at 01:41 UTC
    Thanks much. That worked.
Re^2: match a line and edit it
by Anonymous Monk on Mar 17, 2010 at 23:33 UTC
    May I know, How I can add a new line char with the line I am adding, like
    if (/foo/) { $_ = "This is now bar\n"; }

    I tried the above, but is not adding a new line, I tried to set autochomp => 0 also.