How can I insert a line at the beginning of a file with Perl's Tie::File?

Posted by thebourneid on Stack Overflow See other posts from Stack Overflow or by thebourneid
Published on 2010-05-21T23:47:03Z Indexed on 2010/05/22 15:10 UTC
Read the original article Hit count: 161

Filed under:
|

I'm trying to insert/add a line 'COMMENT DUMMY' at the beginnig of a file as a first row if /PATTERN/ not found. I know how to do this with OPEN CLOSE function. Probably after reading the file it should look something like this:

open F, ">", $fn or die "could not open file: $!"; ;
     print F "COMMENT DUMMY\n", @array;
close F;

But I have a need to implement this with the use of the Tie::File function and don't know how.

use strict; 
use warnings; 
use Tie::File;


my $fn = 'test.txt';
tie my @lines, 'Tie::File', $fn or die "could not tie file: $!";

untie @lines; 

© Stack Overflow or respective owner

Related posts about perl

Related posts about tie-file