while (my $line = <FILE>) {
$line =~ s/$curUrl/$tarUrl/g;
}
says to insert each line of FILE into the variable $line, then perform a substitution on the $line variable. Notably, you never printed $line to a file, so it can't end up in any file. The only thing you changed was the $line variable.
Some preliminary things first: you should NOT use bareword file handles. The modern way to open a file is like this:
open my $INFILE, '<', 'some_file'
or die "Couldn't open $fname: $!";
You should use a variable instead of a bareword filehandle, and you should include $! in the error message, which contains an explanation of what went wrong.
You need to realize first and foremost that generally you CANNOT do inplace editing of a file. You have to read the old file line by line and write each line to a new file. You alter the lines you want before printing them to the new file. Then you can delete the old file, unlink(), and rename the new file to the old file name, rename(). You should try doing that by hand before employing some of perl's tricks, which allow you to mimic that process behind the scenes.
In any case, the -i command line switch trick, which mimics inplace editing, can be duplicated inside a perl script by assiging a chosen backup extension to perls "in place editing" global variable $^I. Thereafter, STDOUT will be redirected to the new file, i.e. print()'s will go to the new file, and perl will handle the deleting and renaming behind the scenes.
$^I (or $INPLACE_EDIT) tells perl that files processed by the <> construct are to be edited in-place. It's the same as the -i command line switch.
http://www.tek-tips.com/faqs.cfm?fid=6549
The code for doing that will probably confuse you more than doing it by hand yourself (untested):
{
local $^I = ".backup";
@ARGV = @files;
while (<>) {
s/$target/$replacement/g;
print;
}
}
...or better:
...
use English '-no_match_vars';
...
{
local $INPLACE_EDIT = ".backup";
@ARGV = @files;
while (<>) {
s/$target/$replacement/g;
print;
}
}
Note that the code above creates files with the extension .backup for every file. I cannot find any information about what you are supposed to assign to $^I if you don't want backup files--maybe a blank string? But I read that Windows won't let you use $^I without creating backup files anyway.
I think the value of $^I works like this:
$^I = undef; #default value, Turn off inplace editing mode.
$^I = "some_string"; #Turn on inplace editing mode, use "some_string"
#as the extension for the backup files.
$^I = ""; #Turn on inplace editing mode, delete backup files
+.
|