Vinay Thombre wrote at Tue, 05 Aug 2003 17:31:32 +0530:

> I am novice to Perl and learning very basic things. I want to replace a 
> text in a file with new text. I want to do it programatically. How can I
> do that?

What have you learned so far yet?
What is your tutorial?
What does it say about replacting texts in a file?
What have you tried so far?
What are your exact problems?

I'm asking as it is simpler to write a good answer if we know more about
your knowledge and the problems you are running.
Especially as it is hard to believe that no example of a file changing is
shown in any tutorial.

> I do not want to use Perl command line argumanets. Can anyone help?

Well, you could e.g. do something like

use strict;
use warnings;

open FILE, '<filename' or die "Can't open file: $!";
open TEMP, '>filename.bak' or die "Can't open temporary file: $!";

while (<FILE>) {
    s/windows/linux/;   # Upgrade your system
    print TEMP;
}

close FILE;
close TEMP;

rename 'filename.bak', 'filename' 
   or die "Could not rename temporary to actually file: $!";


Another way would be perhaps to use the module Tie::File.

But why won't you use command line arguments?
The whole above script could be rewritten as

perl -pi -e 's/windows/linux' filename

what is much easier.


Greetings,
Janek

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to