Gossamer Forum
Home : General : Perl Programming :

Sub Routine question

Quote Reply
Sub Routine question
How would you open a file, and replace all occurances of a phrase with another phrase, and save the file?

thanks,
Nicholas

------------------
Quote Reply
Re: Sub Routine question In reply to
One method is to read, substitute then overwrite.
Code:
open (RFILE, "<path to your file") or &error("Unable to read your file");
if ($flock eq "y") {
flock RFILE, 2;
}
@file=<RFILE>;
close (RFILE);

foreach $line (@file) {
if ($line=~ s/your existing phrase here/your new phrase here/g) {
}
}

open (WFILE, ">path to your file") or &error("Unable to write your file");
if ($flock eq "y") {
flock WFILE, 2;
}
print WFILE @file;
close (WFILE);

[This message has been edited by Dave (edited July 16, 1999).]
Quote Reply
Re: Sub Routine question In reply to
Thanks. One last question. How would you (given a base dir) make a list of every file from that root dir on up?

------------------
Quote Reply
Re: Sub Routine question In reply to
CEGlobe,

I already wrote this script Smile
http://find.virtualave.net/00/


Regards,

Pasha

------------------
find.virtualave.net
Quote Reply
Re: Sub Routine question In reply to
I just tried it and it didn't work. It just kep saying website found, and then it said cannont find server.

------------------


[This message has been edited by CEGlobe (edited July 17, 1999).]