in reply to Searching and Replacing file content within directory

I think a better way to recursively process all files would be to pass your processing subroutine as a code reference to File::Find.

use warnings; use strict; use File::Find my $curUrl = "http://localhost:8080"; my $tarUrl = "http://localhost"; sub process_files { next unless -f $File::Find::name; print "Processing $File::Find::name\n"; open FILE, "+>", $File::Find::name or die "Could not open file $File::Find::name"; while (my $line = <FILE>) { $line =~ s/$curUrl/$tarUrl/g; } close FILE; } find( &process_files, $ARGV[0] );

It is a bit old school perl, but sometimes I find the old tools are the best.

Replies are listed 'Best First'.
Re^2: Searching and Replacing file content within directory
by mjscott2702 (Pilgrim) on Nov 12, 2010 at 15:10 UTC
    I too would advocate using the File::Find approach - only issue I can see with the solution from chrestomanci is that it doesn't update the files themselves.

    To emulate what perl's -i switch does, first rename each file with an extension like .orig, open that new file for reading, and write output to the original filename.