Title: RE: read/write simultaneously?

You are opening the file to write, so it opens the
file and positions at the top of the file, if you
don't write anything, you get an empty file at
the end.

Practically speaking, the best way to do this sort
of thing is like this:

use File::Copy;
move("test.txt", "test.bak") || die "Could not move file: $!\n";
open(TESTIN, "<test.bak") || die "Could not open input: $!\n";
open(TESTOUT, ">test.txt") || die "Could not open output: $!\n";
while (<TESTIN>) {
    s/jimmy/thomas/;
    print TESTOUT;
}
close TESTIN;
close TESTOUT;
unlink("test.bak") # may not want to do this, up to you

Or, do it as a one-liner:

perl -pi.bak -e "s/jimmy/thomas/;" test.txt

-Peter

---
Peter A. Vogel
Manager, Engineering Operations
iReady Corporation
http://www.iready.com; http://www.iready.net


> -----Original Message-----
> From: Jimmy S. Lim [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, May 04, 2000 5:12 PM
> To: Perl-Win32-Users Mailing List
> Subject: read/write simultaneously?
>
>
> how do I use the s/ / / to read/match and then later to
> write/substitute
> words into the same existing file? i tried this below but it
> just wipes out
> my entire file.
>
> #!/usr/bin/perl
> open(TEST, ">test.txt") or die "Can't open PRICE: $!\n";
> $file = <TEST>;
> $file =~ s/jimmy/thomas/;          # or $file = tr/jimmy/thomas/;
>
>
>
> ---
> You are currently subscribed to perl-win32-users as:
> [EMAIL PROTECTED]
> To unsubscribe, forward this message to
>          [EMAIL PROTECTED]
> For non-automated Mailing List support, send email to 
>          [EMAIL PROTECTED]
>

Reply via email to