> Rajesh Dorairajan wrote:
> > Does anyone know of a way to open a file in append mode and append
on top of
>
> I don't know why people are having a problem with this sollution,
simply 
> open in append mode so open doesn't clobber the file, then use seek()
to 
> move to the beginning of the file. Done :)

Not quite so done -- this solution just plain doesn't work. When I want
to prepend text to the beginning of a file, I use perl's CLI edit in
place like this:

perl -ni.bak -we'print "Some next text here\n" if $. == 1; print;'
filetoedit.txt

Or from within a program:

push @ARGV, 'foobar.txt';
$^I = '.bak';
while (<>) {
        print "Some new text here.\n" if $. == 1;
        print; # print $_
}

Or, as suggested in the FAQ, Tie::File is good for stuff like this:

Use Tie::File;

my $file = 'textfile.txt';
my @tied;

tie @tied, 'Tie::File', $file or die $!;

unshift @tied, 'My new PRE-pended line!';
untie @tied;

None of these things actually 'edit the file in place', they re-write
the file. Hope this helps.

See also: perldoc -q append


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to