Perhaps this is more a point of "style", but to me, your sub replace should either open the file and loop through the contents, or simply replace the text. Openening the file outside the sub, then letting the sub loop through it just feels odd to me. Here's how i might do it:
use strict;
open (INFILE, $ARGV[0]) or die "Unable to open file '$ARGV[0]' - $!";
while(<INFILE>) {
print replace($_);
}
close (INFILE);
sub replace {
my $line = shift;
chomp ($line);
$line =~ s/data|=|detector//g;
return $line;
}
Tested on this file:
xyzzy
data abc
detector = data
hooplah =
enddata
From the command line (note: i opted to use > to print to an OUTFILE rather than do it in the script):
C:\>perl a.pl test.txt > out.txt
Results:
xyzzy abc hooplah end
HTH
Update: Fixed some minor things with the code. Also, as you can see, my output is on one line due to the fact that we're chomping each line. You may wish to leave that out if you want to preserve the lines.
--
Rock is dead. Long live paper and scissors!