[Date Prev][Date Next][Thread Prev][Thread Next]
[Search]
[Date Index]
[Thread Index]
Re: [MacPerl] How to modify a text file
On Tue, Jun 08, 1999 at 10:09:23AM -0400, Ingo Bitsch wrote:
> Another newbie-question:
>
> What is the standard way of modifying a text file with Perl? I
> experimented a bit with all the modifiers in front of the filename in the
> "open"-function, but I always ended up opening the file TWICE:
>
> open (IN, "file.txt");
> @all_lines=<IN>;
> $_ = join "",@all_lines;
>
> #(Do some operations on $_ here)
>
> open (OUT, ">file.txt");
> print OUT;
>
> I can't believe that there is no other way of doing it. Another thing
> that makes me nervous about my approach is that there is a moment in the
> script (after the second "open") when the contents of my file is alrady
> erased and lives only in the $_-Variable...
>
Open for read-write access:
open(FH, "+<file.txt") or die "Unable to open file.txt: $!\n";
{
local $/;
$_ = <IN>;
}
#(Do some operations on $_ here)
seek(FH, 0, 0) or die "Unable to seek in file.txt: $!\n";
print FH;
This is mentioned in the documentation for open() in perlfunc.
Ronald
===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org