[EMAIL PROTECTED] wrote:

>Hi,
>
>I have to search and replace 'foo' to 'bar' in all the files in a
>directory(has subdirectories). The files are about 40k in size.
>
>What is the most efficient way to implement this inside a perl
>program ? There are about 30 files to be processed. I went through
>perldoc perlrun and saw the code.
>
I don't know if this is the most efficient way, but it works:

#!/usr/bin/perl -w

use strict;
use File::Find;

find \&process, $ARGV[0];
sub process {
    $filename = $File::Find::name;
    open IN, "$filename" or die "Couldn't open $filename for reading: $!";
    $^I = "";
    while (<IN>) {
        s/foo/bar/g;
    }
    close IN;
    print "Updated file $filename\n";
}

- Jan
-- 
If all else fails read the instructions. - Donald Knuth

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to