On Mon, Jun 09, 2003 at 02:46:48AM -0500 christopher j bottaro wrote:

> what is the easiest way to replace text in a file?  say i have file blah.txt 
> and i want to replace some regular expression found in that file with 
> something.  also, i want to do this from within the perl program, not by 
> invoking perl with the -e option.

You can use the same underlying technique from within a Perl script. You
have to set two special variables accordingly and Perl can even do an
inplace-edit:

    local $^I = 1;              # enable inplace editing
    local @ARGV = "blah.txt";   # make it accessible with <>
    while (<>) {
        s/blabla/BLABLA/;
        print;
    }

This however might not work on Windows due to some limitations concerning
open files. 

So if the above is no option for you, you have to do it manually:

    open IN,  "blah.txt"     or die $!;
    open OUT, "blah.txt.tmp" or die $!;
    
    while (<IN>) {
        s/blabla/BLABLA/;
        print OUT;
    }
    
    close IN;
    close OUT;
    
    rename "blah.txt.tmp", "blah.txt" or die "Renaming: $!";

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Reply via email to